温馨提示×

如何监控debian apache

小樊
36
2026-09-21 18:13:00
栏目: 智能运维

在 Debian 上监控 Apache(通常是 apache2)可以从系统层、服务层、Web 层、日志层、性能层几个维度来做。下面按“从简单到专业”给你一套实用方案。


一、基础:确认 Apache 状态(最常用)

1. systemd 查看服务状态

systemctl status apache2

开机自启检查:

systemctl is-enabled apache2

重启 / 重载:

systemctl restart apache2
systemctl reload apache2

二、实时连接与请求监控

2. 查看当前连接数

ss -ant | grep :80 | wc -l
ss -ant | grep :443 | wc -l

3. 查看 Apache 进程

ps aux | grep apache2

三、Apache 自带模块(强烈推荐)

4. 启用 mod_status

a2enmod status

编辑配置(Debian 通常在 /etc/apache2/mods-enabled/status.conf):

<Location "/server-status">
    SetHandler server-status
    Require local
    # 如需远程
    # Require ip 1.2.3.4
</Location>

启用扩展状态:

ExtendedStatus On

重启:

systemctl restart apache2

访问:

http://localhost/server-status

可看到:

  • 当前请求
  • 空闲/忙碌 worker
  • 访问量统计

四、日志监控(问题排查核心)

5. 实时看访问日志

tail -f /var/log/apache2/access.log

6. 错误日志

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

7. 统计访问 IP

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

五、系统资源监控(Apache 卡顿常用)

8. 基础命令

top
htop
free -h
df -h

9. 查看 Apache 内存占用

ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | grep apache2

六、专业监控方案(生产环境)

10. Prometheus + Node Exporter + Apache Exporter

适合服务器集群。

  • node_exporter:系统指标
  • apache_exporter:Apache 状态指标
  • Prometheus + Grafana

Apache Exporter 示例:

apache_exporter --scrape_uri=http://localhost/server-status?auto

七、告警方案(推荐)

  • 简单monit
apt install monit

监控 apache2 进程,自动重启

  • 专业:Zabbix / Prometheus 告警

八、常见监控指标建议

✅ 必须关注:

  • Apache 是否运行
  • 请求失败率(5xx)
  • 当前连接数
  • 内存占用
  • 磁盘空间

九、快速自检清单

systemctl status apache2
ss -ant | grep :80 | wc -l
tail -n 50 /var/log/apache2/error.log
curl -I http://localhost

如果你愿意,可以告诉我:

  • 是个人服务器还是生产环境?
  • 是否用了 HTTPS / 反向代理 / PHP?
  • 想要 命令行 / Web 面板 / 自动告警 哪种?

我可以直接给你一套可复制的配置

0