syntax highlighter

Saturday, January 7, 2012

Changing Permissions of /var/www folder in ubuntu

After setting up LAMP in Ubuntu we are ready to develop our own webpage but there is just one more step before we continue. By default a user is not allowed to access the var/www folder under the root and has to have super user privileges for the same. So we have to change the permissions of this folder and all of its sub folders to create,read,write and execute files.We do this by the following method

Step 1 :- Create a new group (www-pub) and yourself to that group

sudo groupadd www-pub
sudo usermod -a -G www-pub usera

Here "usera" is your user name. You must use -a to append to existing groups

Now display groups for a user (optional)

sudo groups usera

Step 2 :- Change the ownership of everything under /var/www to root:www-pub

sudo chown -R root:www-pub /var/www

Step 3 :- Change the permissions of all the folders to 2775

sudo chmod 2775 /var/www

2=set group id, 7=rwx for owner (root), 7=rwx for group (www-pub), 5=rx for world (including apache www-data user)
Set group ID (SETGID) bit (2) causes the group (www-pub) to be copied to all new files/folders created in that folder. Other options are SETUID (4) to copy the user id, and STICKY (1) which I think lets only the owner delete files.

Step 4:- There's a -R recursive option, but that won't discriminate between files and folders, so you have to use find, like

sudo find /var/www -type d -exec chmod 2775 {} \;

Step 5 :- Change all the files to 0664

sudo find /var/www -type f -exec chmod 0664 {} \;

And now we are done. Now we can easily create and edit files in the www folder and all of its sub folders.

1 comment: