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

从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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?