Apache 啟動:停止、重新啟動指令與 graceful 的差異
在 Linux 伺服器上,Apache 的啟動、停止與重新啟動都用 systemctl 指令執行,服務名稱在 RHEL 系列是 httpd,在 Debian 系列是 apache2。我們在 AlmaLinux 9 伺服器上讀了 systemctl status 的輸出、httpd 的 unit 檔與 apachectl 腳本,發現 restart 會先關掉 Apache 再重新啟動,而 graceful 會保留父處理程序,只重新讀取設定。
停止與重新啟動會真的讓服務停下來,所以我們沒有在測試伺服器上執行 start、stop 或 restart,而是以 Apache 官方文件、httpd.service 手冊與伺服器上留下的 reload 紀錄確認行為。下方輸出中的主機名稱、連接埠號碼、時間與請求數都是範例值。
1️⃣ Apache 啟動、停止、重新啟動指令
Apache 的服務名稱依發行版系列而不同。RHEL、AlmaLinux、Rocky Linux 等 RHEL 系列以 httpd 這個名稱註冊到 systemd,Ubuntu 與 Debian 則是 apache2。
| 工作 | RHEL 系列(httpd) | Debian 系列(apache2) |
|---|---|---|
| 啟動 | systemctl start httpd | systemctl start apache2 |
| 停止 | systemctl stop httpd | systemctl stop apache2 |
| 重新啟動 | systemctl restart httpd | systemctl restart apache2 |
| 重新讀取設定(graceful) | systemctl reload httpd | systemctl reload apache2 |
| 查看狀態 | systemctl status httpd | systemctl status apache2 |
| 開機時自動啟動 | systemctl enable httpd | systemctl enable apache2 |
表中的指令以 root 帳號執行。若是一般帳號,請在指令前加上 sudo。
如果把系列弄混、輸入另一個名稱,指令就不會執行。在 RHEL 系列伺服器上用 apache2 查詢狀態,出現了找不到 unit 的錯誤。
systemctl status apache2Unit apache2.service could not be found.這時的結束代碼是 4,在同一台伺服器把名稱改成 httpd 後,就出現了正常的輸出。
在 RHEL 系列上,用下面三個指令啟動、停止、重新啟動 Apache。
systemctl start httpdstart 會啟動已停止的 Apache。是否已啟動,請用 systemctl status 確認。
systemctl stop httpdstop 會停止 Apache。在你再次啟動之前,它不會接收網頁請求。
systemctl restart httpdrestart 會依序執行 stop 與 start。如果只改了設定檔,請用不會中斷連線的 reload(graceful)套用。
AlmaLinux 9 的 apachectl 是取代 Apache 所發佈 apachectl 的 shell 腳本。它收到 start、stop、restart、status 時會原樣交給 systemctl,所以 apachectl restart 與 systemctl restart httpd 的動作相同。
2️⃣ 查看 Apache 服務狀態
Apache 現在是否在執行、何時啟動,請用 systemctl status 確認。
systemctl status httpd● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
Active: active (running) since Sat 2026-10-17 08:30:49 UTC; 1 week 4 days ago
Docs: man:httpd.service(8)
Process: 2930107 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 741 (httpd)
Status: "Total requests: 52418; Idle/Busy workers 75/0;Requests/sec: 0.0528; Bytes served/sec: 3.9KB/sec"
Tasks: 224 (limit: 4615)
Memory: 184.3M (peak: 593.8M)
CPU: 47min 33.944s
CGroup: /system.slice/httpd.service
├─ 741 /usr/sbin/httpd -DFOREGROUND
├─2930111 /usr/sbin/httpd -DFOREGROUND
├─2930143 /usr/sbin/httpd -DFOREGROUND
└─2930144 /usr/sbin/httpd -DFOREGROUND
Oct 28 15:00:00 server systemd[1]: Reloading The Apache HTTP Server...
Oct 28 15:00:00 server systemd[1]: Reloaded The Apache HTTP Server.
Oct 28 15:00:00 server httpd[741]: Server configured, listening on: port 80[Active] 行的 active (running) 表示目前正在執行,since 後面的時間就是啟動的時間。這台伺服器在 11 天前啟動後就沒有停過,[Main PID] 741 是當時啟動的父處理程序。
在 [CGroup] 底下、排在 741 下方的 程序,是實際處理請求的子處理程序。[Status] 行會顯示到目前為止處理的請求數,以及閒置與工作中的 worker 數量。
輸出最下方的三行日誌,是每天在同一時間留下的 reload 紀錄。這是按日期切割日誌的 logrotate 在輪替結束後執行 systemctl reload httpd 所留下的,httpd.service 手冊也寫著在 logrotate 之後會對 httpd 執行 reload。
在腳本或檢查指令中,請使用只輸出一個詞的 is-active 與 is-enabled。
systemctl is-active httpdactive執行中時輸出 active 與結束代碼 0。即使輸入伺服器上不存在的 apache2,也會和已停止的服務一樣輸出 inactive 與結束代碼 3,所以在腳本中要先確認服務名稱是否正確。
systemctl is-enabled httpdenabledenabled 是 Apache 已註冊為開機時自動啟動的狀態。
3️⃣ Apache 重新啟動與 graceful 的差異
restart 與 graceful(reload)處理進行中請求與父處理程序的方式不同。我們先從 httpd unit 檔中用於 reload 與 stop 的行確認了這個差異。
systemctl cat httpd | grep -E "Exec|Kill"ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
KillSignal=SIGWINCH
KillMode=mixedExecReload 行表示收到 reload 時會執行 httpd -k graceful,KillSignal 行表示 stop 時送出的訊號是 SIGWINCH(graceful-stop)。
因此 systemctl restart httpd 會先以 graceful-stop 停止 Apache,再重新啟動。進行中的請求會處理完才關閉,但父處理程序本身會重新啟動,所以 [Main PID] 號碼會改變。從停止到啟動之間連接埠是關閉的,無法接收新的連線。Debian 套件的 apache2.service unit 檔在停止時也會執行 apachectl graceful-stop。
httpd -k restart 會向父處理程序送出 HUP 訊號。根據 Apache 官方文件,父處理程序會留著,但子處理程序會立刻結束,所以進行中的請求會中斷,請求統計也會歸 0。
graceful 會向父處理程序送出 USR1 訊號。父處理程序重新讀取設定檔,子處理程序則在處理完手上的請求後,依序換成使用新設定的子處理程序。這段期間進來的連線由作業系統排隊等候,所以不會遺失。
就像營業中的店不關門,收銀員每服務完一位客人,就依序和新班表的店員交班。
這個差異我們用 Main PID 確認過。雖然每天都會執行 reload,Main PID 741 與 Active 時間仍和 11 天前一樣,子處理程序號碼(2930111 等)則緊接在最後一次執行 reload 的 [Process] 行中的 2930107 之後。
systemctl reload httpd、apachectl graceful、httpd -k graceful 全都是同一個 graceful。AlmaLinux 9 的 apachectl 收到 graceful 時會用 systemctl kill 向父處理程序送出 SIGUSR1,前面加上 -k 的 apachectl -k graceful 也會拿掉 -k,做相同的動作。
graceful 只會重新讀取設定,所以不適合在更新 httpd 套件之後使用。根據 httpd.service 手冊,若更新後的模組用到只有新版 httpd 才有的功能,用 reload 並不安全,因此 httpd 套件在更新時會以 systemctl try-restart 自動重新啟動 Apache。
4️⃣ Apache 設定語法檢查
即使用 graceful 在不中斷連線的情況下套用,只要設定檔有錯字,Apache 就不會換成新設定。根據 Apache 官方文件,restart 與 graceful 在執行前會先做語法檢查,有錯誤時只會輸出錯誤訊息,不會重新啟動。
所以修改設定後,請先用 apachectl -t 檢查語法。
apachectl -tSyntax OK執行 apachectl -t 後輸出一行 Syntax OK,用 apachectl configtest 執行也是相同結果。RHEL 系列的 apachectl 收到 -t 時會直接執行 httpd -t。
用一個指令名稱寫錯的測試設定檔檢查時,出現的不是 Syntax OK,而是 AH00526 錯誤,並附上是第幾行、哪個指令無法辨識([Invalid command 'DirectoryIndx']),結束代碼是 1。請修正錯誤訊息中提到的那一行後再檢查一次。
systemctl reload httpd通過檢查後,用 reload 套用設定。根據 Apache 官方文件,語法檢查只看語法,所以即使通過,套用仍可能失敗。reload 之後,請再確認一次 systemctl status 的 [Active] 行與最下方的日誌。
🔲 Apache 指令模擬器
在下方模擬器的虛擬伺服器上,你可以親自輸入 systemctl、apachectl 與 httpd -k 指令。reload 之後 Main PID 不變,只有子處理程序號碼會改變。restart 之後連 Main PID 都會換成新號碼。
🔢 FAQ & 推薦內容
Apache · 重新啟動錯誤
Apache 重新啟動錯誤 | (20014)Internal error: Error retrieving pid file logs/httpd.pid
apachectl restart 等指令出現 pid 檔錯誤的原因,以及清理空白 httpd.pid 的步驟。
Apache · php-fpm
php-fpm max_children 警告:增加工作程序前先從日誌找出原因
在 Nginx、Apache、php-fpm 架構的 WordPress 伺服器上,用日誌找出 max_children 警告堆積原因的紀錄。
ℹ️ 聯盟行銷說明
本站內容包含聯盟行銷連結。訪客透過這些連結購買商品或服務時,本站會從賣家獲得佣金。在這個過程中,購買者支付的金額(遇到活動折扣時金額會降低↓)並不會因此提高。刊登的價格、折扣與庫存資訊以撰寫當時為準,可能與實際情況不同,購買前請向賣家做最後確認。商品的挑選與評價依本站自訂的標準撰寫,是否支付佣金不會影響介紹順序或評價內容。