Centos

Apache 不會在本地網路中的 CentOS 上啟動

  • October 11, 2021

我在 CentOS 7 伺服器上安裝了 httpd,但systemctl start httpd.service失敗了。

httpd為了在 CentOS 7 上正確啟動,需要輸入什麼特定的命令序列?


錯誤資訊


從底部的完整結果中提取的精確錯誤消息如下:

AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain.   
Set the 'ServerName' directive globally to suppress this message  

此外,根據@DopeGhoti 的建議,日誌的內容是:

[root@localhost ~]# vi /var/log/httpd/error_log
(13)Permission denied: AH00091: httpd: could not open error log file /var/www/mytestdeployment/error.log.
AH00015: Unable to open logs
(13)Permission denied: AH00091: httpd: could not open error log file /var/www/mytestdeployment/error.log.
AH00015: Unable to open logs
(13)Permission denied: AH00091: httpd: could not open error log file /var/www/mytestdeployment/error.log.

如何httpd安裝:


1.) 安裝 Apache:

sudo yum -y install httpd

2.) 啟用 Apache 作為 CentOS 服務,以便它在重新啟動時自動重新啟動:

sudo systemctl enable httpd.service

3.) 配置防火牆

sudo firewall-cmd --zone=public --add-service=http
sudo firewall-cmd --list-all
sudo firewall-cmd --zone=public --permanent --add-service=http  

4.) 給伺服器起一個名字:

vi /etc/httpd/conf/httpd.conf
//Uncomment the ServerName line and give it the IP of the machine:  
ServerName 192.168.1.5:80

錯誤資訊:


httpd使用上述命令安裝後,httpd啟動失敗如下:

[root@localhost ~]# systemctl start httpd.service
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.


[root@localhost ~]# systemctl status httpd.service -l
● httpd.service - The Apache HTTP Server
  Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
  Active: failed (Result: exit-code) since Tue 2017-06-06 11:31:32 PDT; 15min ago
    Docs: man:httpd(8)
          man:apachectl(8)
 Process: 32268 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
 Process: 32267 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
Main PID: 32267 (code=exited, status=1/FAILURE)

Jun 06 11:31:32 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
Jun 06 11:31:32 localhost.localdomain httpd[32267]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Jun 06 11:31:32 localhost.localdomain systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE
Jun 06 11:31:32 localhost.localdomain kill[32268]: kill: cannot find process ""
Jun 06 11:31:32 localhost.localdomain systemd[1]: httpd.service: control process exited, code=exited status=1
Jun 06 11:31:32 localhost.localdomain systemd[1]: Failed to start The Apache HTTP Server.
Jun 06 11:31:32 localhost.localdomain systemd[1]: Unit httpd.service entered failed state.
Jun 06 11:31:32 localhost.localdomain systemd[1]: httpd.service failed.
[root@localhost ~]# systemctl status httpd.service -l

[root@localhost ~]# vi /var/log/httpd/error_log
(13)Permission denied: AH00091: httpd: could not open error log file /var/www/mytestdeployment/error.log.
AH00015: Unable to open logs
(13)Permission denied: AH00091: httpd: could not open error log file /var/www/mytestdeployment/error.log.
AH00015: Unable to open logs
(13)Permission denied: AH00091: httpd: could not open error log file /var/www/mytestdeployment/error.log.
AH00015: Unable to open logs
(13)Permission denied: AH00091: httpd: could not open error log file /var/www/mytestdeployment/error.log.
AH00015: Unable to open logs
(13)Permission denied: AH00091: httpd: could not open error log file /var/www/mytestdeployment/error.log.
AH00015: Unable to open logs
~
"/var/log/httpd/error_log" 10L, 675C

@JeffSchaller 的建議


在@JeffSchaller 建議考慮 SELinux 之後,我發現setenforce 0以 root 身份輸入會導致以下結果:

[root@localhost ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      28

[root@localhost ~]# setenforce 0

[root@localhost ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      28

[root@localhost ~]# systemctl start httpd.service -l
[root@localhost ~]# systemctl status httpd.service -l
● httpd.service - The Apache HTTP Server
  Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
  Active: active (running) since Tue 2017-06-06 12:28:38 PDT; 22s ago
    Docs: man:httpd(8)
          man:apachectl(8)
 Process: 32577 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
Main PID: 32690 (httpd)
  Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
  CGroup: /system.slice/httpd.service
          ├─32690 /usr/sbin/httpd -DFOREGROUND
          ├─32691 /usr/sbin/httpd -DFOREGROUND
          ├─32692 /usr/sbin/httpd -DFOREGROUND
          ├─32693 /usr/sbin/httpd -DFOREGROUND
          ├─32694 /usr/sbin/httpd -DFOREGROUND
          └─32695 /usr/sbin/httpd -DFOREGROUND

Jun 06 12:28:38 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
Jun 06 12:28:38 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
[root@localhost ~]#

Apache 啟動失敗,出現錯誤提示

(13)權限被拒絕:AH00091:httpd:無法打開錯誤日誌文件/var/www/mytestdeployment/error.log。

AH00015: 無法打開日誌

由於 SELinux 處於強制模式,它阻止 Apache 寫入非標準日誌目錄。為了防止Dan Walsh 哭泣CodeMed高效,我們可以將 httpd_log_t 策略應用於該目錄:

semanage fcontext -a -t httpd_log_t "/var/www/mytestdeployment(/.*)?"
restorecon -Rv /var/www/mytestdeployment

並確認:

ls -lZ /var/www/mytestdeployment

如果您沒有 semanage 實用程序,您可以使用以下命令安裝它:

yum install policycoreutils-python

引用自:https://unix.stackexchange.com/questions/369570