在 Ubuntu 上监控 Apache2 通常可以从 服务状态、访问/错误日志、性能与资源、运行状态接口 几个层面来做。下面按“由浅入深”的方式说明。
systemctl status apache2
常见状态:
active (running) ✅ 正常inactive (dead) ❌ 未运行failed ❌ 启动失败启动 / 重启 / 停止:
sudo systemctl start apache2
sudo systemctl restart apache2
sudo systemctl stop apache2
设置开机自启:
sudo systemctl enable apache2
tail -f /var/log/apache2/access.log
tail -f /var/log/apache2/error.log
# 查看访问最多的 IP
awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -nr | head
# 查看 500 错误
grep " 500 " /var/log/apache2/access.log
ps aux | grep apache2
top
# 或
htop
sudo netstat -tulnp | grep apache
# 或
sudo ss -tulnp | grep apache
sudo a2enmod status
sudo systemctl restart apache2
编辑:
sudo nano /etc/apache2/mods-enabled/status.conf
示例(允许本地访问):
<Location "/server-status">
SetHandler server-status
Require local
</Location>
如需远程访问(谨慎):
Require ip 1.2.3.4
http://服务器IP/server-status
可看到:
适合服务器集群、生产环境。
例如检测 Apache 是否运行:
#!/bin/bash
systemctl is-active --quiet apache2 || echo "Apache down" | mail -s "Alert" admin@mail.com
| 指标 | 说明 |
|---|---|
| 请求数 / 秒 | 负载 |
| 错误率 (4xx/5xx) | 服务质量 |
| 连接数 | 是否接近上限 |
| 内存占用 | 是否泄漏 |
| 响应时间 | 用户体验 |
如果你愿意,可以告诉我:
我可以给你更精确的监控方案。