CentOS6.4 + nginx1.8.0 + PHP5.3.3の環境をVagrantを使って作る
phpとphp-fpmとnginxをインストール
yum instal -y php
yum install -y php-fpm
yum install -y nginx
php-fpmの設定&起動
php-fpmの設定ファイルを変更
9000番ポート経由というのはなんとなく嫌だったので、unixソケットに切り替え
- user = apache
- group = apache
+ user = nginx
+ group = nginx
- listen = 127.0.0.1:9000
+ listen = /var/run/php5-fpm.sock
起動
/etc/init.d/php-fpm restart
nginxの設定&起動
設定ファイルを変更
include /etc/nginx/conf.d/*.conf;
デフォルトでconf.d/以下の*.confファイルをすべて読むようになっているのでnginx.confはさわらず、default.confを修正
location / {
- root /usr/share/nginx/html;
- index index.html index.htm;
+ root /var/www/htdocs;
+ index index.php;
}
- #location ~ \.php$ {
- # root html;
- # fastcgi_pass 127.0.0.1:9000;
- # fastcgi_index index.php;
- # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- # include fastcgi_params;
- #}
+ location ~ \.php$ {
+ root /var/www/htdocs;
+ fastcgi_pass /var/run/php5-fpm.sock;
+ fastcgi_index index.php;
+ fastcgi_param $document_root /$fastcgi_script_name;
+ include fastcgi_params;
+ }
nginx起動
nginx
アクセスするPHPファイルを作成
適当に
echo "hogehoge";
テスト
ブラウザで仮想サーバーの
http://プライベートIP/index.php
にアクセスしてhogehogeが表示されればOK
遭遇したエラー&解決方法
ブラウザでアクセスしても "File Not Found"となる
ログを見る。
以下のエラーの場合
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
僕の場合はfastcgi_paramのパスが間違っていた。
このあたりを要確認
以下のエラーの場合
open() "/var/www/htdocs/index.html" failed (13: Permission denied)...(略)
アクセスしようとしている
/var/www/htdocs/index.html
に権限がないとのこと。
このファイル自体の権限は問題なかったが、
親ディレクトリの/var/wwwに権限が無くてPermissionDeniedとなっていた。
結構ありがちな問題。
chmod 755 /var/www
で解決
ブラウザでアクセスすると"An error occurred."とエラーがでる
*1 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream
php-fpmが起動していないもしくは設定が反映されていないので
php-fpmを起動(再起動)で解消
/etc/init.d/php-fpm restart
ブラウザでアクセスしても真っ白で何も表示されていない
PHPのエラーが出ていたり・・・(初期設定でエラー表示はないので画面に何も表示されない)
FastCGI sent in stderr: "PHP message: PHP Parse error: syntax error...(略)
参考
http://qiita.com/puttyo_bubu/items/5bf554ef2790f6435c32
http://kawatama.net/web/1352
http://qiita.com/utano320/items/36b6eac2bbd5bb5657f6
http://hotolab.net/blog/nginx_simple/