1. 程式人生 > >nginx+php測試時顯示 502 bad gateway的解決方法

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

修改後的許可權

[[email protected]  nginx]# chmod 777 /tmp/php-fcgi.sock

[[email protected] 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;