调试 Linux 下的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 环境,通常要分层次进行:网络 → Nginx → PHP → 数据库 → 日志 → 权限。下面给你一套系统的排查思路和常用命令。
systemctl status nginx
systemctl status php-fpm
systemctl status mysql
# 或 mariadb
systemctl status mariadb
如果没启动:
systemctl start nginx
systemctl start php-fpm
systemctl start mysql
设置开机启动:
systemctl enable nginx php-fpm mysql
nginx -t
有错误会直接提示哪一行。
tail -f /var/log/nginx/error.log
常见错误:
permission denied → 权限问题File not found → root 路径或 PHP 配置错误connect() failed (111: Connection refused) → PHP-FPM 没起curl http://localhost
netstat -lntp | grep php-fpm
# 或
ss -lntp | grep php-fpm
默认监听:
127.0.0.1:9000
Nginx 配置示例(关键):
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
php -v
php -m
写个测试文件:
<?php
phpinfo();
tail -f /var/log/php-fpm/error.log
# PHP 自身错误
tail -f /var/log/php_errors.log
(php.ini 中开启)
display_errors = On
error_log = /var/log/php_errors.log
mysql -u root -p
tail -f /var/log/mysqld.log
# 或
tail -f /var/log/mysql/error.log
<?php
$conn = new mysqli("127.0.0.1", "user", "pass", "db");
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
echo "OK";
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
(用户可能是 nginx 或 www-data)
getenforce
临时关闭:
setenforce 0
iptables -L
# 或
firewall-cmd --list-all
开放端口:
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
nginx -tsystemctl statuscurl localhost你可以告诉我:
我可以直接帮你定位到具体配置文件和修复命令。