在 CentOS 上使用 PHP-FPM 时遇到 502 错误,通常是由于 Nginx 或 Apache 与 PHP-FPM 之间的通信问题引起的。以下是一些常见的排查步骤和解决方法:
确保 PHP-FPM 服务正在运行:
sudo systemctl status php-fpm
如果服务未运行,启动它:
sudo systemctl start php-fpm
确保 Nginx 或 Apache 的配置文件中正确设置了 PHP-FPM 的监听地址和端口。
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php-fpm/www.sock; # 或者使用 tcp:127.0.0.1:9000
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:fcgi://localhost:9000"
</FilesMatch>
</VirtualHost>
确保 PHP-FPM 正在监听正确的地址和端口。编辑 PHP-FPM 配置文件(通常是 /etc/php-fpm.d/www.conf 或 /etc/php-fpm.conf),检查 listen 参数:
listen = /run/php-fpm/www.sock
# 或者
listen = 127.0.0.1:9000
如果 SELinux 启用,可能会阻止 Nginx 或 Apache 与 PHP-FPM 通信。可以临时禁用 SELinux 进行测试:
sudo setenforce 0
如果这解决了问题,可以考虑调整 SELinux 策略或永久禁用 SELinux。
查看 Nginx 和 PHP-FPM 的日志文件,获取更多错误信息。
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/php-fpm/error.log
在修改配置文件后,重启相关服务以应用更改:
sudo systemctl restart nginx
sudo systemctl restart php-fpm
通过以上步骤,应该能够解决大多数 PHP-FPM 在 CentOS 上出现的 502 错误。如果问题仍然存在,请提供更多的日志信息以便进一步诊断。