David Casillas
David Casillas

Reputation: 1911

PHP running from Apache cannot write to filesystem

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?

Upvotes: 3

Views: 846

Answers (1)

miholeus
miholeus

Reputation: 1597

check selinux is enabled

selinuxenabled && echo enabled || echo disabled

if it is enabled, try to disable

echo 0 > /selinux/enforce

Upvotes: 3

Related Questions