插件窝 干货文章 LNMP环境下如何进行故障排查

LNMP环境下如何进行故障排查

检查 bash Nginx 排查 768    来源:    2025-05-06

LNMP环境故障排查指南

LNMP(Linux + Nginx + MySQL + PHP)环境是常见的Web服务架构,以下是针对LNMP环境的系统性故障排查方法。

1. 网络连接检查

1.1 基础网络检查

ping 目标IP/域名
traceroute 目标IP/域名
netstat -tulnp | grep 'nginx\|mysql\|php'

1.2 防火墙检查

iptables -L -n
ufw status  # Ubuntu系统
firewall-cmd --list-all  # CentOS 7+

2. Nginx服务排查

2.1 服务状态检查

systemctl status nginx
nginx -t  # 测试配置文件语法

2.2 日志检查

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

2.3 常见问题

  • 端口冲突:netstat -tulnp | grep :80
  • 权限问题:检查/var/www/html目录权限
  • 配置文件错误:nginx -t检查语法

3. MySQL/MariaDB服务排查

3.1 服务状态检查

systemctl status mysql  # 或 mariadb
mysqladmin -u root -p ping

3.2 日志检查

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

3.3 常见问题

  • 连接数过多:show processlist;
  • 内存不足:检查my.cnf配置
  • 权限问题:检查用户权限SELECT user,host FROM mysql.user;

4. PHP服务排查

4.1 PHP-FPM状态检查

systemctl status php-fpm
ps aux | grep php-fpm

4.2 测试PHP解析

创建测试文件/var/www/html/info.php

<?php phpinfo(); ?>

访问http://your-server/info.php

4.3 日志检查

tail -f /var/log/php-fpm.log

5. 综合检查

5.1 进程检查

top
htop

5.2 磁盘空间检查

df -h
du -sh /var/log/*

5.3 内存检查

free -m
vmstat 1 5

6. 常见故障场景及解决方案

6.1 502 Bad Gateway

  1. 检查PHP-FPM是否运行
  2. 检查Nginx与PHP-FPM的socket配置是否匹配
  3. 检查/var/run/php-fpm目录权限

6.2 数据库连接失败

  1. 检查MySQL服务状态
  2. 检查用户权限和密码
  3. 检查防火墙是否阻止3306端口

6.3 静态文件无法访问

  1. 检查文件权限chown -R nginx:nginx /var/www/html
  2. 检查Nginx配置中的root路径
  3. 检查SELinux状态(如有)

6.4 高负载问题

  1. 检查慢查询日志
  2. 优化MySQL配置
  3. 增加PHP-FPM进程数

7. 高级工具

7.1 性能分析

strace -p [PID]  # 跟踪进程系统调用
perf top  # 性能分析

7.2 数据库分析

mysqldumpslow  # 分析慢查询
pt-query-digest  # Percona工具包

7.3 Web请求分析

ab -n 1000 -c 100 http://your-site.com/  # Apache Benchmark
siege -c 100 -t 1M http://your-site.com/

通过以上系统性的排查方法,可以解决LNMP环境下的大多数常见问题。对于复杂问题,建议结合多种工具和方法综合分析。