Debian 下 LNMP 问题排查与修复指南
一、系统化排查流程
二、常见症状与快速修复
| 症状 | 快速检查 | 修复要点 |
|---|---|---|
| 502 Bad Gateway | Nginx error.log 出现 “connect() to unix:/run/php/… failed (2: No such file or directory)” | 确认 PHP-FPM 正在运行;核对 /etc/php/<版本>/fpm/pool.d/www.conf 中 listen 与 Nginx 中 fastcgi_pass 一致;常见路径为 /run/php/php<版本>-fpm.sock;执行 systemctl restart php<版本>-fpm 并 nginx -t && systemctl reload nginx |
| 504 Gateway Timeout | 页面长时间等待后超时 | 调整 PHP-FPM 进程管理:增大 pm.max_children,必要时设置 pm.max_requests 回收进程;适当提升 listen.backlog(如 1024);同时优化慢查询与后端逻辑 |
| 403 Forbidden | Nginx error.log 报 “permission denied” | 核对站点根目录与 index 指令;确保 /var/www 及子目录属主为 www-data:www-data;在 location ~ .php$ 中保留 include snippets/fastcgi-php.conf; 并使用正确的 fastcgi_pass |
| 数据库连接失败 | 应用报 “Access denied” 或 “Can’t connect to MySQL” | 确认 MariaDB/MySQL 运行(systemctl status mysql);检查 /var/log/mysql/error.log;运行 mysql_secure_installation 加固并设置密码;如远程访问,确认 bind-address 与防火墙策略 |
| Nginx 启动失败 | 执行 nginx -t 报语法错误或端口占用 | 依据 nginx -t 提示修正配置;若端口被占用,释放端口或调整 listen;必要时 systemctl restart nginx |
| PHP-FPM 启动失败 | 状态报错或 socket 未创建 | 检查 /etc/php/<版本>/fpm/pool.d/www.conf 的 listen.owner/listen.group 与 www-data 一致;确认 /run/php/ 目录存在且可写;重启服务并复核日志 |
| APT 源不可用或速度慢 | apt update 失败或超时 | 替换为可用镜像源(如国内镜像),更新索引后再安装/升级 |
| 以上为高频故障的“现象—定位—修复”对照,覆盖 502/504、403、数据库连接、服务启动 等核心场景。 |
三、关键配置与验证
四、维护与加固建议