PHP is_writable() 函数对于可写目录总是 returns false

PHP is_writable() function always returns false for a writable directory

我正在尝试在具有 Apache 2.4.6 和 PHP 的 Red Hat 7 Amazon EC2 实例 (ami-8cff51fb) 中安装基于 PHP 的 software package 5.4.16用yum安装就可以了。安装失败,因为它说特定目录需要由具有 0755 或 0775 权限的网络服务器写入。

有问题的目录具有 0775 权限和 root:apache 所有权。我已经验证 httpd 进程正在被 apache 用户 运行 并且 apache 用户是 apache 组的成员。

如果我编辑 /etc/passwd 暂时给 apache 用户一个登录名 shell 然后 su 到该帐户,我可以在使用 touch 命令的目录。

我查看了安装程序脚本的源代码,发现它失败了,因为 PHP 的 is_writable() 函数为相关目录返回 false。我创建了一个单独的测试 PHP 脚本来隔离和验证我看到的行为:

<?php
  $dir = '/var/www/html/limesurvey/tmp';
  if (is_writable($dir)) {
    echo $dir, ' is writable';
  } else {
    echo $dir, ' is NOT writable';
  }
?>

这会输出 NOT writable 消息。如果我将上面的 $dir 更改为 /tmp 那么它会正确输出 /tmp 是可写的。

如果我将目录权限更改为 0777 and/or 将所有权更改为 apache:apache,那么 PHP 仍然报告该目录不可写。我什至尝试创建一个设置了相同权限和所有权的 /test 目录,但我的测试脚本仍然报告它不可写。

我真的不知道如何解释这种行为,所以欢迎任何想法!

提前致谢。


/var/www/html/limesurvey 的目录列表如下。根据 Lime Survey 的 installation instructionstmpupload 目录具有 0775 权限。 test.php就是我上面说的测试脚本

[ec2-user@ip-xx-x-x-xxx limesurvey]$ pwd
/var/www/html/limesurvey
[ec2-user@ip-xx-x-x-xxx limesurvey]$ ls -al
total 80
drwxr-xr-x. 20 root apache 4096 Mar 30 11:25 .
drwxr-xr-x.  3 root root     23 Mar 25 14:41 ..
drwxr-xr-x.  2 root apache   38 Mar 10 12:56 admin
drwxr-xr-x. 16 root apache 4096 Mar 10 12:56 application
drwxr-xr-x.  3 root apache 4096 Mar 10 12:56 docs
drwxr-xr-x.  2 root apache 4096 Mar 10 12:56 fonts
drwxr-xr-x. 19 root apache 4096 Mar 10 12:56 framework
-rw-r--r--.  1 root apache  429 Mar 10 12:56 .gitattributes
-rw-r--r--.  1 root apache  399 Mar 10 12:56 .gitignore
-rw-r--r--.  1 root apache  296 Mar 10 12:56 .htaccess
drwxr-xr-x.  4 root apache 4096 Mar 10 12:56 images
-rw-r--r--.  1 root apache 6652 Mar 10 12:56 index.php
drwxr-xr-x.  5 root apache   39 Mar 10 12:56 installer
drwxr-xr-x. 89 root apache 4096 Mar 10 12:56 locale
drwxrwxr-x.  2 root apache   39 Mar 25 14:41 logs
drwxr-xr-x.  4 root apache   49 Mar 10 12:56 plugins
-rw-r--r--.  1 root apache   61 Mar 10 12:56 README
drwxr-xr-x.  4 root apache 4096 Mar 10 12:56 scripts
-rw-r--r--.  1 root apache  380 Mar 10 12:56 .scrutinizer.yml
drwxr-xr-x.  5 root apache 4096 Mar 10 12:56 styles
drwxr-xr-x.  5 root apache 4096 Mar 10 12:56 styles-public
drwxr-xr-x. 12 root apache 4096 Mar 10 12:56 templates
-rw-r--r--.  1 root apache  159 Mar 30 11:11 test.php
drwxr-xr-x.  3 root apache   20 Mar 10 12:56 themes
drwxr-xr-x. 26 root apache 4096 Mar 10 12:56 third_party
drwxrwxr-x.  5 root apache   80 Mar 26 13:45 tmp
drwxrwxr-x.  6 root apache   79 Mar 10 12:57 upload

运行 namei -l /var/www/html/limesurvey/tmp 给出:

[ec2-user@ip-x-x-x-xxx ~]$ namei -l /var/www/html/limesurvey/tmp
f: /var/www/html/limesurvey/tmp
drwxr-xr-x root root   /
drwxr-xr-x root root   var
drwxr-xr-x root root   www
drwxr-xr-x root root   html
drwxr-xr-x root apache limesurvey
drwxrwxr-x root apache tmp

要写入目录,您还需要对上述目录的执行权限。

namei -l /var/www/html/limesurvey/tmp

应该显示您没有正确权限的步骤。

HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1`
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX tmp
sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX tmp

直接取自 Symfony2 安装指南,这解决了 Apache 和 CLI 工具之间缓存写访问共享的问题。这也适用于您的 tmp 目录。

is_writable 默认只检查用户,不检查组。 因此,即使您的组匹配并且具有权限 is_writable 也会 return 为假。 要放宽此检查,您需要设置

safe_mode_gid = On

在 PHP 配置中或相应地更改用户。

经过一番摸索之后,发现是 SELinux 阻止了目录被写入。我找到了 good tutorial that explains what's going on。我能够通过 运行 这个命令修复它:

sudo chcon -R -t httpd_sys_rw_content_t tmp

CentOS 6上面应该是SELinux enable enforcing

setenforce Permissive

查看状态

sestatus

参考https://wiki.centos.org/HowTos/SELinux