# failed to open stream: Permission denied



## steveh627 (May 31, 2015)

i use a VPS through digitalocean and currently run 
fedora 21x64. I've installed the lamp stack as a fresh install since I've been stuck for 2 weeks trying different ideas I was afraid I messed up too much internally. 

I'm trying to run the following code under /var/www/html called makefile.php

<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Bobby Bopper\n";
fwrite($fh, $stringData);
$stringData = "Tracy Tanner\n";
fwrite($fh, $stringData);
fclose($fh);

?>


I can execute the command line version of the php file(su apache , cd to dir and running code). It does end up working. However when I try it online through the browser I still get the permission denied in the logs. 
Current group/owner is root:root for 
/var/
Current group/owner is apache:apache for 

/var/www 
/var/www/html .

I've used

get_current_user(); 
current script owner is apache

whoami 

apache

I placed the same file in all directories and it worked under the command line version. 
/var/makefile.php 
/var/www/makefile.php 
/var/www/html/makefile.php

The oddest thing is, i tried a work around where I told the php script to write to 
/var/orders (who's current directory owner is root and it created a text file with the owner of the text file bring apache:apache) using the same fopen commands. 
I have since changed the owner/group to apache:apache and nothing has changed, it can save text into /var/orders but it can't go to the www folders. Here is the permissions under ls -la and acl

getfacl /var/ 
getfacl: Removing leading '/' from absolute path names 
file:var 
owner:root 
group:root 
user::rwx 
group::rwx 
other::rwx

getfacl /var/orders 
getfacl: Removing leading '/' from absolute path names 
file:var/orders 
owner:apache 
group:apache 
user::rwx 
group:rwx 
other:rwx

getfacl /var/www 
getfacl: Removing leading '/' from absolute path names 
file:var/www 
owner:apache 
group:apache 
user::rwx 
group:rwx 
other:rwx

getfacl /var/www/html 
getfacl: Removing leading '/' from absolute path names 
file:var/www/html 
owner:apache 
group:apache 
user::rwx 
group:rwx 
other:rwx

(for folder var) 
ls -la 
drwxrwxrwx 20 root root 4096 May 31 19:35 var

(for folder /var/www & /var/orders) 
ls -la 
drwxrwxrwx 4 apache apache 4096 May 31 19:33 www 
drwxrwxrwx 4 apache apache 4096 May 31 19:38 orders

I'm at a loss as to why it won't execute to create a file under /var/www/ or /var/www/html but only /var/orders even though it looks like they have they same exact permissions.


----------



## Fjandr (Sep 26, 2012)

Do you have PHP error reporting enabled?

Try using error_get_last() or (if you are using a version < 5.2) track_errors to perhaps get a better idea of exactly what is going wrong with your permissions.


----------



## wmorri (May 29, 2008)

Also I would like to see if you php in running or not. Create a file called info.php and post the following


```
<?php info(); ?>
```
Then we can make sure that php in enabled and running.


----------

