最後に、今回のチューニングの総仕上げとなる作業をしましょう。Apacheの構成を「prefork MPM+mod_php」構成から、「event MPM+php-fpm」構成に変更することで、さらなる最適化が図れます。
従来、Apacheの主流となるマルチプロセッシングモジュールは「prefork MPM」でしたが、Apache 2.4から「event MPM」が選択できるようになりました。event MPMは、fast cgi方式のphp-fpmと組み合わせることによって、従来のprefork MPM+mod_phpの組み合わせに比べて、メモリ使用量を大きく減らし、同時接続数増加時のパフォーマンス劣化も抑えられるメリットを得られるようになります。
イメージとしては、軽量高速で近年シェアを伸ばしているWebサーバの「Nginx(エンジンエックス)」に近いものです。Apacheがもともと持っている豊富なモジュールや.htaccessによる柔軟な設定制御などのメリットはそのままに、パフォーマンスが大幅に向上しますので、この構成は「新しいApache」といっても過言ではないと思います。
では、具体的に設定していきましょう。まず、php-fpmを導入し、実行します。
[root@ip plugins]# yum install php-fpm -y [root@ip plugins]# systemctl enable php-fpm [root@ip plugins]# systemctl start php-fpm
次に、Apacheの設定ファイルを変更します。変更するファイルは「/etc/httpd/conf.modules.d/00-mpm.conf」「/etc/httpd/conf.d/php.conf」「/etc/httpd/conf/httpd.conf」の3つです。
# Select the MPM module which should be used by uncommenting exactly # one of the following LoadModule lines: # prefork MPM: Implements a non-threaded, pre-forking web server # See: http://httpd.apache.org/docs/2.4/mod/prefork.html #LoadModule mpm_prefork_module modules/mod_mpm_prefork.so # worker MPM: Multi-Processing Module implementing a hybrid # multi-threaded multi-process web server # See: http://httpd.apache.org/docs/2.4/mod/worker.html # #LoadModule mpm_worker_module modules/mod_mpm_worker.so # event MPM: A variant of the worker MPM with the goal of consuming # threads only for connections with active processing # See: http://httpd.apache.org/docs/2.4/mod/event.html # LoadModule mpm_event_module modules/mod_mpm_event.so
<IfModule prefork.c>
#
# Cause the PHP interpreter to handle files with a .php extension.
#
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
#
# 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
#
# 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"
</IfModule>
(略)
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
(略)
# Supplemental configuration
#
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
# gzip setting
AddOutputFilterByType DEFLATE text/html text/plain text/css
AddOutputFilterByType DEFLATE text/javascript application/x-javascript application/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# expire setting
ExpiresActive On
ExpiresDefault "access plus 600 seconds"
ExpiresByType text/html "access plus 10 seconds"
# event MPM setting
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 50
ThreadsPerChild 50
MaxRequestWorkers 50
MaxConnectionsPerChild 0
<FilesMatch \.php$>
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
</IfModule>
Apacheを再起動して設定を反映させます。
[root@ip plugins]# systemctl restart httpd
筆者の環境では、ページのロード時間が33ms、Requests per secondは60.79となり、さらに約5%のパフォーマンス向上を果たせました。
さっくり理解するPHP 5.5の言語仕様と「いい感じ」の使い方
WordPress自体のチューニングが必要な理由と高速化の基本的な考え方
ここが変わったCentOS 7──「新機能の概要とインストール」編
安いホスティングに引っ越しって簡単にいうけど 〜リモートアクセスにはSSHを使いましょう〜
Webのバグを燃やしまくるFirebugと、そのアドオン7選
httpd.confによるWebサーバの最適化
Apacheパフォーマンス・チューニングのポイント
Apacheパフォーマンス・チューニングの実践
MySQLの高度な管理とチューニングテクニック
安全を考えてPHPの実行時設定を調整するCopyright © ITmedia, Inc. All Rights Reserved.