在 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 命令,這是一種可行的替代方案。