Centos7

systemctl restart httpd 啟動失敗 Apache HTTP Server httpd pid 已經在執行

  • April 11, 2022

請原諒來自 Stack Overflow 的交叉文章,但我意識到這可能是更好的詢問位置。

我最近在 AWS 上重建了我的伺服器,從 Amazon Linux 升級到 Amazon Linux 2,就像 Linux 1 似乎是 CentOS 的一個分支,現在是 CentOS 7。我的虛擬主機配置與之前的建構保持相同,而所有其他 conf 文件都是較新的預設文件。(僅供參考,不確定它是否有任何影響)。

自從重建我不能再做systemctl restart httpd。我得到錯誤The Apache HTTP Server httpd pid (NNNN) already running

我做了很多Google搜尋,發現很多人都有這個錯誤,但結果總是與他們所做的*不同。*我想不出我所做的任何“不同”。

我了解到,我看到的“已經在執行”的 PID 由 root 擁有,是“主程序”,而 apache 擁有的所有其他 PID 都是“工作程序”。CentOS httpd 作為 root 和 apache 使用者執行

root      4461     1  0 07:18 ?        00:00:00 /usr/sbin/httpd -k restart
apache    4467  4461  0 07:18 ?        00:00:09 /usr/sbin/httpd -k restart
apache    4468  4461  0 07:18 ?        00:00:08 /usr/sbin/httpd -k restart
apache    4471  4461  0 07:18 ?        00:00:08 /usr/sbin/httpd -k restart
apache    4477  4461  0 07:18 ?        00:00:08 /usr/sbin/httpd -k restart
apache    4498  4461  0 07:18 ?        00:00:07 /usr/sbin/httpd -k restart
apache    5236  4461  0 07:19 ?        00:00:08 /usr/sbin/httpd -k restart
apache    5248  4461  0 07:19 ?        00:00:09 /usr/sbin/httpd -k restart
apache    5987  4461  0 07:20 ?        00:00:09 /usr/sbin/httpd -k restart
apache    5993  4461  0 07:20 ?        00:00:08 /usr/sbin/httpd -k restart
apache    5994  4461  0 07:20 ?        00:00:09 /usr/sbin/httpd -k restart
# systemctl restart httpd;systemctl status httpd;
Job for httpd.service failed. See "systemctl status httpd.service" and "journalctl -xe" for details.
● httpd.service - The Apache HTTP Server
  Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  Active: failed (Result: protocol) since Thu 2021-12-23 19:56:05 UTC; 4ms ago
    Docs: man:httpd.service(8)
 Process: 31799 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
Main PID: 31799 (code=exited, status=0/SUCCESS)
  Status: "Reading configuration..."

Dec 23 19:56:04 ip-10-5-11-55.ec2.internal systemd[1]: Starting The Apache HTTP Server...
Dec 23 19:56:05 ip-10-5-11-55.ec2.internal httpd[31799]: httpd (pid 4461) already running
Dec 23 19:56:05 ip-10-5-11-55.ec2.internal systemd[1]: Failed to start The Apache HTTP Server.
Dec 23 19:56:05 ip-10-5-11-55.ec2.internal systemd[1]: Unit httpd.service entered failed state.
Dec 23 19:56:05 ip-10-5-11-55.ec2.internal systemd[1]: httpd.service failed.

Apache 錯誤日誌中沒有異常:

[Thu Dec 23 07:18:58.777745 2021] [suexec:notice] [pid 4460] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Thu Dec 23 07:18:58.802753 2021] [lbmethod_heartbeat:notice] [pid 4461] AH02282: No slotmem from mod_heartmonitor
[Thu Dec 23 07:18:58.802850 2021] [http2:warn] [pid 4461] AH10034: The mpm module (prefork.c) is not supported by mod_http2. The mpm determines how things are processed in your server. HTTP/2 has more
[Thu Dec 23 07:18:58.847386 2021] [mpm_prefork:notice] [pid 4461] AH00163: Apache/2.4.51 () PHP/7.4.21 configured -- resuming normal operations
[Thu Dec 23 07:18:58.847414 2021] [core:notice] [pid 4461] AH00094: Command line: '/usr/sbin/httpd'

如果我殺死這個“主”PID,那麼我可以重新啟動沒問題。

# kill 4461
# systemctl restart httpd;systemctl status httpd;
● httpd.service - The Apache HTTP Server
  Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  Active: active (running) since Thu 2021-12-23 20:01:31 UTC; 6ms ago
    Docs: man:httpd.service(8)
Main PID: 3091 (httpd)
  Status: "Configuration loaded."
  CGroup: /system.slice/httpd.service
          └─3091 /usr/sbin/httpd -DFOREGROUND

Dec 23 20:01:31 ip-10-5-11-55.ec2.internal systemd[1]: Starting The Apache HTTP Server...
Dec 23 20:01:31 ip-10-5-11-55.ec2.internal systemd[1]: Started The Apache HTTP Server.
# ps -Af | grep httpd
root      3145     1  1 20:01 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    3147  3145  0 20:01 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    3153  3145  0 20:01 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    3171  3145  0 20:01 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    3177  3145  0 20:01 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    3183  3145  0 20:01 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

大多數人的建議是“殺死程序”,Apache 無法啟動,已經執行但沒有處理 pid 文件?但我需要這個在無人看管的情況下工作。我還能看什麼?

雖然我搜尋了很多,但沒有找到其他 Linux 風格的答案。當我注意到 httpd 最初以“apachectl”開頭並且我試圖使用“systemctl”重新啟動並認為這可能是問題時,我得到了答案。當我搜尋這個時,我找到了很多答案。這似乎是最簡潔的:https ://unix.stackexchange.com/questions/240528/apache-and-systemd

在其他情況下,您會看到我遇到的相同問題,即一旦您開始使用 apachectl,您必須堅持使用它,因為 systemd 無法知道您對 apachectl 做了什麼,因為它使用了不同的配置。

引用自:https://serverfault.com/questions/1088121