插件窝 干货文章 LNMP服务器如何进行故障排查

LNMP服务器如何进行故障排查

检查 排查 bash 日志 164    来源:    2025-05-06

LNMP服务器故障排查指南

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

1. 基础系统检查

1.1 服务器状态检查

# 检查服务器负载
uptime
top
htop

# 检查内存使用情况
free -h

# 检查磁盘空间
df -h

# 检查磁盘I/O
iostat -x 1

# 检查网络连接
netstat -tulnp
ss -tulnp

1.2 日志检查

# 系统日志
tail -f /var/log/messages
tail -f /var/log/syslog

# 安全日志
tail -f /var/log/secure

2. Nginx故障排查

2.1 服务状态检查

# 检查Nginx状态
systemctl status nginx
service nginx status

# 测试Nginx配置
nginx -t

2.2 日志分析

# 访问日志
tail -f /var/log/nginx/access.log

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

2.3 常见问题

  • 502 Bad Gateway:通常是PHP-FPM未运行或配置错误
  • 403 Forbidden:检查文件权限和目录索引设置
  • 404 Not Found:检查Nginx配置中的root路径

3. MySQL故障排查

3.1 服务状态检查

# 检查MySQL状态
systemctl status mysql
service mysql status

# 连接测试
mysql -u root -p

3.2 日志分析

# MySQL错误日志
tail -f /var/log/mysql/error.log

3.3 常见问题

  • 连接数过多:检查max_connections设置
  • 内存不足:调整innodb_buffer_pool_size
  • 表损坏:使用mysqlcheck修复

4. PHP/PHP-FPM故障排查

4.1 服务状态检查

# 检查PHP-FPM状态
systemctl status php-fpm
service php-fpm status

# 检查PHP版本
php -v

4.2 日志分析

# PHP-FPM日志
tail -f /var/log/php-fpm/error.log

# PHP错误日志(取决于php.ini配置)
tail -f /var/log/php_errors.log

4.3 常见问题

  • 脚本执行超时:调整max_execution_time
  • 内存不足:调整memory_limit
  • 文件上传问题:检查upload_max_filesizepost_max_size

5. 综合问题排查

5.1 权限问题

# 检查Web目录权限
ls -la /path/to/webroot

# 通常建议权限设置
chown -R www-data:www-data /path/to/webroot
find /path/to/webroot -type d -exec chmod 755 {} \;
find /path/to/webroot -type f -exec chmod 644 {} \;

5.2 端口冲突

# 检查端口占用
netstat -tulnp | grep :80
lsof -i :80

5.3 防火墙/SELinux

# 检查防火墙规则
iptables -L
firewall-cmd --list-all

# 临时关闭SELinux(生产环境不推荐)
setenforce 0

6. 高级诊断工具

6.1 网络诊断

# 检查DNS解析
nslookup example.com
dig example.com

# 检查网络连通性
traceroute example.com
mtr example.com

6.2 性能分析

# PHP性能分析
xdebug

# MySQL慢查询分析
mysqldumpslow /var/log/mysql/mysql-slow.log

# Nginx实时监控
ngxtop

6.3 压力测试

# 使用ab进行压力测试
ab -n 1000 -c 100 http://example.com/

# 使用siege
siege -c100 -t1M http://example.com/

7. 故障排查流程建议

  1. 确认问题现象和重现步骤
  2. 检查相关服务日志
  3. 验证服务是否正常运行
  4. 检查配置文件和权限设置
  5. 测试网络连接和端口可用性
  6. 逐步排除可能的故障点
  7. 记录排查过程和解决方案

通过系统化的排查方法,可以快速定位和解决LNMP环境中的大多数问题。