Amazon Web Services, Server version: Apache/2.4.6 (Red Hat Enterprise Linux)

The apache log reports:

[Fri Aug 07 13:53:40.793562 2015] [:error] [pid 10730] [client] PHP Warning:  file_put_contents(./test.txt): failed to open stream: Permission denied in /var/www/html/vraim/index.php on line 18

Here is PHP code which tries to write to the current directory:

file_put_contents("./test.txt", "Test"); // <=== This cannot write!!!
$uid = posix_getuid();
echo "PosixGetUID: " . $uid . "<br/>";
$userinfo = posix_getpwuid($uid);

The permissions on the directory:

# namei -movl /var/www/html/vraim/
f: /var/www/html/vraim/
dr-xr-xr-x root   root /
drwxr-xr-x root   root var
drwxr-xr-x root   root www
drwxr-xr-x apache root html
drwxrwxrwx apache root vraim

The httpd.conf for the virtual host:

DocumentRoot /var/www/html/vraim
<Directory />
   Require all granted

What do I do wrong? Why PHP cannot write to the folder which is in ownership of Apache?

PHP script itself outputs the following info about its effective user id:

PosixGetUID: 48
Array ( [name] => apache [passwd] => x [uid] => 48 [gid] => 48 [gecos] => Apache [dir] => /usr/share/httpd [shell] => /sbin/nologin )

1 Answer 1


could come from selinux, try to set it to permissive to check:

setenforce permissive

this could happen when you mv something from one place to another. It preserves the selinux context of the original when you move it

  • N.B: set to permissive just to check if selinux is the problem, I do not recommend to let it to permissive then. try "restorecon -r /var/www/html" to restore the security context
    – Tom
    Aug 8, 2015 at 8:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .