nginx + PHP-FPM 環境でCakePHPの利用設定
nginxとPHP-FPMを連携させた環境でCakePHPを利用する設定。
なお、nginxやPHP-FPMの導入についてはそれぞれ以下の記事を参照。
今回は「CakePHP 2.3」を想定し作業を進める。
(1) CakePHPのファイルの配置
公式サイトから取得したファイル(cakephp-2.3.10.zip)を展開しサーバにアップ。 ここではnginx導入時のデフォルトドキュメントルートの一階層上である「/usr/share/nginx」にアップし作業を進める。
(2) ドキュメントルートの変更
nginxのドキュメントルートをCakePHPのアプリケーションフォルダに変更する。 ドキュメントルートの変更は「/etc/nginx/conf.d/default.conf」を修正し行う。
server { listen 80; server_name localhost; root /usr/share/nginx/cakephp/app/webroot; index index.php; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { try_files $uri $uri/ /index.php?$uri&$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; #} }
修正内容のソースは公式サイトの以下の内容を参照。
主な修正個所は背景色を変更した箇所。
5行目及び6行目は新しいドキュメントルートの指定とそのデフォルトドキュメントの設定。
12行目はドキュメントルート表示時の動作の設定。
ここでは指定されたファイルが存在する場合はそのファイルを表示し、存在しない場合はリダイレクトするURIのパスを返す設定。
32行目から38行目はPHPの動作に関する設定。
33行目は対象のファイルが存在しない場合に404エラーを返す設定。
(3) 所有権の変更と書込み権限の付与
「tmp」フォルダの所有権をnginx及びPHP-FPMの実行権限に合わせる。 具体的には何れも「nginx」ユーザ及びグループに変更。
chown -R nginx /usr/share/nginx/cakephp/app/tmp
chgrp -R nginx /usr/share/nginx/cakephp/app/tmp
同時に書込み権限も調整する。
chmod -R 700 /usr/share/nginx/cakephp/app/tmp
(4) 動作確認
いつものCakePHPの画面が表示されればOK。
何かエラーが出ている場合は以下の記事も参考に解消する。