Apache2 + MPM Worker + mod_fastcgi + php5-fpm でWordPressを動かす
今までQMACloneはTomcat6に付属のHTTP Connectorを使用してhttpをサーブしていた。つい先日、httpのサーブはApacheを使用し、AJP Connector等でTomcatと繋げたほうが通信が安定するという都市伝説をネット上で見かけた。なんでもSocket通信のエラーハンドリングがTomcat6のJVMのものよりApacheのモノの方がしっかり作られているらしい。ということで早速やってみた。
ところが・・・、Apache2のMPMをPreforkのままにしたため、一つのhttpコネクションに対して一つのプロセスが作られ、最終的には約800のApacheプロセスが立ち上がり、あわやスワップアウトを起こしかけるという状態になってしまった。
これを防ぐためにMPMをWorkerに切り替えることにした。ところが・・・、MPM Workerではmod_phpが使用できないらしく、トップページのWordPressが動かなくなってしまった。色々調べたところmod_fastcgi + php5-fpmならMPM Workerでも動かすことができ、かつキャッシュが効くのでそこそこの速度が出るという情報を得た。
以下に備忘録として作業内容を残しておく。
作業内容
php5-fpmとmod_fastcgiをインストールして、php5-fpmを起動しておく。
% sudo aptitude install php5-fpm libapache2-mod-fastcgi % sudo update-rc.d php5-fpm enable % sudo service php5-fpm restart
必要となるモジュールを有効にする
% cd /etc/apache2/mods-enabled % sudo ln -s ../mods-available/actions.conf % sudo ln -s ../mods-available/actions.load % sudo ln -s ../mods-available/rewrite.load
cgiを有効にする
% sudoedit /etc/apache2/sites-enabled/000-default OptionsにExecCGIを追加 AllowOverrideをAllに変更
Ubuntuに含まれるphp5-fpmパッケージに合わせてfastcgiの設定をする。標準設定ではバイナリの場所は"/usr/sbin/php5-fpm"、Socketのパスは"/var/run/php5-fpm.sock"とのこと。
% sudoedit fastcgi.conf <IfModule mod_fastcgi.c> FastCgiExternalServer /usr/sbin/php5-fpm -socket /var/run/php5-fpm.sock AddHandler php-fastcgi .php ScriptAlias /fcgi-bin/ /usr/sbin/ Action php-fastcgi /fcgi-bin/php5-fpm </IfModule>
この時点でApacheを再起動してWordPressが正しく動くことを確認する。
% sudo service apache2 restart
最後にMPM PreforkからMPM Workerに切り替える。
% sudo aptitude install apache2-mpm-worker
以上でApache2 + MPM Worker + mod_fastcgi + php5-fpmが動くようになった。
レスポンスは以下の通り。
% ab -n 10 http://kishibe.dyndns.tv/ This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking kishibe.dyndns.tv (be patient).....done Server Software: Apache/2.2.22 Server Hostname: kishibe.dyndns.tv Server Port: 80 Document Path: / Document Length: 11589 bytes Concurrency Level: 1 Time taken for tests: 0.684 seconds Complete requests: 10 Failed requests: 0 Write errors: 0 Total transferred: 119100 bytes HTML transferred: 115890 bytes Requests per second: 14.62 [#/sec] (mean) Time per request: 68.418 [ms] (mean) Time per request: 68.418 [ms] (mean, across all concurrent requests) Transfer rate: 170.00 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 7 8 1.3 8 11 Processing: 48 60 8.5 61 72 Waiting: 47 59 8.4 59 70 Total: 56 68 8.3 69 80 Percentage of the requests served within a certain time (ms) 50% 69 66% 72 75% 77 80% 79 90% 80 95% 80 98% 80 99% 80 100% 80 (longest request)
同時実行性や負荷耐久などは調べていないが、トップページにアクセスする人は少ないのでこれで良しとする。
リンク
- apache の FastCGI(mod_fastcgi) + PHP-FPM で phpを動かしてみる | レンタルサーバー・自宅サーバー設定・構築のヒント http://server-setting.info/centos/apache-mod_fastcgi-php-fpm.html
- Apache module mod_fastcgi http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html
- FastCGI | FastCGI - http://www.fastcgi.com/drupal/