現状、Apache+mod_phpをnginxで置き換えるとすると、nginx+spawn-fcgi+php-cgiを使ってる人が多いと思うんだけど、Ubuntuの公式リポジトリだとnginxのバージョンが低く(0.7系)、spawn-fcgiはinit.dスクリプトを書くかdaemontools使わなきゃいけないし、Apache+mod_phpみたいにインストール一発で動かせないので割とメンドクサイ。
同じこと思ってる人はnginxチームが提供しているaptリポジトリを使うと、nginx1.0.2+php5.3.5+php-fpmをaptでインストールできるので楽になりますよ。
既にnginxがインストールされている場合は念のためremoveしておく、spawn-cgiなどはkillしておきましょう。
以下、手順。
# nginxリポジトリを登録 $ sudo echo "deb http://ppa.launchpad.net/nginx/stable/ubuntu lucid main" > /etc/apt/sources.list.d/nginx-stable-lucid.list $ sudo echo "deb http://ppa.launchpad.net/nginx/php5/ubuntu lucid main" > /etc/apt/sources.list.d/nginx-stable-lucid.list $ sudo echo "deb-src http://ppa.launchpad.net/nginx/php5/ubuntu lucid main" > /etc/apt/sources.list.d/nginx-stable-lucid.list $ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C # aptitudeのリストを更新 $ sudo aptitude update # nginxをインストール $ sudo aptitude install nginx # PHP関連をインストール $ sudo aptitude install php5 php5-cgi php5-cli php5-mysql php5-memcached php5-gd php5-curl php-pear php-apc php-fpm
インストールできたら、プロセスを確認してみる。
$ ps aux | grep nginx root 14051 0.0 0.1 71092 1568 ? Ss May28 0:00 nginx: master process /usr/sbin/nginx www-data 14052 0.1 0.2 72496 3568 ? S May28 3:47 nginx: worker process $ ps aux | grep php root 14238 0.0 0.3 182432 4944 ? Ss May28 0:25 php-fpm: master process (/etc/php5/fpm/main.conf) www-data 14255 0.1 3.1 192428 48292 ? S May28 4:54 php-fpm: pool www www-data 14256 0.1 4.0 207328 62988 ? S May28 4:43 php-fpm: pool www www-data 14258 0.1 3.8 202720 59212 ? S May28 4:36 php-fpm: pool www www-data 14291 0.1 3.6 199648 55496 ? S May28 4:39 php-fpm: pool www www-data 14293 0.1 3.3 196576 52260 ? S May28 4:51 php-fpm: pool www www-data 14294 0.1 3.4 196576 52952 ? S May28 4:47 php-fpm: pool www www-data 14297 0.1 4.5 214756 70368 ? S May28 4:34 php-fpm: pool www www-data 14298 0.1 3.1 192336 48900 ? S May28 4:46 php-fpm: pool www www-data 14299 0.1 3.1 192332 48856 ? S May28 5:05 php-fpm: pool www www-data 14300 0.1 2.9 191044 46132 ? S May28 4:58 php-fpm: pool www
ちゃんとユーザーwww-dataでphp-fpmのプロセスが起動されてますね。一番上のrootで実行されているのでphp-fpmの子プロセスを管理してるマスタープロセスです。起動していないければsudo service nginx startとsudo service php-fpm startで起動させて再度確認します。
起動するプロセス数やfastcgiのlisten URLなどは/etc/php5/php-fpm/pool.d/www.confにて設定します。
次に、nginxのバーチャルホストの設定。
下の設定だとwordpressとCakePHPは動くはず。
# /etc/nginx/sites-avilable/example.com
# www.example.comへのアクセスをexample.comへリダイレクト
server {
listen 80;
server_name www.example.com;
rewrite ^(.*) http://example.com$1 permanent;
}
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
root /var/www/example.com;
index index.php index.html;
try_files $uri $uri/ /index.php?q=$1;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
location ~ /\.ht {
deny all;
}
}
この内容を/etc/nginx/sites-available/example.comに保存し、/etc/nginx/sites-enabled/example.comからシンボリックリンクを張ります。
$ sudo vi /etc/nginx/sites-available/example.com $ cd $ sudo ln -s /etc/nginx/sites-availabe/example.com /etc/nginx/sites-enabled/example.com
ここまでやったらnginxとphp-fpmを再起動して、http://example.comをブラウザで開き、正常に動けばOKです。ダメだったら何とかしましょう。