Ubuntu LNMP 性能监控实操指南
一 快速巡检命令
location /nginx_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
访问 http://服务器IP/nginx_status 查看 Active connections、Reading、Writing、Waiting 等指标。二 可视化与长期监控
scrape_configs:
- job_name: 'node'
static_configs: [{ targets: ['localhost:9100'] }]
- job_name: 'nginx'
static_configs: [{ targets: ['服务器IP:9113'] }]
- job_name: 'mysql'
static_configs: [{ targets: ['服务器IP:9104'] }]
三 关键指标与告警阈值建议
| 维度 | 关键指标 | 建议阈值或动作 |
|---|---|---|
| CPU | load average、CPU 使用率 | 持续高于 CPU 核心数 或 >80% 持续 5 分钟,优先排查慢查询/阻塞进程 |
| 内存 | 可用内存、swap 使用 | 可用 < 10% 或 swap 持续 >0,优化应用内存或扩容 |
| 磁盘 | IOPS、await、util | await 高、util 接近 100%,检查磁盘健康与 SQL/日志写入 |
| 网络 | 带宽占用、丢包/重传 | 带宽接近网卡上限或 重传率高,优化内容分发/压缩与链路质量 |
| Nginx | Active/Waiting、5xx 比例 | 5xx > 1% 或 Waiting 持续偏高,检查后端健康与超时配置 |
| MySQL | Threads_connected、Slow_queries、QPS/TPS | Threads_connected 接近上限或 慢查询突增,优化索引/SQL 与连接池 |
| PHP-FPM | 进程占用、慢请求 | 慢请求增多 或 进程打满,调整 pm.max_children/request_terminate_timeout 与慢日志 |
四 日志与慢查询优化
五 一键巡检脚本示例
#!/usr/bin/env bash
echo "=== 系统资源 ==="
free -h && echo
vmstat 1 5 && echo
iostat -x 1 5 && echo
echo "=== 服务状态 ==="
systemctl is-active --quiet nginx && echo "Nginx: active" || echo "Nginx: inactive"
systemctl is-active --quiet mysql && echo "MySQL: active" || echo "MySQL: inactive"
systemctl is-active --quiet php-fpm && echo "PHP-FPM: active" || echo "PHP-FPM: inactive"
echo "=== 端口监听 ==="
ss -tulpen | egrep ':(80|443|3306|9000)' && echo
echo "=== Nginx 状态页 ==="
curl -s http://127.0.0.1/nginx_status 2>/dev/null || echo "Nginx status disabled or not reachable"
echo "=== MySQL 状态 ==="
mysqladmin -u root -p status 2>/dev/null || echo "MySQL status failed (check credentials/permissions)"