【问题标题】:php fopen failed to open stream: Permission deniedphp fopen 无法打开流:权限被拒绝
【发布时间】:2011-11-15 14:56:27
【问题描述】:

我在使用 php 写入文本文件时遇到问题。这听起来可能很简单,但我已将文件所有者和组设置为 apache/root,权限为 777,但我仍然无法写入文件。我正在使用 php 5.3.8 运行 centos。

====================
New info
====================

semanage fcontext -l | grep httpd | grep rw
/var/lib/drupal(/.*)?                              all files          system_u:object_r:httpd_sys_script_rw_t:s0 
/var/spool/gosa(/.*)?                              all files          system_u:object_r:httpd_sys_script_rw_t:s0 
/var/lib/bugzilla(/.*)?                            all files          system_u:object_r:httpd_bugzilla_script_rw_t:s0 
/var/spool/viewvc(/.*)?                            all files          system_u:object_r:httpd_sys_script_rw_t:s0 

【问题讨论】:

  • SELinux 很可能会阻止文件写入,如果包含它的目录没有 rw 上下文。
  • 您可能需要chcon -t httpd_sys_rw_content_t /path/to/file
  • 我已将包含文件的目录设置为 777 和 root/apache.. 但仍然无法正常工作
  • 为了测试这个理论,以 root 身份运行 setenforce 0 然后尝试运行脚本。如果成功,SELinux 就是问题所在。用setenforce 1重新打开它
  • 是的,设置为 0 后它可以工作。我做了以下操作并得到以下错误:chcon -t httpd_sys_rw_content_t test.txt chcon: failed to change context of test.txt to root:object_r:httpd_sys_rw_content_t : 无效参数

标签: php fopen


【解决方案1】:

为了允许目录为 r/w,我使用chcon 命令将httpd_sys_script_rw_t 类型添加到目录及其下的任何内容:

chcon -R -t httpd_sys_script_rw_t <directory>

-R 标志使命令递归。

-t 标志将文件的扩展属性设置为指定的文件上下文。在这种情况下,httpd 文件上下文 httpd_sys_script_rw_t 在以下情况下使用:

您希望 httpd_sys_script_exec_t 脚本读取/写入数据,并禁止其他非 sys 脚本访问。

【讨论】:

    【解决方案2】:

    httpd_selinux(8) 手册页中描述了用于允许 HTTPd 写入磁盘的适当文件上下文(以及用于其他操作的布尔值)。

    【讨论】:

      猜你喜欢
      • 2016-02-16
      • 2015-02-01
      • 1970-01-01
      • 2018-07-23
      • 2019-05-02
      • 2016-06-13
      • 2015-11-15
      • 1970-01-01
      • 2013-03-21
      相关资源
      最近更新 更多