LoginSignup
26
25

More than 5 years have passed since last update.

nginxとphp-fpmでhtmlファイルのphpを動かす

Last updated at Posted at 2015-10-27

nginxのサイトを作るたびに.phpだと動くけど、.htmlに変えると真っ白とか
*.phpでも実行されずにダウンロードされたりするので忘れすぎ。
忘れないようにメモ

環境

CentOS 6.8 final
nginx 1.10.1
PHP 5.6.22

nginxはmainline版をyumインストール
PHPはepel,remiリポジトリを追加してremiの最新版をyumインストール

php-fpmの設定ファイル変更点

/etc/php-fpm.d/www.conf

user  = nginx
group = nginx

listen = /var/run/php-fpm/php-fpm.sock
listen.mode = 660

security.limit_extensions = .php .html

nginxの設定ファイル変更点

/etc/nginx/conf.d/server.conf

location /{
    root /usr/share/nginx/html;
    index index.php index.html;
}

location ~\.(php|html)${
root /usr/share/nginx/html;
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    include fastcgi_params;
}

他にやること

まだ動かないとき、ログを見るとphp-fpmがparmission deniedとなってるので
#sudo chmod 666 /var/run/php-fpm/php-fpm.sock

これで .phpファイルも、.htmlファイル内で<?php ~~ ?>で囲った部分も実行される。
セキュリティのためらしいので666にするなら注意

iptablesが動いてるとダメなこともあるので止めること

#sudo /etc/init.d/iptables stop
#sudo /etc/init.d/ip6tables stop
#sudo chkconfig iptables off
#sudo chkconfig ip6tables off
26
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
25