温馨提示×

debian lnmp如何调试

小樊
42
2026-07-30 08:16:30
栏目: 智能运维

下面按常见场景给你一套在 Debian + LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 环境下的调试思路与常用命令,从“我该先看哪里”到“逐项排查”。


一、先明确:你是在调试哪一层?

LNMP 常见出问题的地方:

层级 常见现象
Nginx 502 / 404 / 访问无响应
PHP 页面空白、500、PHP 不执行
MySQL 连不上数据库
系统 权限、端口、防火墙

二、Nginx 调试(最常见)

1️⃣ 看 Nginx 是否运行

systemctl status nginx

启动 / 重启:

systemctl start nginx
systemctl restart nginx

2️⃣ 看 Nginx 错误日志(非常重要)

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

常见错误:

  • connect() failed (111: Connection refused) → PHP-FPM 没启动
  • permission denied → 权限问题
  • no live upstreams → 后端挂了

3️⃣ 看访问日志

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

状态码含义:

  • 404:路径不对
  • 500:PHP 出错
  • 502:PHP-FPM 没响应
  • 403:权限不足

4️⃣ 检查 Nginx 配置

nginx -t

三、PHP / PHP-FPM 调试

1️⃣ PHP-FPM 是否运行

systemctl status php-fpm
# 或(Debian 常见)
systemctl status php8.2-fpm

启动:

systemctl start php8.2-fpm

2️⃣ 看 PHP-FPM 日志

tail -f /var/log/php8.2-fpm.log

或:

journalctl -u php8.2-fpm -f

3️⃣ 确认 Nginx 和 PHP-FPM 通信方式

常见两种:

✅ Unix Socket(推荐)

fastcgi_pass unix:/run/php/php8.2-fpm.sock;

检查 socket 是否存在:

ls -l /run/php/

权限问题常见:

chown www-data:www-data /run/php/php8.2-fpm.sock

✅ TCP 端口

fastcgi_pass 127.0.0.1:9000;

4️⃣ PHP 本身是否执行

创建测试文件:

echo "<?php phpinfo();" > /var/www/html/info.php

访问:

http://服务器IP/info.php
  • 能看到页面 ✅ PHP 正常
  • 下载 / 空白 ❌ PHP 没解析

5️⃣ 打开 PHP 错误显示(调试用)

编辑:

vim /etc/php/8.2/fpm/php.ini

设置:

display_errors = On
error_reporting = E_ALL

重启:

systemctl restart php8.2-fpm

四、MySQL / MariaDB 调试

1️⃣ 数据库是否运行

systemctl status mariadb
# 或
systemctl status mysql

2️⃣ 登录测试

mysql -u root -p

3️⃣ 查看错误日志

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

4️⃣ PHP 连不上数据库?

检查:

  • 用户名 / 密码
  • 是否允许远程(localhost vs %)
  • 端口(3306)

测试:

telnet 127.0.0.1 3306

五、文件权限 & 用户问题(非常常见)

Debian 默认 Web 用户是:

www-data

检查网站目录权限:

chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html

六、端口 & 防火墙

1️⃣ 查看监听端口

ss -lntp

确认:

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

2️⃣ 防火墙

ufw status

放行 Web:

ufw allow 80
ufw allow 443

七、快速定位 502 报错(最常见)

90% 是 PHP-FPM 问题

排查顺序:

  1. PHP-FPM 是否启动
  2. Nginx 的 fastcgi_pass 是否正确
  3. socket 是否存在
  4. socket 权限是否正确
  5. PHP-FPM 日志

八、推荐调试流程(实战)

# 1. 看 Nginx 状态
systemctl status nginx

# 2. 看错误日志
tail -f /var/log/nginx/error.log

# 3. 看 PHP-FPM
systemctl status php8.2-fpm

# 4. 测试 PHP
curl http://localhost/info.php

九、如果你愿意,我可以更精确帮你

你可以直接贴出:

  • 报错截图或日志
  • nginx -t 输出
  • 502 / 500 / 404 具体现象
  • Debian 版本 + PHP 版本

我可以一步一步帮你定位 ✅

0