CentOS 7 環境における LAMP ストックのソースビルド構成

前置き:依存パッケージとユーザ設定

まず、コンパイルに必要な開発ライブラリ類をシステムに導入します。

# コンパイラおよび基本ツール群
yum -y install gcc gcc-c++ g++ cpp make cmake ncurses-devel automake autoconf
# ネットワークおよび画像処理ライブラリ
yum -y install wget sudo libxml2-devel libpng-devel freetype-devel bzip2-devel
# セキュリティ関連およびデータベース連携用ライブラリ
yum -y install openssl openssl-devel pcre-devel zlib-devel curl-devel gd-devel
yum -y install geoip-devel gmp-devel libmcrypt-devel libtool

# 不要になった古いパッケージのクリーンアップ(環境によっては省略可能)
rpm -qa | grep -E "gcc|wget|openssl|make" | xargs rpm -e --nodeps

Web サービス実行用の専用グループとユーザを作成します。

groupadd -f www
useradd -g www www

Nginx WEB サーバの構築

安定版である 1.18.0 をダウンロードし展開します。

wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0

SSL 暗号化対応のため、OpenSSL のソースも別途用意する必要があります。

mkdir -p /usr/local/src
cd /usr/local/src
wget https://openssl.org/source/openssl-1.0.2l.tar.gz
tar zxvf openssl-1.0.2l.tar.gz
cd ../nginx-1.18.0

以下のオプションを指定して設定スクリプトを実行します。IPv6 や HTTP/2、gzip 静圧縮モジュールなどを有効化しています。

./configure \
  --prefix=/usr/local/nginx \
  --user=www \
  --group=www \
  --with-http_stub_status_module \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_gzip_static_module \
  --with-http_sub_module \
  --with-stream \
  --with-openssl=/usr/local/src/openssl-1.0.2l

設定完了後、Makefile を作成しインストーラーを実行します。

make && make install

システムのサービス管理スクリプトとして登録するため、以下の内容を /etc/init.d/nginx に作成します。

#!/bin/sh
# chkconfig: 2345 55 25
# description: Nginx Web Server Startup Script

NGINX_SBIN='/usr/local/nginx/sbin/nginx'
NGINX_CONF='/usr/local/nginx/conf/nginx.conf'

start() {
    if [ -e /usr/local/nginx/logs/nginx.pid ]; then
        echo "Nginx is already running."
        exit 1
    fi
    $NGINX_SBIN -c $NGINX_CONF
    echo "Nginx started successfully."
}

stop() {
    if [ ! -e /usr/local/nginx/logs/nginx.pid ]; then
        echo "Nginx is not running."
        exit 1
    fi
    $NGINX_SBIN -s stop
    echo "Nginx stopped."
}

status() {
    if pgrep -f "$NGINX_SBIN" > /dev/null; then
        echo "Nginx is running."
    else
        echo "Nginx is stopped."
        exit 1
    fi
}

case "$1" in
    start) start ;;
    stop) stop ;;
    status) status ;;
    restart) $0 stop; sleep 1; $0 start ;;
    reload) $NGINX_SBIN -s reload ;;
    *) echo "Usage: $0 {start|stop|status|restart|reload}" ;;
esac

スクリプトへの実行権限を与え、起動に登録します。

chmod +x /etc/init.d/nginx
chkconfig nginx on
/etc/init.d/nginx start

データベースエンジン MySQL のインストール

mysql ユーザを作成後、バイナリソースを取得します(ここでは 5.6.40 を使用)。

groupadd mysql
useradd -r -g mysql mysql
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.40.tar.gz
tar zxvf mysql-5.6.40.tar.gz
cd mysql-5.6.40

CMake を利用してビルド設定を行います。データディレクトリやポート番号を定義します。

cmake \
  -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
  -DMYSQL_DATADIR=/usr/local/mysql/data \
  -DSYSCONFDIR=/usr/local/mysql \
  -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  -DDEFAULT_CHARSET=utf8 \
  -DDEFAULT_COLLATION=utf8_general_ci \
  -DENABLED_LOCAL_INFILE=1 \
  -DENABLE_DTRACE=0 \
  -DMYSQL_TCP_PORT=3306

make && make install
echo "/usr/local/mysql/lib" >> /etc/ld.so.conf.d/mysql.conf
ldconfig

