CentOS 上 LNMP 启动失败的快速排查与修复
一、快速定位问题
- 查看各组件状态与最新日志,优先看报错关键词与时间点:
- Nginx:systemctl status nginx;日志:/var/log/nginx/error.log
- MariaDB/MySQL:systemctl status mariadb 或 mysqld;日志:/var/log/mysql/error.log
- PHP-FPM:systemctl status php-fpm;日志:/var/log/php-fpm/error.log
- 检查配置语法:
- Nginx:nginx -t
- PHP-FPM:php-fpm -t
- MariaDB/MySQL:mysqld --verbose --help | grep -A1 ‘Default options’
- 检查端口占用(以 80/443/3306/9000 为例):ss -tulpen | egrep ‘:(80|443|3306|9000)’
- 若使用一键脚本或编译安装,注意 systemd 服务是否存在:systemctl status nginx 若提示 Unit nginx.service could not be found,多为源码安装未生成服务单元。
二、按组件定位与修复
- Nginx
- 端口被占用:ss -tulpen | grep ‘:80’ 找到占用进程 PID,kill 后重启;或改用未占用端口并同步更新防火墙与站点配置。
- 配置错误:执行 nginx -t 修复语法;常见错误为 include 路径、server 块冲突、root 目录不存在等。
- 权限/SELinux:确保监听端口与目录对 nginx 用户可读/可执行;必要时 setsebool -P httpd_can_network_connect 1(按需)。
- PHP-FPM
- 配置或权限:php-fpm -t 校验;检查 /etc/php-fpm.d/www.conf 中 user/group、listen(如 127.0.0.1:9000 或 /run/php-fpm.sock)与目录权限;sock 文件属主应与 listen.owner/group 一致。
- 与 Nginx 通信:Nginx fastcgi_pass 需与 PHP-FPM 的 listen 一致;常见为 fastcgi_pass 127.0.0.1:9000; 并确保 SCRIPT_FILENAME 正确指向 $document_root$fastcgi_script_name。
- MariaDB/MySQL
- 进程/文件锁冲突:错误含 InnoDB: Unable to lock ./ibdata1, error: 11 多为已有 mysqld 占用或异常退出未清理;ps -ef | grep mysqld 查杀后重试,必要时检查数据目录权限与磁盘空间。
- 端口/权限:确认 3306 未被占用;数据目录(如 /var/lib/mysql)属主为 mysql:mysql,权限 700/755 合理。
三、一键安装脚本与编译相关故障
- 服务单元缺失:源码安装后若 systemctl 找不到服务,需自行创建或改用包管理安装;一键脚本环境建议全程使用其提供的启动/升级脚本。
- 编译依赖缺失:PHP 编译阶段常见缺 libcurl、PCRE、libzip 等依赖;先通过 yum/dnf 安装对应开发包,缺失则按提示源码补装后重新编译。
- 升级/安装中断:查看安装/升级日志(如 /root/lnmp-install.log、/root/upgrade_lnmp_php.log),定位具体报错再针对性修复。
四、防火墙与云安全组
- 放行访问端口:firewall-cmd --permanent --add-service=http;firewall-cmd --permanent --add-service=https;firewall-cmd --reload(或放行 80/443/3306 等自定义端口)。
- 云服务器需同时在安全组放行对应端口,否则外网仍无法访问。
五、最小复现与验证
- 建议按以下顺序验证:
- 启动数据库:systemctl start mariadb && systemctl enable mariadb
- 启动 PHP-FPM:systemctl start php-fpm && systemctl enable php-fpm
- 启动 Nginx:systemctl start nginx && systemctl enable nginx
- 验证:ss -tulpen | egrep ‘:(80|443|3306|9000)’;在浏览器访问 http://服务器IP 或 http://域名 查看是否正常解析与加载 PHP 页面。