检查Nginx或Apache配置中是否将PHP请求转发给php-fpm,并确认fastcgi_pass指向的Unix socket路径与php-fpm.conf中的listen一致。

排查fastcgi_pass路径匹配
一键安装包通常默认使用Unix Socket通信。若Web服务器配置的转发地址与PHP-FPM监听的Socket文件路径不一致,会导致502 Bad Gateway错误。需核对两处配置文件的对应关系。

在Nginx配置块中查找location ~ \.php$段,查看fastcgi_pass指令。常见的一键包如宝塔、LNMP,其Socket路径可能位于/tmp/php-cgi.sock或/var/run/php/php-fpm.sock。打开PHP-FPM配置文件(通常在/etc/php/*/fpm/pool.d/www.conf),检查listen参数。若两者路径不同,修改Nginx配置使其指向正确的Socket文件,然后重启Nginx和PHP-FPM服务即可恢复访问。
验证index.php解析与权限设置
若路径正确但仍无法访问,可能是Web服务器未识别PHP文件类型,或文件权限不足导致解析失败。需确保index.php存在于根目录且拥有正确的读写执行权限。

<?php
// 检查 fastcgi_pass 与 php-fpm listen 是否匹配
$nginxConf = '/etc/nginx/conf.d/default.conf';
$fpmConf = '/etc/php/8.1/fpm/pool.d/www.conf'; // 假设版本为8.1
if (!file_exists($nginxConf) || !file_exists($fpmConf)) {
echo "Config files not found.\n";
exit(1);
}
$nginxContent = file_get_contents($nginxConf);
$fpmContent = file_get_contents($fpmConf);
preg_match('/fastcgi_pass\s+([^;]+);/', $nginxContent, $nginxMatch);
preg_match('/listen\s*=\s*([^;]+);/', $fpmContent, $fpmMatch);
if (isset($nginxMatch[1]) && isset($fpmMatch[1])) {
$nginxPath = trim($nginxMatch[1]);
$fpmPath = trim($fpmMatch[1]);
if ($nginxPath === $fpmPath) {
echo "Match: {$nginxPath}\n";
} else {
echo "Mismatch: Nginx={$nginxPath}, FPM={$fpmPath}\n";
}
} else {
echo "Could not extract paths.\n";
}
// 检查 index.php 权限
$siteRoot = '/var/www/html';
$indexPath = $siteRoot . '/index.php';
if (file_exists($indexPath)) {
$perms = substr(sprintf('%o', fileperms($indexPath)), -4);
echo "Index permissions: {$perms}\n";
} else {
echo "index.php not found in {$siteRoot}\n";
}
程序输出显示Socket路径匹配状态及文件权限检查结果。若提示不匹配,请手动修正Nginx配置中的fastcgi_pass值。若权限错误,使用chown命令调整属主。成功标志为无报错且能正常加载页面。
检查网站根目录下是否存在index.php文件。使用ls -l /path/to/site查看权限,建议所有者为运行Web服务的用户(如www-data或nobody)。若权限为755但属主错误,可能导致PHP解释器无法读取文件。同时确认Nginx的index指令包含index.php。
处理open_basedir限制导致的白屏
部分一键包启用open_basedir安全限制,若PHP代码尝试访问受限目录外的文件,会直接返回空白页或500错误而不记录详细日志。需调整该限制范围以允许必要的文件读取。
当出现白屏时,检查PHP错误日志。若发现open_basedir相关警告,说明访问路径超出允许范围。在php.ini或站点专用的.user.ini文件中,修改open_basedir的值,添加网站根目录及必要的系统临时目录(如/tmp)。保存后重启PHP-FPM使配置生效。此设置可防止跨站文件包含攻击,但需确保路径准确无误。