在 Ubuntu 上监控 Apache2 可以从系统资源、Apache 运行状态、访问日志、性能指标、告警几个层面来做。下面按“从简单到专业”的方式介绍。
systemctl status apache2
开机自启检查:
systemctl is-enabled apache2
ps aux | grep apache2
或:
top -p $(pgrep apache2 | tr '\n' ',')
ss -lntp | grep :80
ss -lntp | grep :443
netstat -an | grep :80 | wc -l
或(推荐):
ss -ant | grep :80 | wc -l
sudo a2enmod status
sudo systemctl restart apache2
编辑:
sudo nano /etc/apache2/mods-enabled/status.conf
示例(允许内网访问):
<Location /server-status>
SetHandler server-status
Require ip 192.168.1.0/24
# Require local
</Location>
重启:
sudo systemctl restart apache2
http://服务器IP/server-status
开启扩展状态:
http://服务器IP/server-status?auto
可以看到:
tail -f /var/log/apache2/access.log
tail -f /var/log/apache2/error.log
awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -nr | head
htop
安装:
sudo apt install htop
iotop
iftop
适合服务器集群
Apache Exporter:
https://github.com/Lusitaniae/apache_exporter
bash <(curl -Ss https://my-netdata.io/kickstart.sh)
打开:
http://服务器IP:19999
✅ 必须关注:
✅ 告警建议:
#!/bin/bash
if ! systemctl is-active --quiet apache2; then
echo "Apache is down!" | mail -s "Apache Alert" admin@example.com
systemctl restart apache2
fi
如果你愿意,我可以:
你现在是个人服务器、公司生产环境,还是学习用?