【转】解决Nginx php-fpm配置有误引起的502错误
转自:https://www.centos.bz/2017/07/nginx-php-fpm-502-error/
在Ubuntu+Nginx+PHP环境下部署好以后,访问网站报错502,在后台nginx error_log里看到以下报错信息(nginx日志路径:/var/log/nginx)
2017/07/29 10:59:15 [error] 5622#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 183.14.134.39, server: xx.xx.xx.xx, request: “GET /index.php HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9001”, host: “xx.xx.xx.xx”
通常这个报错是表示php-fpm这个服务未启动,由于默认是配置的9000端口,执行netstat -anp|grep 9000确实没有看到相关进程。
但执行命令查询php-fpm是running状态
# /etc/init.d/php7.0-fpm status ● php7.0-fpm.service – The PHP 7.0 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2017-07-29 11:52:47 CST; 3h 43min ago Process: 7191 ExecStartPre=/usr/lib/php/php7.0-fpm-checkconf (code=exited, status=0/SUCCESS) Main PID: 7200 (php-fpm7.0) Status: “Processes active: 0, idle: 2, Requests: 54, slow: 0, Traffic: 0req/sec” CGroup: /system.slice/php7.0-fpm.service ├─7200 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf) ├─7203 php-fpm: pool www └─7204 php-fpm: pool www Jul 29 11:52:47 iZwz96f0gkw4blayus6g2yZ systemd[1]: Starting The PHP 7.0 FastCGI Process Manager… Jul 29 11:52:47 iZwz96f0gkw4blayus6g2yZ systemd[1]: Started The PHP 7.0 FastCGI Process Manager.
查看/etc/php/7.0/fpm/pool.d/www.conf和/etc/php/7.0/fpm/php-fpm.conf发现以下参数:
listen = /run/php/php7.0-fpm.sock
查阅资料后才知道,原来php-fpm支持网络端口监听和socket两种方式,但后者效率更高。
针对该问题的解决方案是,修改nginx/conf/vhosts下的conf文件,
将fastcgi_pass 127.0.0.1:9000;修改为fastcgi_pass unix:/run/php/php7.0-fpm.sock;
重启Nginx服务后,WEB访问依然报错502,继续定位分析。
在nginx error_log日志中出现了以下新的报错内容:
2017/07/29 11:24:47 [crit] 6114#0: *1 connect() to unix:/run/php/php7.0-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 183.14.134.xx, server: 112.74.89.xx, request: “GET / HTTP/1.1”, upstream: “fastcgi://unix:/run/php/php7.0-fpm.sock:”, host: “112.74.89.xx”
然后找到/etc/php/7.0/fpm/pool.d/www.conf文件,做以下处理:
注释掉
;listen.owner = www-data
;listen.group = www-data
将mode值修改为0666
listen.mode = 0666
最后,执行/etc/init.d/php7.0-fpm restart重启php-fpm服务,执行/etc/init.d/nginx restart重启Nginx服务,问题解决!!!