2013年2月16日土曜日

apache+phpからnginx+php-fpmへ移行してみた

なんとなくapache2+phpからnginx+php-fpmへ移行してみた。別にapacheが遅いと感じている分けではないが、nginxも触っとくかなと。

まずはインストール。
# apt-get install php5-fpm nginx

nginx.confをいじる。あんまりわからないので下記の2つのコメントを外すだけにしとく。レスポンスヘッダーにバージョンを返さないようにするのと、cssやjsも圧縮するように。
# vi /etc/nginx/nginx.conf
server_tokens off;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

serverの設定。muninで監視したいので、下の設定も追加しておく。ちなみにnodeだけmasterではない。
# vi /etc/nginx/sites-available/default  
server {
    listen   80;
    root /var/example.com;
    index index.php index.html index.htm;
    charset     utf-8;
    access_log  /var/log/nginx/example.com.access.log;
    error_log   /var/log/nginx/example.com.error.log;
    server_name example.com;

    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

server {
    listen 80;
    server_name localhost;

    location /nginx_status {
        stub_status on;
        access_log  off;
        allow 127.0.0.1;
        deny  all;
    }
}

X-Powered-Byもレスポンスヘッダに含めないようにしておく。
# vi /etc/php5/fpm/php.ini
expose_php = Off

あとはapache2を止めてnginx、php5-fpmを起動。それとサーバ再起動でapache2を起動しないようにしておく。
# service apache2 stop
# service php5-fpm restart
# service nginx start
# update-rc.d apache2 disable
以上で、切り替え完了。

munin-nodeの設定。
# vi /etc/munin/plugin-conf.d/munin-node 
[nginx*]
env.url http://localhost/nginx_status

# cd /etc/munin/plugins/
# rm apache*
# ln -s /usr/share/munin/plugins/nginx_status nginx_status
# ln -s /usr/share/munin/plugins/nginx_request nginx_request
# service munin-node restart

速くはなっているのか?それは後日見ることにして今日はおしまい。でんがなまんがなー。