【发布时间】:2015-01-31 11:45:58
【问题描述】:
从 Apache 网络服务器运行时,我无法让 PHP 将文件写入文件系统。
我有一个简单的 PHP 脚本:
<?php
print 'User : '.posix_getpwuid(posix_getuid())['name'];
print ' ';
print 'Group: '.posix_getgrgid(posix_getgid())['name'];
file_put_contents('./test.txt', 'OK');
?>
我以用户 ec2-user:ec2-user 登录,只是为了测试 Apache 以 ec2-user:ec2-user 运行。
ec2-user 属于以下组:
>groups
ec2-user adm wheel systemd-journal www
脚本位于 Apache 文档根目录中。
/var/www/html/test.php
drwxr-xr-x. 21 root root 4096 ene 31 05:45 var
drwxrwsr-x. 4 root www 31 ene 29 17:30 www
drwxrwsr-x. 2 root www 36 ene 31 06:16 html
-rw-rw-r--. 1 ec2-user www 172 ene 31 06:15 test.php
如果通过 PHP cli 运行脚本,则会创建文件 test.txt 并生成以下输出。
>php ./test.php
User : ec2-user Group: ec2-user
但是,如果我通过浏览器将脚本作为普通网页或通过 curl 调用,则会收到文件权限错误:
> curl http://my.ip/test.php
User : ec2-user Group: ec2-user<br />
<b>Warning</b>: file_put_contents(./test.txt): failed to open stream: Permission denied in <b>/var/www/html/test.php</b> on line <b>5</b><br />
我也尝试以ec2-user:www 运行 Apache,但输出相同:
User : ec2-user Group: www
Warning: file_put_contents(./test.txt): failed to open stream: Permission denied in /var/www/html/test.php on line 6
我检查了 PHP 配置,没有配置 open_basedir 选项。
我尝试使用777 权限和相同的输出写入/dummy 文件夹。
我缺少什么配置吗?
【问题讨论】:
-
我没有解决方案,但我假设您在 AWS 上,所以不要忘记在您的问题中添加
aws标签。它与此有关。
标签: php apache amazon-web-services