Nginx+php踩坑
- 問題
- 環境介紹
- Nginx配置決議php
- 關鍵一:
- 配置修改php-fpm(權限)
- 關鍵二:
- 重啟nginx,確保php-fpm啟動
- 鏈接參考:☆
- 總結
問題
nginx無法決議.php檔案,頁面空白,并且替換為html靜態檔案后可以決議;
日志報錯:nginx FastCGI sent in stderr: “Primary script unknown”
環境介紹
- Linux服務器:Centos7;
- 提前yum安裝有apache + php-fpm (關鍵,php-fpm需要指定用戶和組)
- yum 安裝nginx
Nginx配置決議php
yum安裝的nginx,組態檔在/etc/nginx/目錄下
[root@centos7php nginx]# pwd
/etc/nginx
[root@centos7php nginx]# ll
total 60
drwxr-xr-x 2 root root 6 Sep 15 2017 conf.d
-rw-r--r-- 1 root root 1077 Sep 15 2017 fastcgi.conf
-rw-r--r-- 1 root root 1077 Sep 15 2017 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 Sep 15 2017 fastcgi_params
-rw-r--r-- 1 root root 1007 Sep 15 2017 fastcgi_params.default
-rw-r--r-- 1 root root 2837 Sep 15 2017 koi-utf
-rw-r--r-- 1 root root 2223 Sep 15 2017 koi-win
-rw-r--r-- 1 root root 3957 Sep 15 2017 mime.types
-rw-r--r-- 1 root root 3957 Sep 15 2017 mime.types.default
-rw-r--r-- 1 root root 3509 Sep 12 12:29 nginx.conf
-rw-r--r-- 1 root root 2656 Sep 15 2017 nginx.conf.default
-rw-r--r-- 1 root root 636 Sep 15 2017 scgi_params
-rw-r--r-- 1 root root 636 Sep 15 2017 scgi_params.default
-rw-r--r-- 1 root root 664 Sep 15 2017 uwsgi_params
-rw-r--r-- 1 root root 664 Sep 15 2017 uwsgi_params.default
-rw-r--r-- 1 root root 3610 Sep 15 2017 win-utf
[root@centos7php nginx]# vim nginx.conf
修改nginx.conf組態檔,添加對.php檔案的決議
關鍵一:
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log main;
include /etc/nginx/conf.d/*.conf;
index index.html index.htm;
server {
listen 80;
server_name localhost;
# 服務根目錄(代碼存放處),開發環境需要設定為nginx權限,nginx組
# 當前目錄/usr/share/nginx/html,為root權限,root組,參考關鍵一,更改
root /usr/share/nginx/html;
location / {
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root html;
fastcgi_intercept_errors on;
# 指定 php-fpm
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# 關鍵一:如果你默認不更改root目錄地址,就需要將/scripts改為$document_root
#默認:fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
配置修改php-fpm(權限)
yum安裝的php-fpm組態檔位置:/etc目錄下
[root@centos7php etc]# pwd
/etc
[root@centos7php etc]# ll | grep php
drwxr-xr-x 2 root root 4096 Sep 7 18:27 php.d
-rw-r--r-- 1 root root 4207 Dec 7 2018 php-fpm.conf
drwxr-xr-x 2 root root 22 Sep 12 12:19 php-fpm.d
-rw-r--r-- 1 root root 62641 Dec 7 2018 php.ini
drwxr-xr-x 2 root root 4096 Sep 7 18:27 php-zts.d
關鍵二:
總的默認配置為:php-fpm.conf檔案,因為我們需要修改的是權限,如果主組態檔中沒有,就去找組態檔目錄/etc/php-fpm.d/
[root@centos7php etc]# cd php-fpm.d/
[root@centos7php php-fpm.d]# ll
total 20
-rw-r--r-- 1 root root 18027 Sep 12 12:19 www.conf
[root@centos7php php-fpm.d]# vim www.conf
[www]
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
; 關鍵二:因為之前部署環境的時候是采用的,Apache + php,所以此處的user和group才會是Apache的(yum自動配置的)
; 現在我們環境更改為,nginx + php,所以用戶和組改為nginx
;user = apache
user = nginx
; RPM: Keep a group allowed to write in log dir.
;group = apache
group = nginx
listen = 127.0.0.1:9000
重啟nginx,確保php-fpm啟動
[root@centos7php php-fpm.d]# systemctl restart nginx.service
[root@centos7php php-fpm.d]# netstat -anp | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 61037/php-fpm: mast
[root@centos7php php-fpm.d]# systemctl restart php-fpm
如果nginx啟動失敗,報錯:
Job for nginx.service failed because the control process exited with error code. See “systemctl status nginx.service” and “journalctl -xe” for details.
則,執行命令:nginx -t 查看組態檔報錯位置內容,修改并重啟
[root@centos7php nginx]# nginx -t
nginx: [emerg] invalid number of arguments in "fastcgi_pass" directive in /etc/nginx/nginx.conf:86
nginx: configuration file /etc/nginx/nginx.conf test failed
[root@centos7php nginx]# vim nginx.conf
[root@centos7php nginx]# systemctl restart nginx
[root@centos7php nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
鏈接參考:☆
- 解決nginx FastCGI sent in stderr: “Primary script unknown”
總結
- 自己還是不愿意去看日志,來找問題,浪費了時間和精力,
- 如果下次還繼續配置nginx或者Apache,環境一定要分開,避免因為環境問題導致自己卡主很長時間;
- 大佬的文章其實已經說明白了,因為不熟悉nginx和php-fpm配置,所以寫了這篇文章,防止自己以后忘記;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/299641.html
標籤:其他
