在Ubuntu上遇到PHP-FPM的502 Bad Gateway错误,通常是由于Nginx与PHP-FPM之间的通信问题引起的。以下是一些常见的解决方法:
确保PHP-FPM服务正在运行:
sudo systemctl status php7.4-fpm
如果服务未运行,启动它:
sudo systemctl start php7.4-fpm
确保Nginx配置文件中正确设置了PHP-FPM的监听地址和端口。编辑Nginx配置文件(通常位于/etc/nginx/sites-available/your-site):
server {
listen 80;
server_name your-domain.com;
root /var/www/your-site;
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/php7.4-fpm.sock; # 或者使用127.0.0.1:9000
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
确保PHP-FPM配置文件(通常位于/etc/php/7.4/fpm/pool.d/www.conf)中的listen参数设置正确:
listen = /run/php/php7.4-fpm.sock
或者如果你使用TCP/IP连接:
listen = 127.0.0.1:9000
确保Nginx用户(通常是www-data)有权限访问PHP-FPM的socket文件或TCP端口。例如,如果你使用socket文件:
sudo chown www-data:www-data /run/php/php7.4-fpm.sock
重启Nginx和PHP-FPM服务以应用更改:
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm
查看Nginx和PHP-FPM的错误日志以获取更多信息:
/var/log/nginx/error.log/var/log/php7.4-fpm.log如果你的系统启用了SELinux或AppArmor,确保它们没有阻止Nginx与PHP-FPM之间的通信。
确保防火墙允许HTTP(80)和HTTPS(443)流量:
sudo ufw allow 'Nginx Full'
通过以上步骤,你应该能够解决大多数Ubuntu上PHP-FPM的502 Bad Gateway错误。如果问题仍然存在,请提供更多的错误日志信息以便进一步诊断。