LoginSignup
1
2

More than 5 years have passed since last update.

Apache 2.4 PHP7.X MariaDB をインストール (CentOS 6.8)

Last updated at Posted at 2017-04-14

CentOS 6.8 で PHP 7.X MariaDB 10.1.X をインストールにトライしました。

サーバーの確認

cat /etc/redhat-release # CentOS & RHLE バージョン確認
arch

Sudo user を追加

add user [ユーザー名]
usermod -aG wheel [ユーザー名] # wheel サブグループに追加
vi /home/[ユーザー名]/.ssh/authorized_keys #アクセスする側の pub キーを追加
vi /etc/sshsshd_config # 下記「/etc/ssh/sshd_config」の説明を参照
visudo # 下記「visudo」の説明を参照
/etc/init.d/sshd restart
# 現在のターミナルを閉じずに、きちんと root & 新ユーザーがアクセスできるか必ず確認!

config ファイルを書き換えていないと、アクセスできなくなる、非常に危険な作業なので、開いているターミナルを閉じる前にきちんと設定が変更されているか、確認してからターミナルを閉じましょう。間違っていたら開いているままのターミナルで設定をもとに戻したり出来るので安心。

/etc/ssh/sshd_config

  • root がログインできないようにする
  • 該当ユーザーはパスワード無し、秘密鍵のみでログインできるように設定
PermitRootLogin no

Match User [ユーザー名]
PasswordAuthentication no

visudo

  • sudo をパスワードなしで実行できるように設定
  • wheel グループに所属しているユーザーは sudo 可能にする
  • visudo で下記のラインのコメントを外して実行する
## Same thing without a password
%wheel  ALL=(ALL)       NOPASSWD: ALL

サーバーのタイムゾーンを変更

UTC だったので、日本時間に変更

cp /etc/localtime /root/localtime.utc
rm /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

# 確認
date

YUM のレポジトリをインストール

参考
http://qiita.com/soundws/items/cc84da42419f1ab3443b

cd
wget http://ftp.riken.jp/Linux/fedora/epel/RPM-GPG-KEY-EPEL-6
rpm --import RPM-GPG-KEY-EPEL-6 
rm -f RPM-GPG-KEY-EPEL-6
vi /etc/yum.repos.d/epel.repo
# 以下の内容で新規作成
#--------
[epel]
name=EPEL RPM Repository for Red Hat Enterprise Linux
baseurl=http://ftp.riken.jp/Linux/fedora/epel/6/$basearch/
gpgcheck=1
enabled=0
#--------
yum install epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
rpm -Uvh http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm

libmycrypt のインストール

yum --enablerepo=epel install libmcrypt

PHP のインストール

参考
https://tecadmin.net/install-php-7-apache-2-4-mysql-on-centos-rhel/

cd
yum install php71w
yum install php71w-common
yum install php71w-devel
yum install php71w-pecl-memcached
yum install php71w-mysqlnd
yum install php71w-soap
yum install php71w-opcache
yum install php71w-xmlrpc
yum install php71w-mbstring
yum install php71w-intl
yum install php71w-gd
yum install php71w-pecl-imagick
yum install php71w-mcrypt

Apache のインストール

yum --enablerepo=epel,remi install httpd
/etc/rc.d/init.d/httpd configtest
/etc/rc.d/init.d/httpd start
yum install mod_ssl
yum install openssl
  • ServerName を設定
  • ServerAdmin を設定
  • log ファイルを /var/logs/httpd/ 内に保存するよう変更
  • conf.d ディレクトリの中に追加の設定ファイルを追加するように設定
  • IP 直接アクセスを防ぐために Dummy ファイルを作成 (下記参照)
  • - /etc/httpd/conf.d/vhost_XXXXX.com.conf に vhost 設定を追加

/etc/httpd/conf/httpd.conf

チューニング。

サーバーのスペックにもよりますが、下記とかに設定

