我将nginx更新到1.4.7,将php更新到5.5.12,之后我得到了502错误。在我更新之前,一切都很好。
nginx-error.log
2014/05/03 13:27:41 [crit] 4202#0: *1 connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream, client: xx.xxx.xx.xx, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "xx.xx.xx.xx"
nginx.conf
user www www;
worker_processes 1;
location / {
root /usr/home/user/public_html;
index index.php index.html index.htm;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/home/user/public_html$fastcgi_script_name;
include fastcgi_params;
}
如果你已经尝试了这篇文章中的所有内容,但都没有成功地让PHP工作,这是为我的案例修复的:
确保在/etc/php5/fpm/pool.d/www.conf中没有注释这些行:
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
确保/etc/nginx/fastcgi_params看起来像这样:
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
这两行在我的/etc/nginx/fastcgi_params中丢失了,请确保它们在那里!
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
然后重新启动php5-fpm和nginx。应该能行。
从Ubuntu 14.04 lts升级到Ubuntu 16.04 lts后,我发现了另一个我以前没有见过的错误原因。
在升级过程中,我不知何故丢失了php5-fpm可执行文件。所有的配置文件都是完整的,我花了一段时间才意识到service php5-fpm start并没有真正启动一个进程,因为它没有显示任何错误。
当我注意到/var/run/php5-fpm中没有套接字文件时,我幡然醒悟在尝试解决这个问题时,netstat -an也没有显示侦听端口的进程。由于/usr/sbin/php5-fpm文件也不存在,我终于找到了正确的路径。
为了解决这个问题,我将php从5.5版本升级到7.0版本。Apt-get install php-fpm作为一个副作用做到了。在安装了其他必要的软件包之后,一切恢复正常。
然而,这种升级解决方案可能有其自身的问题。由于php已经发展了很多,软件可能会以难以想象的方式崩溃。所以,即使我真的走了那条路,你可能想要保留你喜欢的版本再久一点。
幸运的是,似乎有一个简单的方法,如自定义Windows网站所述:
add-apt-repository ppa:ondrej/php
apt-get purge php5-common
apt-get update
apt-get install php5.6
这可能是一个更简洁的解决方案,但我没有尝试。我希望接下来的几天会告诉我是否应该这样做。
下面的简单修复对我有用,绕过了套接字可能的权限问题。
在你的nginx配置中,设置fastcgi_pass为:
fastcgi_pass 127.0.0.1:9000;
而不是
fastcgi_pass /var/run/php5-fpm.sock;
这必须匹配/etc/php5/fpm/pool.d/www.conf中的listen =参数,所以也将其设置为:
listen = 127.0.0.1:9000;
然后重新启动php5-fpm和nginx
service php5-fpm restart
And
service nginx restart
更多信息,请参见:https://wildlyinaccurate.com/solving-502-bad-gateway-with-nginx-php-fpm/
这里最重要的事情是,如果用户正在使用nginx,那么你也需要指定它
在nginx.conf中
user www-data;
worker_processes 1;
location / {
root /usr/home/user/public_html;
index index.php index.html index.htm;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/home/user/public_html$fastcgi_script_name;
include fastcgi_params;
}
在你的www.conf
listen.owner = www-data
listen.group = www-data
;listen.mode = 0660
在你的例子中,用户和组是“www”,所以只需替换它。
重启nginx和PHP FPM