权限被拒绝:httpd:无法打开错误日志文件 /etc/httpd/logs/error_log


Permission denied: httpd: could not open error log file /etc/httpd/logs/error_log

当我想在 centOS 6.7 上重新启动 httpd searvice 时,我遇到以下错误:

/etc/init.d/httpd restart
Stopping httpd:                                            [FAILED]
Starting httpd: (13)Permission denied: httpd: could not open error log file /etc/httpd/logs/error_log.
Unable to open logs
                                                           [FAILED]

这是error_log:

ls -Z /etc/httpd/logs/error_log
-rw-r--r--. root root unconfined_u:object_r:var_t:s0   /etc/httpd/logs/error_log

我也禁用了 selinux。

问题出在哪里?

httpd可能以用户apache或用户httpd身份运行。您的日志归您所有,只能由 root 写入。更改日志文件的所有权以完成此操作。

这应该可以解决问题:

~# chown apache:apache /etc/httpd/logs/error_log  

也许你应该将该 forder 的组更改为 apache,不建议将 root 作为服务器内容的所有者。无论如何,apache 应该在 http 启动后自己改变它......

来自httpd Wiki:

在开始之前,我们需要注意 Apache HTTP 服务器 (httpd) 以特定用户和组身份运行。

在Linux以及大多数其他类Unix系统上,httpd的启动方式为。 "根"用户;UID=root,GID=root。这是必要的,因为只有 此用户可以绑定到端口 80 和 443(实际上低于 1024 的任何内容)。

在 http 启动并绑定到其端口之后(由 httpd.conf 中的语句),它将用户更改为 httpd.conf. 通常:

用户: 阿帕奇
组: 阿帕奇

请注意,基于 Debian 的系统,包括 Ubuntu,改用"www-data"。

作为可能的解决方案,您应该将自己添加到 apache 组中

usermod -a -G apache (username)

然后:

chgrp apache (folderPath)
chmod g+rwxs (folderPath)  

反正这很奇怪...告诉我这是否解决了您的问题,如果没有,只要您向我提供更多信息,我就会对其进行编辑:)

就我而言,我已经完成了以下操作,能够启动httpd服务而不会出现任何错误。

错误:

# sudo tail -f /etc/httpd/logs/error_log
Sep 27 09:09:19 MASTERREPOWEB systemd[1]: Starting The Apache HTTP Server...
Sep 27 09:09:20 MASTERREPOWEB httpd[33693]: (13)Permission denied: AH00091: httpd: could not open error log file /etc/httpd/logs/error_log.
Sep 27 09:09:20 MASTERREPOWEB httpd[33693]: AH00015: Unable to open logs
Sep 27 09:09:20 MASTERREPOWEB systemd[1]: httpd.service: Main process exited, code=exited, status=1/FAILURE

溶液:

ls -lZ /etc/httpd/logs/error_log
sudo chcon -Rv system_u:object_r:httpd_log_t:s0 /etc/httpd/logs/
sudo systemctl restart httpd
sudo systemctl status httpd

在 CentOS 7 中

尝试禁用 SElinux,然后重新启动,然后重新启动

systemctl restart httpd

注意:永远不要禁用 SElinux。如果问题解决了,则启用(强制执行)它,然后在SElinux中启用httpd

https://www.serverlab.ca/tutorials/linux/web-servers-linux/configuring-selinux-policies-for-apache-web-servers/

如果您使用的是 RedHat/Fedora/CentOS/RockyLinux,请尝试执行以下命令(14:00错误发生后您的时间):

journalctl -t setroubleshoot --since=14:00

您可能会看到以下错误:

Jun 25 14:21:33 fedora setroubleshoot[1609]: SELinux is preventing httpd from open access on the file /var/log/gitea/error_log. For complete SELinux messages run: sealert -l 5e2bf467-e935-4fa7-a7fd-d0f7f257828e
Jun 25 14:21:33 fedora setroubleshoot[1609]: SELinux is preventing httpd from open access on the file /var/log/gitea/error_log.
                                             *****  Plugin catchall (100. confidence) suggests   **************************
                                             If you believe that httpd should be allowed open access on the error_log file by default.
                                             Then you should report this as a bug.
                                             You can generate a local policy module to allow this access.
                                             Do
                                             allow this access for now by executing:
                                             # ausearch -c 'httpd' --raw | audit2allow -M my-httpd
                                             # semodule -X 300 -i my-httpd.pp

这意味着缺少权限与chmod无关,而是与SELinux有关。

为了解决这个问题,你需要执行以下命令(请相应地调整日志目录):

semanage fcontext -a -t httpd_sys_rw_content_t '/var/log/gitea(/.*)?'
restorecon -Rv '/var/log/gitea/'
systemctl restart httpd

错误应该消失。并且请不要永久关闭SELinux(正如有人可能建议的那样),因为您的服务器可能会受到损害。

只需以超级用户身份运行它:

service httpd start

启动 httpd: (13)权限被拒绝: httpd: 无法打开错误日志文件/etc/httpd/logs/error_log。

无法打开日志 [失败]

sudo service httpd start

启动 httpd: [ 确定 ]