Timeout 60
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 120
<IfModule prefork.c>
StartServers         10
MinSpareServers       5
MaxSpareServers      10
ServerLimit         512
MaxClients          512
MaxRequestsPerChild  10
</IfModule>

PHP 71 の設定を LoadModule のあとぐらいに追加

#
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#

# Cannot load both php5 and php7 modules
<IfModule !mod_php5.c>
  <IfModule prefork.c>
    LoadModule php7_module modules/libphp7.so
  </IfModule>
</IfModule>


<IfModule !mod_php5.c>
  <IfModule !prefork.c>
    LoadModule php7_module modules/libphp7-zts.so
  </IfModule>
</IfModule>

# PHP7
#
# The following lines prevent .user.ini files from being viewed by Web clients.
#
<Files ".user.ini">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
        Satisfy All
    </IfModule>
</Files>

#
# Allow php to handle Multiviews
#
AddType text/html .php

#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php

# mod_php options
<IfModule  mod_php7.c>
    #
    # Cause the PHP interpreter to handle files with a .php extension.
    #
    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

    #
    # Uncomment the following lines to allow PHP to pretty-print .phps
    # files as PHP source code:
    #
    #<FilesMatch \.phps$>
    #    SetHandler application/x-httpd-php-source
    #</FilesMatch>

    #
    # Apache specific PHP configuration options
    # those can be override in each configured vhost
    #
    php_value session.save_handler "files"
    php_value session.save_path    "/var/lib/php/session"
    php_value soap.wsdl_cache_dir  "/var/lib/php/wsdlcache"
    php_value opcache.file_cache   "/var/lib/php/opcache"
</IfModule>

OPcache のファイルキャッシュを保存する用のディレクトリを念のために作成

cd /var/lib/php/
mkdir opcache
chown root:apache opcache/
chmod 770 opcache/

Apache の実行ユーザーとグループを変更 (Git 用に)

User git-user
Group apache

デフォルトのサーバー名を追加

ServerAdmin wedontshowsupportemail@example.com
ServerName www.example.com:80

.git 関連のファイルにアクセスされないように<Files ~ "^\.ht">な設定ぐらいの後に config を追加

# .git から始まるファイルへのアクセスを禁止
<Files ~ "^\.git">
Order allow,deny
Deny from all
</Files>

# .git/ フォルダ以下のファイル・ディレクトリへのアクセスを禁止
<Directorymatch "^/.*/\.git/">
Order deny,allow
Deny from all
</Directorymatch>

エラーメッセージに出てくる、サーバーの OS や Apache バージョン表示などを非表示に

ServerSignature Off

VirtualHost を使えるようにする設定を
最後あたりの Virtual Host で追加。

NameVirtualHost *:80
NameVirtualHost *:443

を追加

/etc/httpd/conf.d/00_vhost_dummy.conf

IP アドレスで直接叩かれたら、何も出ないように設定

  • /var/log/httpd ディレクトリが存在している必要あり
  • /var/www/vhosts/dummy ディレクトリを作成し、空の index.html ファイルを作成
<VirtualHost *:80>
  ServerName dummy

  DirectoryIndex index.php index.html

  CustomLog /var/log/httpd/dummy-access_log combined
  ErrorLog  /var/log/httpd/dummy-error_log

  DocumentRoot /var/www/vhosts/dummy

  <Directory "/var/www/vhosts/dummy">
    AllowOverride All
    Options -Indexes
    # Require all granted
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

<VirtualHost *:443>
  ServerName dummy

  DirectoryIndex index.php index.html

  CustomLog /var/log/httpd/dummy-access_log combined
  ErrorLog  /var/log/httpd/dummy-error_log

  DocumentRoot /var/www/vhosts/dummy

  <Directory "/var/www/vhosts/dummy">
    AllowOverride All
    Options -Indexes
    # Require all granted
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

/etc/httpd/conf.d/10_vhost_ドメイン.conf

本番環境の vhost 設定

