LNMP(Linux + Nginx + MySQL + PHP)环境的监控需要从多个层面进行,包括系统资源、服务状态、性能指标等。以下是全面的监控方案:
# 检查Nginx进程
ps aux | grep nginx
# 检查Nginx监听端口
netstat -tulnp | grep nginx
在nginx.conf中启用状态模块:
server {
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
访问 http://your-server/nginx_status
获取状态信息
使用工具分析:
# 实时监控日志
tail -f /var/log/nginx/access.log
# 统计HTTP状态码
awk '{print $9}' access.log | sort | uniq -c | sort -rn
# 检查MySQL进程
ps aux | grep mysql
# 检查MySQL运行状态
systemctl status mysql
-- 查看服务器状态变量
SHOW STATUS;
-- 查看服务器系统变量
SHOW VARIABLES;
-- 查看当前连接数
SHOW STATUS LIKE 'Threads_connected';
-- 查看进程列表
SHOW PROCESSLIST;
mysqladmin:命令行管理工具
mysqladmin -u root -p status
mysqladmin -u root -p extended-status
mytop:类似top的MySQL监控工具
mytop -u root -p password
在my.cnf中启用慢查询日志:
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 2
使用mysqldumpslow分析慢查询:
mysqldumpslow -s t /var/log/mysql/mysql-slow.log
在php-fpm.conf中启用状态页:
pm.status_path = /status
Nginx配置示例:
location ~ ^/(status|ping)$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
访问 http://your-server/status
查看PHP-FPM状态
#!/bin/bash
# 监控LNMP状态的简单脚本
# 系统状态
echo "===== System Status ====="
uptime
free -m
# Nginx状态
echo -e "\n===== Nginx Status ====="
systemctl status nginx | grep Active
echo "Nginx Connections:"
netstat -nat | grep :80 | wc -l
# MySQL状态
echo -e "\n===== MySQL Status ====="
systemctl status mysql | grep Active
echo "MySQL Connections:"
mysqladmin -u root -p'password' status | awk '{print $4}'
# PHP-FPM状态
echo -e "\n===== PHP-FPM Status ====="
systemctl status php-fpm | grep Active
echo "PHP-FPM Processes:"
ps aux | grep php-fpm | wc -l
通过以上方案,您可以全面监控LNMP环境的运行状态,及时发现并解决问题。