duangan9251
2015-01-31 11:45
浏览 129
已采纳

从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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douguadao3883 2015-01-31 12:20
    已采纳

    check selinux is enabled

    selinuxenabled && echo enabled || echo disabled
    

    if it is enabled, try to disable

    echo 0 > /selinux/enforce
    
    点赞 打赏 评论

相关推荐 更多相似问题