高速な HTTP サーバーである Nginx を使って cakePHP を利用する場合の設定をまとめました。注意点としてはリライトの扱いとファイルパーミッションです:


前提として cakePHP は(Apache HTTPd で使う前提の内容でいいので)設定ができているものとします。cakePHP の導入先は /var/www/html/cakephp/ であると仮定します。Apache HTTPd は導入しないか、サービスを止めておきます。cakePHP の導入手順についてはこちらを参照ください:
cakePHP を CentOS にインストールする


また PHP-FPM や Nginx の導入自体はできているものとします。これらの手順はこちらを参照ください:
CentOS に Nginx をインストールして PHP を使う


では Nginx を cakePHP の環境に合わせて設定していきます。まずは /etc/nginx/conf.d/default.conf を以下の内容に変更します(特に赤字部分は各自の環境に合わせて編集するよう、注意してください):
server {
    listen       80;
    server_name  localhost;
 
    root   /var/www/html/cakephp/app/webroot;
    index  index.php index.html;
 
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
 
    location / {
        try_files $uri $uri?$args $uri/ /index.php?$uri&$args /index.php?$args;
    }
 
    #error_page  404              /404.html;
 
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
 
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        try_files       $uri =404;
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }
 
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

※追記1
index 節に index.html を追加。webroot/sub/index.html に /sub/ でアクセスできるようにするため

※追記2
try_files 節に /index.php?$args を追加。/controller/view?key=value を正しくハンドルできるようにするため



次に cakePHP 内の app/tmp フォルダに(Apache HTTPd ではなく) nginx が書き込めるような権限を与えます:
# chown -R nginx.nginx /var/www/html/cakephp/app/tmp
# chmod -R 700 /var/www/html/cakephp/app/tmp

また、/etc/php.ini に以下の内容を加えて、セッションデータを保存するディレクトリを設定しておきます:
session.save_path = "/var/lib/php/session"

これで Nginx を起動して、ブラウザで http://(サーバー名)/ にアクセスすると Nginx 環境下で動いている cakePHP のホーム画面が表示されるはずです:
# /etc/init.d/nginx start (Nginx サービス起動)
# chkconfig nginx on (自動起動設定)

2014051701




心なしか速くなった・・・のかな?