Skip to content

Instantly share code, notes, and snippets.

@stefanbc
Last active Sep 24, 2021
Embed
What would you like to do?
Set proper permissions on /var/www/

HOWTO

To set up permissions on /var/www where your files are served from by default:

sudo addgroup webmasters
sudo adduser $USER webmasters
sudo chown -R root:webmasters /var/www
sudo find /var/www -type f -exec chmod 664 {} \;
sudo find /var/www -type d -exec chmod 775 {} \;
sudo find /var/www -type d -exec chmod g+s {} \;
sudo chown -R www-data:webmasters application/cache/ [etc...]

Now log out and log in again to make the changes take hold.

The previous set of commands does the following:

  • Create a new group called webmasters; all users who need write access to the app files will be added to this group.
  • adds the current user ($USER) to the webmasters group.
  • changes the owner of /var/www to root and the group to webmasters group.
  • adds 644 permissions (-rw-rw-r--) to all files in /var/www.
  • adds 775 permissions (drwxrwxr-x) to all directories in /var/www.
  • sets the SGID bit on /var/www and all directories therein; this final point bears some explaining. Note also that you can also put a 2 at the front of your chmod octal (e.g. 2644) to do the same thing. sets the owner to www-data (Apache's user) and group of the supplied directory to webmaster. This ensures the directory is writable by Apache and anyone in the webmasters group. Do the same for all other directories that need to be writable.
@jyarali

This comment has been minimized.

Copy link

@jyarali jyarali commented Nov 28, 2018

adds 644 permissions (-rw-rw-r--) to all files in /var/www. => adds 664

@StanleyMasinde

This comment has been minimized.

Copy link

@StanleyMasinde StanleyMasinde commented Oct 3, 2019

Just curious why don't you just sudo chown -R www-data:webmasters /var/www instead of sudo chown -R root:webmasters /var/www?

@StanleyMasinde

This comment has been minimized.

Copy link

@StanleyMasinde StanleyMasinde commented Oct 7, 2019

for some reason sudo find /var/www -type f -exec chmod 664 {} \; does not work in ubuntu 18.04

@bkt1916

This comment has been minimized.

Copy link

@bkt1916 bkt1916 commented Apr 10, 2020

sudo chown -R www-data:webmasters application/cache/ [etc...] what You mean by that? What is application/cache? And how it works on centos?

@StanleyMasinde

This comment has been minimized.

Copy link

@StanleyMasinde StanleyMasinde commented Apr 10, 2020

sudo chown -R www-data:webmasters application/cache/ [etc...] what You mean by that? What is application/cache? And how it works on centos?

These are the special folders in your application that need to be written by the server. example storage/app/logs in laravel

@Nditah

This comment has been minimized.

Copy link

@Nditah Nditah commented Oct 26, 2020

You can make the currrent $USER the owner of that directory

sudo chown -R $(whoami):$(whoami) /var/www

set the appropriate permissions

chmod 755 -R /var/www/html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment