温馨提示×

怎么监控linux lnmp

小樊
46
2026-09-15 21:02:31
栏目: 智能运维

“监控 Linux 上的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP)”通常包含系统层监控服务层监控两部分。下面从简单命令 → 脚本 → 专业监控方案逐步说明,适合不同场景。


一、最基础的手动监控(命令行)

1. 系统资源

top / htop        # CPU、内存、进程
df -h             # 磁盘空间
free -h           # 内存
uptime            # 负载

2. Nginx 监控

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

3. MySQL 监控

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

4. PHP-FPM 监控

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;
}

二、日志监控(非常关键)

Nginx

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

MySQL

tail -f /var/log/mysql/error.log

PHP

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

⚠ 常见异常:

  • 502 → PHP-FPM 挂了
  • 504 → PHP 执行超时
  • 403 → 权限问题

三、自动化脚本监控(轻量方案)

示例:服务存活检测

#!/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

四、专业监控方案(推荐生产环境)

方案 1:Zabbix(企业级)

监控内容:

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

✅ 优点:稳定、功能全
❌ 缺点:部署复杂


方案 2:Prometheus + Grafana(最流行)

组件:

  • node_exporter(系统)
  • nginx-exporter
  • mysqld_exporter
  • php-fpm-exporter
  • Prometheus
  • Grafana 仪表盘

✅ 优点:可视化强、生态好
✅ 适合容器 / 云环境


方案 3:Netdata(开箱即用)

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

✅ 实时图形
✅ 零配置
❌ 不适合大规模集群


五、常见监控指标建议

层级 指标
系统 CPU、内存、磁盘IO、负载
Nginx 请求数、连接数、5xx
MySQL 连接数、慢查询、QPS
PHP 进程数、请求耗时

六、我可以根据你的情况细化

你可以告诉我:

  1. 是个人服务器还是生产环境?
  2. 是否允许装监控软件?
  3. 想要“看图”还是“报警”?
  4. CentOS / Ubuntu / Debian?

我可以直接给你可复制的配置方案

0