/etc/my.cnf に以下のパラメータを設定し、パフォーマンスチューニングを行います。

[client]
port = 3306
socket = /tmp/mysql.sock

[mysqld]
port = 3306
socket = /tmp/mysql.sock
datadir = /usr/local/mysql/data
skip-external-locking
key_buffer_size = 32M
max_allowed_packet = 1M
thread_cache_size = 16
query_cache_size = 16M
innodb_buffer_pool_size = 128M
log-bin=mysql-bin
server-id = 1
default-storage-engine = InnoDB

初期化スクリプトを実行し、デーモンを起動します。

cd /usr/local/mysql
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
/etc/init.d/mysqld start
chkconfig mysqld on

PATH を設定し、セキュリティ設定ウィザードを実行します。

export PATH=$PATH:/usr/local/mysql/bin
source /etc/profile
mysql_secure_installation

PHP フィルタ処理ユニット(FPM)のセットアップ

PHP ソースをダウンロード・展開します(バージョン 5.3.29)。

wget http://mirrors.sohu.com/php/php-5.3.29.tar.gz
tar -zxvf php-5.3.29.tar.gz
cd php-5.3.29

必要となる拡張ライブラリを追加インストールします。

yum install -y libtidy-devel libxslt-devel libmcrypt-devel

以下のように configure オプションを指定してコンパイルを行います。

./configure --prefix=/usr/local/php \
  --with-config-file-path=/usr/local/php/etc \
  --enable-fpm --with-fpm-user=www --with-fpm-group=www \
  --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
  --with-iconv-dir --with-freetype-dir=/usr/local/freetype \
  --with-jpeg-dir --with-zlib --with-libxml-dir=/usr \
  --enable-mbregex --enable-mbstring --with-mcrypt \
  --with-gd --enable-gd-native-ttf --with-openssl \
  --enable-mhash --enable-soap --with-gettext

make && make install

設定ファイルを配置・編集します。

cp php.ini-production /usr/local/php/etc/php.ini
ln -s /usr/local/php/bin/php /usr/bin/php
sed -i "s/;date.timezone =/date.timezone = Asia\/Tokyo/" /usr/local/php/etc/php.ini
mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

php-fpm 設定を修正し、プロセス管理を最適化します。

[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
daemonize = yes

[www]
listen = 127.0.0.1:9000
user = www
group = www
pm = dynamic
pm.max_children = 80
pm.start_servers = 20
pm.min_spare_servers = 20
pm.max_spare_servers = 80

起動スクリプトを準備し、ログディレクトリを作成します。

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
mkdir -p /usr/local/php/logs
/etc/init.d/php-fpm start
chkconfig --add php-fpm

Nginx と PHP-FPM の連携設定

Nginx の設定ファイルにおいて、index.php を優先させます。

location / {
    root   html;
    index  index.html index.htm index.php;
}

PHP スクリプトの処理を FastCGI 経由で送る設定を追加します。

location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
    include        fastcgi_params;
}

設定変更を反映させます。

/usr/local/nginx/sbin/nginx -s reload

主要な PHP 拡張機能のビルド

PDO_Mysql 拡張の追加

wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
tar zxvf PDO_MYSQL-1.0.2.tgz 
cd PDO_MYSQL-1.0.2
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
make && make install
echo "extension=pdo_mysql.so" >> /usr/local/php/etc/php.ini

imagick(画像処理)、redis クライアント、yac(メモリキャッシュ)などのインストール例は以下の通りです。各モジュールの .so ファイルを php.ini に追記してください。

# imagick
yum install ImageMagick-devel
pecl install imagick

# redis
wget http://pecl.php.net/get/redis-4.3.0.tgz
tar zxvf redis-4.3.0.tgz 
cd redis-4.3.0
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

# yac
wget http://pecl.php.net/package/yac/0.9.2
tar zxvf yac-0.9.2.tgz 
cd yac-0.9.2
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

php.ini の末尾に以下の記述を追加することで有効化されます。

; imagick
extension=imagick.so
; redis
extension=redis.so
; yac
extension=yac.so

トラブルシューティング

php-fpm 再起動時に「Address already in use」エラーが発生する場合、前回の停止が正常に行われていない可能性があります。この際、PID ファイルの残存状態を確認し、手動で削除してから再度起動を試みてください。

タグ: centos7 nginx MySQL PHP FPM

7月22日 22:29 投稿