nginx+php测试时显示 502 bad gateway的解决方法

http://www.apelearn.com/study_v2/chapter18.html

由于阿铭老师的PHP版本是 5.3的   我装了 5.5   

测试出现了 502  错误 

查看日志   借助nginx的错误日志来进行排查vim /usr/local/nginx/logs/nginx_error.log 

显示 : connect() to unix:/tmp/php-fcgi.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /2.php HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-fcgi.sock:", host: "localhost"

解决方法一:

# ll /tmp

srw-rw---- 1 root  root     0 Feb 22 00:58 php-fcgi.sock

修改后的权限

[root@centos  nginx]# chmod 777 /tmp/php-fcgi.sock

[root@centos nginx]# ll /tmp

srwxrwxrwx 1 root  root     0 Feb 22 00:58 php-fcgi.sock

修改权限后测试成功

重启  service php-fpm restart  

       service nginx restart 

# ll /tmp

srw-rw---- 1 root  root     0 Feb 22 00:58 php-fcgi.sock

权限又恢复了原样

后修改  vim /etc/init.d/php-fpm    在start)  的fi  后加上 chmod 777 /tmp/php-fcgi.sock

解决方法二:   

配置错误 因为 nginx 找不到php-fpm了,所以报错,一般是fastcgi_pass后面的路径配置错误了,后面可以是socket或者是ip:port

修改php-fpm的配置文件  vim /usr/local/php/etc/php-fpm.conf   里面的 listen = /tmp/php-fcgi.sock  改为  listen = 127.0.0.1:9000

修改nginx的配置文件   vim /usr/local/nginx/conf/nginx.conf     里面的 fastcgi_pass unix:/tmp/php-fcgi.sock; 改为 fastcgi_pass 127.0.0.1:9000;

原文地址:https://www.cnblogs.com/carbon3/p/5344513.html