温馨提示×

如何监控linux lnmp

小樊
34
2026-09-27 21:28:31
栏目: 智能运维

监控 Linux + Nginx + MySQL + PHP(LNMP) 可以从 系统层、服务层、应用层 三个维度来做。下面是一套比较实用、由浅入深的方案。


一、系统层监控(基础)

目的:看服务器是否活着、资源是否够用

1. 基础命令(临时排查)

top / htop        # CPU、内存
df -h             # 磁盘
free -m           # 内存
uptime            # 负载
iostat -x 1       # 磁盘IO
sar -n DEV 1      # 网络

2. 长期监控方案

  • node_exporter + Prometheus + Grafana(推荐)
  • Zabbix
  • netdata(开箱即用,轻量)

二、Nginx 监控

目的:看访问量、错误率、连接数

1. 开启 Nginx status

location /nginx_status {
    stub_status on;
    allow 127.0.0.1;
    deny all;
}

查看:

curl http://127.0.0.1/nginx_status

关键指标:

  • Active connections
  • Requests per second
  • 4xx / 5xx 比例

2. 日志监控

tail -f /var/log/nginx/access.log

关注:

  • 状态码(500、502、404)
  • 慢请求
  • IP 异常访问

可用:

  • GoAccess(实时可视化)
  • ELK / Loki

三、MySQL 监控

目的:看慢查询、连接、锁

1. 内置命令

SHOW PROCESSLIST;
SHOW STATUS LIKE 'Threads%';
SHOW GLOBAL STATUS LIKE 'Slow_queries';

2. 慢查询日志

slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1

3. 监控工具

  • mysqld_exporter + Prometheus
  • Percona Monitoring
  • pt-query-digest

四、PHP 监控

目的:看 PHP 是否卡死、内存泄漏、慢脚本

1. PHP-FPM status

pm.status_path = /status

Nginx:

location /php_status {
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

关注:

  • active processes
  • idle processes
  • slow requests

2. 慢日志

request_slowlog_timeout = 2s
slowlog = /var/log/php-fpm/slow.log

五、服务存活监控(必须)

目的:服务挂了能马上知道

1. 进程检查

ps aux | grep nginx
ps aux | grep php-fpm
systemctl status mysql

2. 自动告警

  • 钉钉 / 企业微信 / 邮件
  • Prometheus Alertmanager
  • Zabbix 触发器

六、推荐组合方案(实战)

✅ 中小项目

  • netdata(实时)
  • 日志 + 告警脚本

✅ 生产环境

  • Prometheus + Grafana
    • node_exporter
    • nginx_exporter
    • mysqld_exporter
    • php-fpm exporter
  • Alertmanager 告警

✅ 大流量

  • ELK / Loki 日志系统
  • APM(如 SkyWalking、Pinpoint)

七、常见监控指标清单

  • CPU > 80%
  • 内存 > 90%
  • 磁盘 > 85%
  • Nginx 5xx > 1%
  • PHP slow request
  • MySQL 连接数接近 max_connections
  • 网站响应时间 > 1s

如果你愿意,我可以:

  • 给你一套 Prometheus + Grafana 的 LNMP 监控配置
  • 或针对 单台服务器 / 云服务器 / 容器 给具体方案

0 踩