假設安裝路徑 /usr/local/apache2 (請替換為實際路徑):
/usr/local/apache2/bin/apachectl start # 啟動
/usr/local/apache2/bin/apachectl stop # 停止
/usr/local/apache2/bin/apachectl restart # 重啟 (硬重啟,斷開現有連接)
/usr/local/apache2/bin/apachectl graceful # 平滑重啟 (不斷開當前連接,推薦生產環境)
/usr/local/apache2/bin/apachectl configtest # 配置語法檢查
systemctl start httpd # 啟動 (RHEL/CentOS/Fedora 服務名 httpd)
systemctl stop httpd
systemctl restart httpd
systemctl reload httpd # 平滑重載配置
systemctl status httpd # 查看狀態
systemctl enable httpd # 開機自啟
service httpd start
service httpd stop
service httpd restart
service httpd graceful # 平滑重啟 (部分版本支援)
/etc/init.d/apache2 start
/etc/init.d/apache2 stop
/etc/init.d/apache2 restart
/etc/init.d/apache2 reload
# 或
systemctl start apache2
systemctl restart apache2
systemctl reload apache2
| 模式 | 命令 | 特點 | 適用場景 |
|---|---|---|---|
| 硬重啟 | restart | 主進程退出,子進程強制終止,所有連接斷開 | 模塊增減、核心配置變更、故障恢復 |
| 平滑重啟 | graceful / reload | 主進程重讀配置,舊子進程自然結束當前請求後退出,新子進程按新配置啟動 | 修改虛擬主機、SSL 證書、靜態資源路徑等熱更新 |
apachectl configtest / httpd -t:修改配置後必先執行,語法錯誤會導致重啟失敗。netstat -tlnp | grep :80 檢查 80/443 端口。setcap 'cap_net_bind_service=+ep' /usr/sbin/httpd。setsebool -P httpd_can_network_connect 1 等。生產環境首選 systemctl reload httpd 或 apachectl graceful 實現零停機熱更新;重大變更才用 restart。養成「改配置 → configtest → reload」肌肉記憶。