Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 下 Apache 服務管理命令大全:啟動、停止、重啟、平滑重載

編輯:Linux 教學

通用二進制控制腳本 (apachectl)

假設安裝路徑 /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 # 配置語法檢查

Systemd 服務管理 (現代主流發行版)

systemctl start httpd      # 啟動 (RHEL/CentOS/Fedora 服務名 httpd)
systemctl stop httpd
systemctl restart httpd
systemctl reload httpd     # 平滑重載配置
systemctl status httpd     # 查看狀態
systemctl enable httpd     # 開機自啟

SysVinit 傳統 service 命令

service httpd start
service httpd stop
service httpd restart
service httpd graceful     # 平滑重啟 (部分版本支援)

Ubuntu / Debian 專用 (apache2 服務名)

/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

平滑重啟 vs 硬重啟

模式命令特點適用場景
硬重啟restart主進程退出,子進程強制終止,所有連接斷開模塊增減、核心配置變更、故障恢復
平滑重啟graceful / reload主進程重讀配置,舊子進程自然結束當前請求後退出,新子進程按新配置啟動修改虛擬主機、SSL 證書、靜態資源路徑等熱更新

常見問題排查

  • apachectl configtest / httpd -t:修改配置後必先執行,語法錯誤會導致重啟失敗。
  • 端口佔用:netstat -tlnp | grep :80 檢查 80/443 端口。
  • 權限不足:啟動需 root 權限 (綁定 <1024 端口),或配置 setcap 'cap_net_bind_service=+ep' /usr/sbin/httpd。
  • SELinux 阻擋:setsebool -P httpd_can_network_connect 1 等。

總結

生產環境首選 systemctl reload httpd 或 apachectl graceful 實現零停機熱更新;重大變更才用 restart。養成「改配置 → configtest → reload」肌肉記憶。

Copyright © Windows教程網 All Rights Reserved