• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

Centos7安装整合Apache+PHP,安装nginx后nginx无法解析.php文件

互联网 diligentman 2小时前 2次浏览

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配置,所以写了这篇文章,防止自己以后忘记;

喜欢 (0)