Ubuntu – permissions – allow two users full access to directory

Apache2command linepermissionsPHPserver

I have a web server directory located at /var/www/web. Inside one of the files, let's say create_dir.php, I'm creating a directory with mkdir(). I'm getting the following message.

Warning: mkdir(): Permission denied in /var/www/web/create_dir.php on line 122

Now, I can run this file when I am the owner of the file, or directory. But, when I am not the owner, I cannot edit the files inside PhpStorm.

My user is called josh and the apache2 user is the default, www-data.

I essentially need to make a group, or a special/magic user that will allow me to run and edit the files whenever.

Note: I have looked at this superuser answer and it did not work for me. I ran this command to create the group:

sudo groupadd website

I ran these commands to add the users:

sudo usermod -a -G website josh
sudo usermod -a -G website www-data

I ran this command to add it to the web folder:

sudo chgrp -R website /var/www/web

I ran this command and once I reloaded my website, I got a Forbidden error message.

sudo chmod -R 770 /var/www/web

So I ran this command to be able to view the webpage:

sudo chmod -R 775 /var/www/web

And now I'm back to square one.

Any help is appreciated.

Best Answer

Shared directories in Linux can be confusing due to ownership and permissions.

For this specific directory, /var/www/html/ there is already an answer here

How to avoid using sudo when working in /var/www?

So make both users a member of www-data. IMHO no need to make a new group.

To manage permissions and file ownership, set the group and setGID

sudo chgrp -R www-data /var/www/html
sudo chmod 2770 /var/www/html 

For details see https://www.gnu.org/software/coreutils/manual/html_node/Directory-Setuid-and-Setgid.html

... if a directory’s set-group-ID bit is set, newly created subfiles inherit the same group as the directory ...

Restart your shell or log out and back in for membership to take effect.

If you need finer grain of control you can use ACL, but that is probably overkill.

For additional information on ACL see https://help.ubuntu.com/community/FilePermissionsACLs and http://brunogirin.blogspot.com/2010/03/shared-folders-in-ubuntu-with-setgid.html

That second link specifically addresses this exact question, how to share /var/www/html, using setGID and ACL.

Related Question