LNMP(Linux + Nginx + MySQL + PHP)是常见的Web服务器架构。以下是针对LNMP环境的系统化故障排查方法:
# 检查服务器负载
uptime
top
htop
# 检查内存使用情况
free -h
# 检查磁盘空间
df -h
# 检查磁盘I/O
iostat -x 1
# 检查网络连接
netstat -tulnp
ss -tulnp
# 系统日志
tail -f /var/log/messages
tail -f /var/log/syslog
# 安全日志
tail -f /var/log/secure
# 检查Nginx状态
systemctl status nginx
service nginx status
# 测试Nginx配置
nginx -t
# 访问日志
tail -f /var/log/nginx/access.log
# 错误日志
tail -f /var/log/nginx/error.log
# 检查MySQL状态
systemctl status mysql
service mysql status
# 连接测试
mysql -u root -p
# MySQL错误日志
tail -f /var/log/mysql/error.log
max_connections
设置innodb_buffer_pool_size
mysqlcheck
修复# 检查PHP-FPM状态
systemctl status php-fpm
service php-fpm status
# 检查PHP版本
php -v
# PHP-FPM日志
tail -f /var/log/php-fpm/error.log
# PHP错误日志(取决于php.ini配置)
tail -f /var/log/php_errors.log
max_execution_time
memory_limit
upload_max_filesize
和post_max_size
# 检查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 {} \;
# 检查端口占用
netstat -tulnp | grep :80
lsof -i :80
# 检查防火墙规则
iptables -L
firewall-cmd --list-all
# 临时关闭SELinux(生产环境不推荐)
setenforce 0
# 检查DNS解析
nslookup example.com
dig example.com
# 检查网络连通性
traceroute example.com
mtr example.com
# PHP性能分析
xdebug
# MySQL慢查询分析
mysqldumpslow /var/log/mysql/mysql-slow.log
# Nginx实时监控
ngxtop
# 使用ab进行压力测试
ab -n 1000 -c 100 http://example.com/
# 使用siege
siege -c100 -t1M http://example.com/
通过系统化的排查方法,可以快速定位和解决LNMP环境中的大多数问题。