排查 LNMP 环境(Linux + Nginx + MySQL/MariaDB + PHP) 问题,一般按 “分层定位” 的思路来,从系统 → Nginx → PHP → 数据库,逐层缩小范围。下面给你一套 实用排查清单,适合生产环境也适合本地开发。
systemctl status nginx
systemctl status php-fpm
systemctl status mysqld
# 或
systemctl status mariadb
如果某个服务没运行:
systemctl start xxx
journalctl -xe -u xxx
nginx -t
常见错误:
;fastcgi_pass 路径错误root 目录不存在tail -f /var/log/nginx/error.log
常见错误:
connect() failed (111: Connection refused) → PHP-FPM 没启动404 Not Found → root 或 index 配置错误403 Forbidden → 权限问题curl http://localhost/test.html
✅ 正常 → Nginx 没问题
❌ 不正常 → 看 Nginx 配置或权限
ps aux | grep php-fpm
查看监听方式:
grep listen /etc/php-fpm.d/www.conf
常见两种:
listen = 127.0.0.1:9000
listen = /run/php-fpm/www.sock
Nginx 中必须对应:
fastcgi_pass 127.0.0.1:9000;
# 或
fastcgi_pass unix:/run/php-fpm/www.sock;
tail -f /var/log/php-fpm/error.log
常见错误:
permission denied(sock 权限)max children reached创建测试文件:
echo "<?php phpinfo();" > /usr/share/nginx/html/info.php
访问:
http://服务器IP/info.php
✅ 能看到 PHP 信息 → PHP 正常
❌ 下载 / 空白 / 502 → PHP-FPM 或 Nginx 配置问题
ps aux | grep nginx
ps aux | grep php-fpm
常见组合:
nginx → nginx
php-fpm → apache / www / nginx
不一致时修改:
; /etc/php-fpm.d/www.conf
user = nginx
group = nginx
并重启:
systemctl restart php-fpm
ls -ld /网站目录
ls -l /网站目录
建议:
chown -R nginx:nginx /网站目录
chmod -R 755 /网站目录
mysql -u root -p
如果连不上:
systemctl status mysqld
journalctl -xe -u mysqld
测试脚本:
<?php
$conn = new mysqli("127.0.0.1", "user", "password", "db");
if ($conn->connect_error) {
die("DB error: " . $conn->connect_error);
}
echo "DB OK";
常见问题:
127.0.0.1php-mysqlfirewall-cmd --list-all
# 或
iptables -L
测试可临时关闭:
systemctl stop firewalld
getenforce
如果是 Enforcing:
setenforce 0
或给 Nginx 授权:
setsebool -P httpd_read_user_content 1
| 现象 | 大概率原因 |
|---|---|
| 502 | PHP-FPM 没启动 / sock 权限 |
| 504 | PHP 执行超时 |
| 空白页 | PHP 报错但没显示 |
| 下载 PHP | Nginx 没解析 PHP |
服务是否启动
→ nginx -t
→ Nginx error.log
→ PHP-FPM 是否监听
→ PHP-FPM log
→ 权限
→ 防火墙 / SELinux
如果你愿意,可以直接把 具体报错信息、系统版本、LNMP 版本 发我,我可以帮你 精准定位某一条错误。