What permissions do I need to give for the server setup

apachepermissionPHP

I am trying to have a server setup for my web app locally on my MacBook Pro. I have installed Apache, PHP successfully and checked out my web app's code to /var/www location. Now there's a script in my app that writes a file in /var/www/someDir. To do so I am using the following line:

$file = fopen($_SERVER['DOCUMENT_ROOT']."/abc.ini", "w");

And on execution I see the following error in error log.


PHP Warning: fopen(/var/www/someDir/abc.ini): failed to open stream: Permission denied in /private/var/www/myscript.php on line 6

someDir is owned by user abhilash.goje (that's me) and group wheel. I have tried giving following permissions.

$sudo chmod -R 755 www     -> Did not resolve the issue
$sudo chmod -R 775 www     -> Did not resolve the issue
$sudo chmod -R 777 www     -> Resolved the issue, but not safe.

permission 777 works but I don't prefer to use this solution as it is not at all safe.
Kindly let me how do I set the correct permissions to this folder.

Thanks in Advance!!

Best Answer

Apache is the process that needs to have appropriate permissions to access /var/www. Apache is typically user _www of group _www on MacOS. Verify this with:

 grep -e '^Group\|^User' /etc/apache2/httpd.conf

Two lines should be printed. My output looks like:

User _www
Group _www

This means that on my system, Apache processes run as user _www with group _www. If you're using another configuration, check the appropriate config file for the same parameters and replace _www in the following solution if yours differs.

  1. Change the group of /var/www with: chgrp -R _www /var/www
  2. Change the permissions to add group write with: chmod -R 775 /var/www

That should fix the error.

As an extra step, you can change the ownership of /var/www to _www and then add yourself to that group to maintain your read and write privileges.