温馨提示×

LNMP如何监控Debian

小樊
36
2026-08-14 21:06:59
栏目: 智能运维

在 Debian 上监控 LNMP(Linux + Nginx + MySQL/MariaDB + PHP),一般从 系统资源、服务状态、日志、性能 四个层面来做。下面给你一套常用且实用的监控方案,从“基础 → 进阶 → 自动化”逐步说明。


一、基础监控(命令行,最常用)

1️⃣ 系统资源监控

CPU / 内存 / 负载

top
htop        # 推荐(需安装)

安装 htop:

apt update
apt install htop

内存使用

free -h

磁盘使用

df -h
du -sh /*

网络流量

iftop

安装:

apt install iftop

2️⃣ 服务状态监控(LNMP 核心)

Nginx

systemctl status nginx
nginx -t

MySQL / MariaDB

systemctl status mariadb
mysqladmin status

PHP-FPM

systemctl status php-fpm
# 或
systemctl status php8.2-fpm

3️⃣ 端口监听检查

ss -lntp
netstat -lntp

LNMP 常见端口:

  • 80 / 443(Nginx)
  • 3306(MySQL)
  • 9000(PHP-FPM,若 TCP 模式)

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

1️⃣ Nginx 日志

/var/log/nginx/access.log
/var/log/nginx/error.log

实时查看错误:

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

2️⃣ PHP-FPM 日志

/var/log/php8.2-fpm.log

查看慢日志(性能问题):

/var/log/php8.2-fpm-slow.log

3️⃣ MySQL / MariaDB 日志

/var/log/mysql/error.log

三、性能监控(性能瓶颈分析)

1️⃣ MySQL 性能

mysqladmin -u root -p processlist
mysqladmin -u root -p status

查看慢查询:

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

2️⃣ PHP-FPM 状态页(强烈推荐)

开启 status

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

pm.status_path = /php-fpm-status

Nginx 配置:

location /php-fpm-status {
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    include fastcgi_params;
}

访问:

http://IP/php-fpm-status

3️⃣ Nginx 状态监控

location /nginx-status {
    stub_status;
    allow 127.0.0.1;
    deny all;
}

访问:

http://IP/nginx-status

四、自动化监控方案(生产环境推荐)

✅ 方案 1:Zabbix(企业级)

监控内容:

  • CPU / 内存 / 磁盘
  • Nginx / PHP-FPM / MySQL
  • 自定义 LNMP 模板

安装:

apt install zabbix-server-mysql zabbix-frontend-php zabbix-agent

✅ 优点:功能强、告警完善
❌ 缺点:配置略复杂


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

组件

  • Node Exporter:系统监控
  • Nginx Exporter
  • MySQL Exporter
  • Grafana:可视化

✅ 优点:界面漂亮、可扩展
✅ 很适合 LNMP + 云服务器


✅ 方案 3:轻量级(脚本 + 邮件/微信告警)

示例:

#!/bin/bash
if ! systemctl is-active --quiet nginx; then
    echo "Nginx down" | mail -s "ALERT" admin@domain.com
fi

配合 cron:

*/5 * * * * /root/check_lnmp.sh

五、推荐组合(按场景)

场景 推荐
个人服务器 htop + 日志 + 脚本
小公司 Zabbix
高并发网站 Prometheus + Grafana
云服务器 云监控 + 日志

六、我可以继续帮你

你可以告诉我:

  1. Debian 版本(10 / 11 / 12)
  2. LNMP 版本
  3. 个人博客 / 企业站 / 高并发
  4. 是否需要 微信 / 邮件 / 钉钉告警

我可以直接给你 完整可复制的监控配置方案

0