温馨提示×

如何监控debian lnmp

小樊
49
2026-09-07 20:44:11
栏目: 智能运维

监控 Debian 上的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 环境,通常可以从系统资源、服务状态、Web 服务、数据库、日志、告警几个层面来做。下面给你一套实用可落地的方案,从简单到进阶。


一、基础监控(命令行,零成本)

1. 系统资源监控

top
htop        # 推荐,需安装
free -h     # 内存
df -h       # 磁盘
uptime      # 负载

2. 服务状态监控

systemctl status nginx
systemctl status mysql
systemctl status php-fpm

批量检查:

for s in nginx mysql php-fpm; do systemctl is-active $s; done

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

三、MySQL / MariaDB 监控

1. 基本状态

mysqladmin status
mysqladmin extended-status

2. 慢查询 & 连接数

SHOW PROCESSLIST;
SHOW STATUS LIKE 'Threads_connected';

3. 慢日志(强烈建议)

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

四、PHP-FPM 监控

1. 开启 status 页

/etc/php/*/fpm/pool.d/www.conf

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

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

关键日志位置

/var/log/nginx/access.log
/var/log/nginx/error.log
/var/log/mysql/error.log
/var/log/php-fpm.log

实时查看

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

错误统计

grep -c "error" /var/log/nginx/error.log

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

✅ 方案 1:Netdata(最简单、可视化)

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

✅ 实时监控 CPU、内存、Nginx、MySQL、PHP
✅ 网页访问:http://IP:19999


✅ 方案 2:Prometheus + Grafana(专业)

组件:

  • node_exporter(系统)
  • nginx-exporter
  • mysqld-exporter
  • php-fpm-exporter

适合:

  • 多服务器
  • 长期监控
  • 告警

✅ 方案 3:Zabbix(企业级)

  • 自动发现 LNMP
  • 告警邮件 / 微信
  • 历史数据

七、告警机制(必须)

1. 磁盘告警

df -h | awk '$5+0 > 80 {print $0}'

2. 服务挂了自动重启

systemctl enable nginx
systemctl enable mysql

3. 邮件/脚本告警

systemctl is-active nginx || echo "nginx down" | mail -s "alert" admin@xx.com

八、推荐最小监控组合(新手)

htop
nginx status
php-fpm status
mysqladmin status
Netdata
✅ 日志 tail -f


如果你愿意,我可以:

  • 给你一套 Debian LNMP 监控脚本
  • 帮你 配置 Prometheus + Grafana
  • 或按 生产 / 个人服务器 给你定制方案

你现在是学习 / 生产 / 云服务器

0