1. 使用systemctl命令管理Apache服务状态
CentOS 7及以上版本使用systemd管理服务,可通过以下命令快速检查Apache(httpd)的运行状态、启动/停止服务或配置开机自启:
sudo systemctl status httpd(显示是否运行、最近日志片段);sudo systemctl start httpd、sudo systemctl stop httpd、sudo systemctl restart httpd;sudo systemctl reload httpd(不重启服务即可应用配置变更);sudo systemctl enable httpd(开机自动启动Apache)。2. 启用Apache mod_status模块查看实时状态
mod_status是Apache内置的性能监控模块,可提供服务器运行状态、请求处理数、工作线程等详细信息:
/etc/httpd/conf/httpd.conf或/etc/apache2/apache2.conf),添加以下内容:<IfModule mod_status.c>
    ExtendedStatus On
    <Location "/server-status">
        SetHandler server-status
        Require local  # 仅允许本地访问,如需远程访问可替换为Require ip 你的IP
    </Location>
</IfModule>
sudo systemctl restart httpd;http://服务器IP/server-status,即可查看实时状态(如请求处理速率、CPU使用率、内存占用等)。3. 查看Apache日志分析运行情况
Apache的日志文件记录了访问请求和错误信息,是监控服务器状态的重要依据:
/var/log/httpd/access_log(CentOS 7)或/var/log/apache2/access.log(CentOS 8+);/var/log/httpd/error_log(CentOS 7)或/var/log/apache2/error.log(CentOS 8+)。sudo tail -f /var/log/httpd/access_log;sudo tail -f /var/log/httpd/error_log;sudo awk '{print $1}' /var/log/httpd/access_log | cut -d'-' -f1 | sort | uniq -c | sort -rn;sudo grep "192.168.1.1" /var/log/httpd/access_log;sudo grep " 404 " /var/log/httpd/access_log | wc -l。4. 使用第三方监控工具实现全面监控
第三方工具可提供图形化界面、告警功能和更丰富的监控维度:
http://服务器IP:19999访问仪表盘。安装命令:sudo yum install netdata -y;prometheus_apache_exporter),Grafana负责可视化。通过http://服务器IP:3000创建Apache监控面板;sudo yum install monit -y,配置文件示例:check process httpd with pidfile /var/run/httpd.pid
    start program = "/usr/sbin/apachectl start"
    stop program = "/usr/sbin/apachectl stop"
    if failed host 127.0.0.1 port 80 protocol http then restart
sudo yum install glances -y,启动命令:glances。5. 通过Shell脚本+Cron定时任务实现自动化监控
编写简单的Shell脚本检查Apache进程状态,结合Cron定时任务定期执行,实现自动化监控:
/usr/local/bin/apache_monitor.sh):#!/bin/bash
if ! pgrep -x httpd > /dev/null; then
    echo "$(date): Apache is not running. Starting..." >> /var/log/apache_monitor.log
    systemctl start httpd
fi
sudo chmod +x /usr/local/bin/apache_monitor.sh;crontab -e,添加以下内容(每5分钟检查一次):*/5 * * * * /usr/local/bin/apache_monitor.sh
日志将记录在/var/log/apache_monitor.log中。