温馨提示×

怎样监控linux lnmp

小樊
35
2026-10-03 17:44:57
栏目: 智能运维

监控 Linux + LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 可以从 系统层、服务层、应用层 三个维度来做。下面给你一套实用、可落地的监控方案,从简单到专业。


一、基础监控(必须做)

1. 系统资源监控

查看 CPU、内存、磁盘、网络:

top
htop
free -h
df -h
iostat -x 1
vmstat 1

安装 htop / iotop:

yum install htop iotop -y   # CentOS
apt install htop iotop -y   # Ubuntu

2. Nginx 监控

(1)启用 Nginx status

编辑 nginx 配置:

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
  • 4xx / 5xx 错误率

3. MySQL 监控

(1)基本命令

mysqladmin status
mysql -e "SHOW PROCESSLIST;"
mysql -e "SHOW GLOBAL STATUS LIKE 'Threads%';"

(2)慢查询

SHOW VARIABLES LIKE 'slow_query_log';
SHOW VARIABLES LIKE 'long_query_time';

开启慢日志:

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

4. PHP 监控(PHP-FPM)

(1)启用 status

pm.status_path = /status

Nginx:

location ~ ^/(status)$ {
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi_params;
}

查看:

curl http://127.0.0.1/status

关注:

  • active processes
  • idle processes
  • requests

二、日志监控(非常重要)

1. 实时看错误

tail -f /var/log/nginx/error.log
tail -f /var/log/php-fpm/error.log
tail -f /var/log/mysql/error.log

2. 错误统计

grep "500" /var/log/nginx/access.log | wc -l

三、自动化监控方案(推荐)

✅ 方案一:Zabbix(企业最常用)

监控内容:

  • CPU / 内存 / 磁盘
  • Nginx status
  • MySQL status
  • PHP-FPM
  • 自定义告警

优点:

  • 功能强
  • 告警灵活
  • 适合生产

✅ 方案二:Prometheus + Grafana(现代主流)

组件:

  • node_exporter(系统)
  • nginx-prometheus-exporter
  • mysqld_exporter
  • php-fpm exporter

效果:

  • 实时图表
  • 秒级监控
  • 非常好看

✅ 方案三:轻量级(适合小服务器)

netdata

安装:

bash <(curl -Ss https://my-netdata.io/kickstart.sh)

打开:

http://服务器IP:19999

✅ 零配置,秒级监控


四、告警机制(必须)

常见告警项

  • CPU > 90%
  • 内存 > 90%
  • 磁盘 > 85%
  • Nginx 5xx > 1%
  • MySQL 连接数过高
  • PHP-FPM 进程耗尽

告警方式

  • 邮件
  • 钉钉
  • 企业微信
  • 飞书

五、推荐组合(按场景)

场景 推荐
个人 / 小站 netdata
中小企业 Zabbix
技术团队 Prometheus + Grafana
云服务器 云监控 + 自定义脚本

六、我可以继续帮你

你可以告诉我:

  • ✅ 用的什么系统(CentOS / Ubuntu)
  • ✅ 服务器规模(几台)
  • ✅ 是否生产环境
  • ✅ 想要“看图”还是“告警”

我可以直接给你 完整配置教程。

0 踩