在 CentOS 系统中,Apache2 通常被称为 httpd
方法一:使用 systemctl 命令
sudo yum install httpd
sudo systemctl start httpd
sudo systemctl enable httpd
现在,Apache2 服务将在系统启动时自动运行,并在出现故障时自动重启。
方法二:使用 crontab 定时任务
crontab -e
* * * * * systemctl is-active --quiet httpd || systemctl restart httpd
注意:这种方法可能不如使用 systemctl 命令可靠,因为它依赖于定期检查服务状态。但是,如果您的系统不支持 systemctl 命令,这是一种可行的替代方案。