【问题标题】:httpd (apache) centos fail to open stream: Permission deniedhttpd(apache)centos无法打开流:权限被拒绝
【发布时间】:2018-03-23 21:51:54
【问题描述】:

我在 centos 7 服务器中遇到下一个错误

我查看了类似的问题,说这是因为 SELinux 不允许 httpd 写入我的 /home 文件夹,我尝试更改文件夹的所有者但没有成功;尝试将上下文 (chcon) 更改为我的 /home 的 httpd_sys_rw_content_t 并出现相同的错误;尝试禁用 SELinux,错误仍然存​​在;并在文件httpd.conf 中将UserGroupapache 更改为test 这也不起作用。我的服务器是:

LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.4.1708 (Core) 
Release:        7.4.1708
Codename:       Core

Linux localhost 3.10.0-693.17.1.el7.x86_64 #1 SMP Thu Jan 25 20:13:58 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

当我以用户 test 的身份从 php -a 执行 move_uploaded_file() 时,它正常工作,我发现问题出在用户 apache

【问题讨论】:

  • /home/test 和 /home 的权限是什么?
  • 您是否尝试过 chown -R test:apache /home/test 然后在 /home/test 上授予 750 权限?
  • 嗨@jawbonewalk,感谢我尝试chown -R test:apache /home/test 并授予750 文件夹/home/test 权限的评论,但我看到了同样的错误。我的/home/test 的权限是 700/home 755
  • 您是否将 /etc/httpd/conf/httpd.conf 更改为指向您的文件夹?
  • 是的,我在我的 httpd.conf 中添加了我的文件夹,但我终于找到了一个解决方案,它是关于文件夹 /home/test/images/ 和上层文件夹 /home/test 的所有者,因为所有者必须是apache 和小组也是,感谢@jawbonewalk 的指导

标签: apache centos7 php-5.4


【解决方案1】:

TLDR:

不要运行setenforce 0 命令,这将禁用 SELinux!出于安全原因,您不应禁用 SELinux。

解决办法:

您应该更新策略以使 SELinux 允许对特定目录进行读写:

允许apcahe读写。

chcon -R -t httpd_sys_rw_content_t /path/your_writabl_dir

对于只读目录:

chcon -R -t httpd_sys_content_t /path/yourdir

例如,您可以将您的 public(文档根)目录设为只读,并且只允许在您允许的目录上写入您的应用可以写入:

# Make all read only
chcon -R -t httpd_sys_content_t /var/www/myapp

# Only allow write on uploads dir for example
chcon -R -t httpd_sys_rw_content_t /var/www/myapp/public/uploads

【讨论】:

  • 解决方案对我有用,但它最大限度地降低了安全性。如果我们能有什么,那就更好了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-29
  • 2011-06-22
  • 2015-12-16
  • 2015-11-15
  • 2017-09-23
  • 2015-02-01
相关资源
最近更新 更多