“监控 Linux 上的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP)”通常包含系统层监控和服务层监控两部分。下面从简单命令 → 脚本 → 专业监控方案逐步说明,适合不同场景。
top / htop # CPU、内存、进程
df -h # 磁盘空间
free -h # 内存
uptime # 负载
nginx -t # 配置检测
systemctl status nginx
netstat -lntp | grep nginx
查看 Nginx 状态(需开启 stub_status):
location /nginx_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
访问:http://localhost/nginx_status
mysqladmin status
mysql -e "SHOW PROCESSLIST;"
mysql -e "SHOW GLOBAL STATUS LIKE 'Threads_connected';"
systemctl status php-fpm
PHP-FPM 状态页(www.conf):
pm.status_path = /status
Nginx 中配置访问:
location /php_status {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /status;
}
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
tail -f /var/log/mysql/error.log
tail -f /var/log/php-fpm/error.log
⚠ 常见异常:
#!/bin/bash
for svc in nginx mysql php-fpm; do
systemctl is-active --quiet $svc || echo "$svc down" | mail -s "LNMP Alarm" admin@x.com
done
加入 crontab:
* * * * * /root/check_lnmp.sh
监控内容:
✅ 优点:稳定、功能全
❌ 缺点:部署复杂
组件:
✅ 优点:可视化强、生态好
✅ 适合容器 / 云环境
bash <(curl -Ss https://my-netdata.io/kickstart.sh)
✅ 实时图形
✅ 零配置
❌ 不适合大规模集群
| 层级 | 指标 |
|---|---|
| 系统 | CPU、内存、磁盘IO、负载 |
| Nginx | 请求数、连接数、5xx |
| MySQL | 连接数、慢查询、QPS |
| PHP | 进程数、请求耗时 |
你可以告诉我:
我可以直接给你可复制的配置方案。