やっぱりApacheのメモリ消費量が多い気がしてきたのでnginxに移行しました。要点としては、mod_phpのようなものはないため、FastCGIでPHPを動作させる必要があると言うこと。
インストールは大して難しくないのですが、いかんせん日本語での情報が少ないのが難点です。基本的には公式wikiのPHP/FastCGI Exampleを参考にすれば問題ありません。以下、Debian (lenny) での作業ログです。
まずは必要なパッケージをインストールします。その前に、必要に応じてApacheを停止しておきます。
1 2 | % sudo invoke-rc.d apache2 stop % sudo aptitude install php5-cgi nginx |
次にFastCGI用daemonのスクリプトを作成。
1 | % sudo vim /etc/init.d/php5-fastcgi |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #!/bin/bash BIND=127.0.0.1:9000 USER=www-data PHP_FCGI_CHILDREN=2 PHP_FCGI_MAX_REQUESTS=1000 PHP_CGI=/usr/bin/php5-cgi PHP_CGI_NAME=`basename $PHP_CGI` PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND" RETVAL=0 start() { echo -n "Starting PHP FastCGI: " start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS RETVAL=$? echo "$PHP_CGI_NAME." } stop() { echo -n "Stopping PHP FastCGI: " killall -q -w -u $USER $PHP_CGI RETVAL=$? echo "$PHP_CGI_NAME." } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; *) echo "Usage: php5-fastcgi {start|stop|restart}" exit 1 ;; esac exit $RETVAL |
作成したら実行権限をつけて自動起動の設定をし、起動しておきます。
1 2 3 | % sudo chmod a+x /etc/init.d/php5-fastcgi % sudo update-rc.d php5-fastcgi defaults % sudo invoke-rc.d php5-fastcgi start |
次にnginxの設定です。DebianではApacheと同じく、/etc/nginx/site-availableに設定ファイルを入れて、有効にしたいものだけ/etc/nginx/site-enabledにシンボリックリンクを張る感じになります。今回は/etc/nginx/site-available/wordpressと言うファイル作ります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | # for wordpress upstream wordpress { server 127.0.0.1:9000; } server { listen 80 default; server_name stnard.jp; location / { root /var/www; index index.php index.html; # static files if (-f $request_filename) { expires 30d; break; } # request to index.php if (!-e $request_filename) { rewrite ^(.+)$ /index.php?q=$1 last; } } location ~ \.php$ { fastcgi_pass wordpress; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name; } location ~ /\.ht { deny all; } access_log /var/log/nginx/wordpress.access.log combined; error_log /var/log/nginx/wordpress.error.log; } |
作成したらシンボリックリンクを張って、設定ファイルの構文チェックをします。ついでにデフォルト設定のシンボリックリンクは削除しておきます。
1 2 3 4 5 6 | % cd /etc/nginx/site-enabled/ % sudo ln -s /etc/nginx/site-available/wordpress ./ % sudo rm default % sudo nginx -t the configuration file /etc/nginx/nginx.conf syntax is ok configuration file /etc/nginx/nginx.conf test is successful |
上記で問題なければ、nginxを起動して動作確認。
1 | % sudo invoke-rc.d nginx start |
移行してみた感想としては、Apacheよりも何倍も高速になった気がします。恐らくはFastCGIをdaemon化したことが大きいのでしょうが、体感できる高速さはやはり魅力です。用途に応じてnginxやlighttpdなどの高速Webサーバを試してみるのもいいのではないでしょうか。




[...] I looked up this page for installing: http://stnard.jp/2010/04/20/115/ (Japanese Page); Thanks conceal_rs. [...]
[...] ちなみに、php-cgiでの導入手順はこちら:WordPress on nginx with FastCGIに移行してみた [...]
[...] WordPress on nginx with FastCGIに移行してみた – stnard.jp in Japanese [...]
[...] WordPress on nginx with FastCGIに移行してみた [...]
[...] 参考にしたのはこことこことここ。init [...]