温馨提示×

如何调试linux lnmp

小樊
39
2026-09-06 19:38:07
栏目: 智能运维

调试 Linux 下的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 环境,通常要从分层排查入手:先看网络,再看 Nginx,再看 PHP,最后看数据库。下面给你一套实用调试思路和常用命令


一、基础检查(先确认服务是否运行)

1. 查看服务状态

systemctl status nginx
systemctl status php-fpm
systemctl status mysql

或(老版本):

service nginx status
service php-fpm status
service mysql status

2. 端口监听检查

netstat -tunlp | grep -E '80|443|3306|9000'
# 或
ss -tunlp

LNMP 常见端口:

  • Nginx:80 / 443
  • PHP-FPM:9000(或 socket)
  • MySQL:3306

二、Nginx 调试

1. 检查 Nginx 配置

nginx -t

2. 查看 Nginx 错误日志

tail -f /var/log/nginx/error.log

常见错误:

  • Permission denied → 权限或 SELinux 问题
  • File not found → root 路径或 PHP 配置错误
  • connect() failed (111: Connection refused) → PHP-FPM 没启动

3. 查看访问日志

tail -f /var/log/nginx/access.log

三、PHP 调试

1. 检查 PHP-FPM 是否运行

ps aux | grep php-fpm

2. 测试 PHP 是否解析

新建测试文件:

echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php

访问:

http://服务器IP/info.php

3. PHP 错误日志

php -i | grep error_log
tail -f /var/log/php-fpm/error.log

4. 开启错误显示(调试用)

php.ini

display_errors = On
error_reporting = E_ALL

四、Nginx + PHP-FPM 通信问题

常见配置(FastCGI)

location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    # 或
    fastcgi_pass   unix:/run/php-fpm/www.sock;
    fastcgi_index  index.php;
    include        fastcgi.conf;
}

检查:

  • PHP-FPM 监听的是端口还是 socket
  • Nginx 用户是否有权限访问 socket

五、MySQL / MariaDB 调试

1. 能否登录

mysql -u root -p

2. 查看错误日志

tail -f /var/log/mysqld.log
# 或
tail -f /var/log/mysql/error.log

3. PHP 连不上数据库

检查:

  • 主机:127.0.0.1 vs localhost
  • 用户权限
  • 防火墙

六、防火墙 & SELinux

防火墙

firewall-cmd --list-all
# 或
iptables -L

开放端口:

firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload

SELinux

getenforce
setenforce 0   # 临时关闭测试

七、常用调试套路总结

✅ 网页打不开
→ 看 Nginx error.log

✅ 空白页
→ 看 PHP 错误日志 / 开 display_errors

✅ 502 Bad Gateway
→ PHP-FPM 挂了或 socket 权限问题

✅ 数据库连接失败
→ 用户权限 / 端口 / 防火墙


如果你愿意,可以告诉我:

  • 具体报错信息
  • LNMP 安装方式(源码 / 宝塔 / LNMP.org / Docker)
  • 系统是 CentOS 还是 Ubuntu

我可以直接帮你定位具体问题

0