<VirtualHost *:80>
  ServerName example.co.jp
  ServerAlias www.exampleu.co.jp

  DirectoryIndex index.php index.html

  CustomLog /var/log/httpd/example.co.jp-access_log combined
  ErrorLog  /var/log/httpd/example.co.jp-error_log

  DocumentRoot /var/www/vhosts/example.co.jp

  <Directory "/var/www/vhosts/example.co.jp">
    AllowOverride All
    Options FollowSymLinks
    Options -Indexes
    #Require all granted
  </Directory>
</VirtualHost>
/etc/rc.d/init.d/httpd configtest
# エラーがないことを確認して、再起動
/etc/rc.d/init.d/httpd restart

Apache を OS 再起動時も自動的に起動するように設定

chkconfig httpd on

MariaDB をインストール

執筆q時点の安定版は 10.1.22

rpm --import https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
vi /etc/yum.repos.d/mariadb.repo
# vi
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1.21/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
enabled=1
# baseurl のところを安定版や自分の OS などに合わせる
yum install MariaDB-devel MariaDB-client MariaDB-server

エラーが出たら

rpm -qa | grep -i mysql

で残っている MySQL のパッケージを調べます。

yum remove [出てきたパッケージ名]

MariaDB をスタート

/etc/init.d/mysql start

しようとしてもエラーが有る場合は、昔のデータが残っているかも。

/var/lib/mysql
の中が昔のデータなので、必要なければ、一旦、MariaDB のインストールを削除して、データを削除してから、インストールし直してください。

Root ユーザーのパスワードをつけましょう。

/usr/bin/mysqladmin -u root password 'new-password'
vi /etc/my.cnf.d/server.cnf

文字コードを UTF8 にセット

[mysqld]
character-set-server = utf8
/etc/init.d/mysql restart

MariaDB のユーザー & DB を作成

mysql -uroot -p[パスワード]
# -u と -p の後のユーザー名とパスワードの間にはスペース無し
CREATE USER 'concrete5'@'127.0.0.1' IDENTIFIED BY 'パスワード';
CREATE USER 'concrete5'@'localhost' IDENTIFIED BY 'パスワード';
CREATE DATABASE [データベース名] DEFAULT CHARACTER SET=utf8;
GRANT ALL PRIVILEGES ON [データベース名].* TO 'concrete5'@'127.0.0.1' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON [データベース名].* TO 'concrete5'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit;

Git を 1.7.x から最新のにインストール

cd /usr/local/src
yum remove git
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker
# 最新版を https://www.kernel.org/pub/software/scm/git/ から確認
wget https://www.kernel.org/pub/software/scm/git/git-2.12.0.tar.gz
tar -zxf git-2.12.0.tar.gz
cd git-2.12.0
make prefix=/usr/local all
make prefix=/usr/local install

etckeeper のインストール

参考
https://heartbeats.jp/hbblog/2013/09/etckeeper.html

yum --enablerepo=epel install etckeeper
etckeeper init

# 結果が
# fatal: Not a git repository (or any of the parent directories): .git
# だったら
etckeeper init
etckeeper commit "initial commit"
sudo su -
ssh-keygen -t rsa -b 4096 -C "XXXXXX@concrete5.co.jp"
# デフォルト設定で SSH Key を生成
vi .ssh/id_rsa.pub
# SSH 公開鍵をリモートレポジトリ(origin/master)に登録
cd /etc
git remote add origin ssh://【git のリモートレポジトリURL】
git push origin master
sed -i -e 's/PUSH_REMOTE=""/PUSH_REMOTE="origin"/g' /etc/etckeeper/etckeeper.conf

Let's encrypt をインストール

参考
http://qiita.com/hirohiro77/items/8506493e042ddfbe6e1f
https://blog.doizaki.com/entry/2016/02/07/152141

yum install centos-release-SCL
yum update
yum install scl-utils python27 python27-scldevel
scl enable python27 bash
cd /usr/local/
git clone https://github.com/letsencrypt/letsencrypt
cd letsencrypt
./letsencrypt --help
./letsencrypt-auto -d XXXXXX.co.jp  --apacheP
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2