下面我按最常见的 Debian + LNMP(Linux + Nginx + MySQL/MariaDB + PHP)故障场景给你一个系统化的排错思路,不是只讲某一条命令,而是「先定位再处理」。
LNMP 任一层出错,现象可能一样(网页打不开 / 502 / 空白页)。
systemctl status nginx
nginx -t
✅ 正常应显示 active (running) 和 test is successful
常见错误:
看错误日志:
tail -n 50 /var/log/nginx/error.log
systemctl status php8.2-fpm # 版本按实际
或
ps aux | grep php-fpm
测试 PHP:
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
浏览器访问 http://IP/info.php
❌ 如果:
.php 交给 PHP检查 Nginx 配置中:
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
路径必须存在:
ls /run/php/
systemctl status mariadb
mysql -u root -p
常见错误:
localhost 但实际是 socketPHP 连接建议:
new PDO("mysql:host=127.0.0.1;dbname=xxx", "user", "pass");
日志:
tail -n 50 /var/log/mysql/error.log
原因 90% 是:
排查:
journalctl -u php8.2-fpm
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
PHP 报错被关了:
ini_set('display_errors', 1);
error_reporting(E_ALL);
或看:
tail -f /var/log/php8.2-fpm.log
ufw status
systemctl enable nginx php8.2-fpm mariadb
nginx -tsystemctl status nginx php-fpm mariadb/var/log/nginx/error.log/var/log/php*-fpm.log/var/log/mysql/error.loginfo.php 隔离问题你可以直接贴:
nginx -t 输出error.log 最后 20 行我可以一步一步告诉你改哪里、改什么。