温馨提示×

怎么监控 linux lnmp

小樊
46
2026-09-12 15:04:47
栏目: 智能运维

监控 Linux 下的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 环境,通常分为 系统层、服务层、应用层、日志层 四个维度。下面我按“用什么监控 + 怎么监控 + 常用命令/工具”给你一个实用方案。


一、系统层监控(CPU / 内存 / 磁盘 / 网络)

1️⃣ 基础命令(临时排查)

top / htop
free -h
df -h
iostat -x 1
iftop

2️⃣ 长期监控工具

  • Netdata(推荐,开箱即用)
bash <(curl -Ss https://my-netdata.io/kickstart.sh)

访问:http://IP:19999

  • Prometheus + node_exporter(专业方案)
    • node_exporter 采集系统指标
    • Grafana 展示

二、Nginx 监控

1️⃣ 开启 Nginx Status

server {
    listen 80;
    server_name localhost;

    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
  • Reading / Writing / Waiting

2️⃣ 日志监控

tail -f /var/log/nginx/access.log
awk '{print $1}' access.log | sort | uniq -c | sort -nr | head

3️⃣ 高级方案

  • nginx-prometheus-exporter
  • Grafana Nginx Dashboard

三、MySQL / MariaDB 监控

1️⃣ 自带命令

mysqladmin status
mysqladmin extended-status

2️⃣ 慢查询监控

SHOW VARIABLES LIKE 'slow_query%';
SHOW PROCESSLIST;

3️⃣ 专业监控

  • Prometheus + mysqld_exporter
  • PMM(Percona Monitoring)(强烈推荐)

四、PHP 监控(PHP-FPM)

1️⃣ 开启 PHP-FPM status

pm.status_path = /status

Nginx 配置:

location ~ ^/(status)$ {
    fastcgi_pass unix:/run/php/php-fpm.sock;
    include fastcgi_params;
}

查看:

curl http://127.0.0.1/status

2️⃣ 监控指标

  • active processes
  • idle processes
  • requests

五、日志集中监控(很重要)

推荐方案

  • ELK(Elasticsearch + Logstash + Kibana)
  • Loki + Grafana
  • GoAccess(实时分析 Nginx)
goaccess /var/log/nginx/access.log -o report.html

六、告警(不能不配)

  • Prometheus Alertmanager
  • Zabbix
  • 钉钉 / 企业微信 / 邮件告警

七、推荐组合(按规模)

✅ 小项目

Netdata + GoAccess

✅ 中型网站

Prometheus + Grafana + Exporter

✅ 大型 / 生产环境

Zabbix / PMM / ELK


如果你愿意,我可以:

  • 给你一套 Prometheus + Grafana LNMP 监控配置
  • 或只针对 Nginx / MySQL / PHP 某一个详细讲
  • 或给你一个 一键监控脚本

你现在是 学习 / 生产 / 排查问题

0