Amazon Web Services,服务器版本:Apache/2.4.6 (Red Hat Enterprise Linux)
apache日志报告:
[Fri Aug 07 13:53:40.793562 2015] [:error] [pid 10730] [client 149.88.114.40:40800] PHP Warning: file_put_contents(./test.txt): failed to open stream: Permission denied in /var/www/html/vraim/index.php on line 18
这是尝试写入当前目录的 PHP 代码:
<?php
file_put_contents("./test.txt", "Test"); // <=== This cannot write!!!
$uid = posix_getuid();
echo "PosixGetUID: " . $uid . "<br/>";
$userinfo = posix_getpwuid($uid);
print_r($userinfo);
?>
目录权限:
# namei -movl /var/www/html/vraim/
f: /var/www/html/vraim/
dr-xr-xr-x root root /
drwxr-xr-x root root var
drwxr-xr-x root root www
drwxr-xr-x apache root html
drwxrwxrwx apache root vraim
虚拟主机的 httpd.conf:
DocumentRoot /var/www/html/vraim
<Directory />
Require all granted
</Directory>
我做错了什么?为什么 PHP 不能写入 Apache 拥有的文件夹?
PHP 脚本本身会输出以下有关其有效用户 ID 的信息:
PosixGetUID: 48
Array ( [name] => apache [passwd] => x [uid] => 48 [gid] => 48 [gecos] => Apache [dir] => /usr/share/httpd [shell] => /sbin/nologin )
可能来自selinux,尝试将其设置为允许检查:
当您将某物从一个地方移动到另一个地方时,可能会发生这种情况。当您移动它时,它会保留原始的 selinux 上下文