从Apache运行的PHP无法写入文件系统

I cannot make PHP write a file to filesystem when running from Apache webserver.

I have a simple PHP script:

<?php
print 'User : '.posix_getpwuid(posix_getuid())['name'];
print ' ';
print 'Group: '.posix_getgrgid(posix_getgid())['name'];
file_put_contents('./test.txt', 'OK');
?>

I'm logged in as user ec2-user:ec2-user and just for testing Apache is running as ec2-user:ec2-user.

ec2-user belongs to the following groups:

>groups
ec2-user adm wheel systemd-journal www

The script is located in Apache document root.

/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

If a run the script vis PHP cli the file test.txt is created and the following output is generated.

>php ./test.php
User : ec2-user Group: ec2-user

But if I call the script via my browser as a normal web page or via curl, I get a file permissions error:

> 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 />

I have tried also to run Apache as ec2-user:www, but the output is the same:

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

I have checked PHP configuration and there is no open_basedir option configured.

I have tried to write to a /dummy folder with 777 permissions and the same output.

Is there any configuration I'm missing?

dongzhi6927
dongzhi6927 serverfault.com/questions/293143/...
5 年多之前 回复
duanjucong3124
duanjucong3124 我没有解决方案但你在AWS上我假设,所以不要忘记在你的问题中添加aws标签。它与某种程度上有关。
5 年多之前 回复

1个回答

check selinux is enabled

selinuxenabled && echo enabled || echo disabled

if it is enabled, try to disable

echo 0 > /selinux/enforce
dousong5161
dousong5161 就是这样。 谢谢。 您还可以使用getenforce检查SeLinux状态。 我在/ etc / selinux / config和rebooting中禁用它编辑SELINUX = disabled。 access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/...
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