Ubuntu下Apache2监控方法
tail -f /var/log/apache2/access.log 实时显示最新访问记录(如IP、请求路径、状态码);awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -nr 统计访问量最高的IP地址。tail -f /var/log/apache2/error.log 实时查看错误信息(如404、500错误);grep "404" /var/log/apache2/access.log 搜索特定错误类型。top 命令实时显示Apache2进程(apache2或www-data)的CPU、内存占用;htop(需安装:sudo apt install htop)提供更友好的交互界面,支持排序和过滤。sudo apachectl status 查看Apache2的基本运行状态(如服务器版本、模块加载情况、当前连接数)。vmstat 1 每秒显示虚拟内存、CPU、进程等统计信息;iostat -c -d 4 每4秒显示CPU和磁盘I/O使用率(需安装sysstat:sudo apt install sysstat);sar -u 10 每10秒显示CPU利用率。sudo a2enmod status 启用模块;编辑/etc/apache2/mods-enabled/status.conf,修改<Location "/server-status">部分,允许特定IP访问(如Require ip 192.168.1.100);sudo systemctl restart apache2 重启服务。http://your_server_ip/server-status,可查看Apache2的运行状态(如请求处理数、工作线程数、CPU使用率),添加?refresh=5可实现自动刷新。wget官方包并解压,配置prometheus.yml添加Apache2目标targets: ['localhost:9101']);安装Node Exporter(监控系统指标);安装Grafana(sudo apt install grafana),配置Prometheus为数据源,创建仪表盘展示Apache2的访问量、响应时间、错误率等指标。bash <(curl -Ss https://my-netdata.io/kickstart.sh)一键安装;安装完成后,浏览器访问http://your_server_ip:19999,实时查看Apache2的性能指标(如请求速率、响应时间、连接数)。sudo apt install goaccess 安装,sudo goaccess /var/log/apache2/access.log -o report.html 生成实时HTML报告(包含访问量、IP分布、状态码统计);支持终端实时查看和自定义输出格式。crontab -e添加* * * * * /path/to/script.sh),实现定期检查与报警。