「Linux/nginx/php動作」の版間の差分
提供: 初心者エンジニアの簡易メモ
細 (Admin がページ「Linux/nginxインストール/php動作」を「Linux/nginx/php動作」に移動しました) |
(→書き込みエラーになる場合) |
||
| (同じ利用者による、間の10版が非表示) | |||
| 行9: | 行9: | ||
==nginx設定変更== | ==nginx設定変更== | ||
nginx.confに以下を追加 | nginx.confに以下を追加 | ||
| − | location ~ \.php$ { | + | server { |
| − | + | listen 80; | |
| − | + | server_name 192.168.99.100; | |
| − | + | root /var/www/html; | |
| − | + | index index.php index.html; | |
| − | + | location ~ \.php$ { | |
| + | root /var/www/html; | ||
| + | fastcgi_pass 127.0.0.1:9000; | ||
| + | fastcgi_index index.php; | ||
| + | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | ||
| + | include fastcgi_params; | ||
| + | } | ||
} | } | ||
| − | */ | + | */var/www/html/index.php |
<?php | <?php | ||
echo 'helloworld'; // helloworld | echo 'helloworld'; // helloworld | ||
==サーバ再起動== | ==サーバ再起動== | ||
| + | ===centos6=== | ||
$ sudo /etc/init.d/nginx restart | $ sudo /etc/init.d/nginx restart | ||
$ sudo /etc/init.d/php-fpm restart | $ sudo /etc/init.d/php-fpm restart | ||
| + | or | ||
| + | $ service nginx restart | ||
| + | $ service php-fpm restart | ||
| + | ===centos7=== | ||
| + | $ systemctl restart nginx | ||
| + | $ systemctl restart php-fpm | ||
| + | |||
| + | ==php-fpmの設定がsockの場合のnginx側設定== | ||
| + | php-fpmの設定場所 | ||
| + | /etc/php-fpm.d/www.conf | ||
| + | |||
| + | nginx側conf | ||
| + | <pre> | ||
| + | - fastcgi_pass 127.0.0.1:9000; | ||
| + | + fastcgi_pass unix:/run/php-fpm/www.sock; | ||
| + | </pre> | ||
| + | |||
| + | ==ファイル書き込みエラーになる場合== | ||
| + | selinuxが聞いてる可能性がある。 | ||
| + | |||
| + | 確認方法 | ||
| + | <pre> | ||
| + | $ getenforce | ||
| + | Enforcing | ||
| + | </pre> | ||
| + | |||
| + | 解除 | ||
| + | <pre> | ||
| + | sudo setenforce 0 | ||
| + | </pre> | ||
2026年4月7日 (火) 10:53時点における最新版
目次
phpを動作させるには
php-fpmと連携する方法と、apacheからphpを実行する方法があります。
ここではphp-fpmを連携する方法を紹介
php-fpmインストール
linux/phpインストール/php-fpm [ショートカット]
nginx設定変更
nginx.confに以下を追加
server {
listen 80;
server_name 192.168.99.100;
root /var/www/html;
index index.php index.html;
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
- /var/www/html/index.php
<?php echo 'helloworld'; // helloworld
サーバ再起動
centos6
$ sudo /etc/init.d/nginx restart $ sudo /etc/init.d/php-fpm restart
or
$ service nginx restart $ service php-fpm restart
centos7
$ systemctl restart nginx $ systemctl restart php-fpm
php-fpmの設定がsockの場合のnginx側設定
php-fpmの設定場所 /etc/php-fpm.d/www.conf
nginx側conf
- fastcgi_pass 127.0.0.1:9000; + fastcgi_pass unix:/run/php-fpm/www.sock;
ファイル書き込みエラーになる場合
selinuxが聞いてる可能性がある。
確認方法
$ getenforce Enforcing
解除
sudo setenforce 0
