How-To chown and chmod
http://www.computerhope.com/unix/uchmod.htm
Everyone experiencing this issue should execute these commands: sudo usermod -aG www-data $USER Adds the currently logged in user to the www-data group. sudo chown -R www-data:www-data /var/www Changes the ownership of the /var/www directory to www-data group. sudo chmod -R 774 /var/www sudo adduser username www-data sudo chown -R www-data:www-data /var/www sudo chmod -R g+rw /var/www
Use this knowledge to setup Apache Assumptions: Apache is run as user www-data and group www-data. Server web root is /var/www
First
We need to set the owner/group of the web root (and any directories/files therein): $ sudo chown -R www-data:www-data /var/www
Second
We need to setup the proper permissions for users and groups. We do some blanket commands restricting access, and then open access up as much as we need to. To start, make it so no-one but the current user (www-data) can access the web-root content. We use ‘go’, meaning apply to ‘group’ and ‘other’. We use ‘-‘, which means remove permissions. We use ‘rwx’ to remove read, write and execute permissions. $ chmod go-rwx /var/www Next, allow users of the same group (and ‘other’) to enter the /var/www directory. This is not done recursively. Once again, we use ‘group’ and ‘other’ but we use ‘+’ to allow the execute (‘x’) permission. $ chmod go+x /var/www Next, change all directories and files in the web root to the same group (www-data) - just in case there are files in there currently: $ chgrp -R www-data /var/www Next, let’s do another “reset” of sorts - Make it so only the user can access web content: $ chmod -R go-rwx /var/www And finally, make it so anyone in the same group can ready/write and execute directories/files in the web root. $ chmod -R g+rx /var/www I actually give group write permissions as well, for users which need to modify content, such as users used to deploy code. That looks like this: $ chmod -R g+rwx /var/www Often going through all of these steps isn’t necessary, but this is a useful exercise to see how these commands work!
- Add the www-data group to an existing user sudo usermod -a -G www-data username OR if the user doesn’t exist: Create a new user and assign them the www-data group sudo adduser username www-data
- Make sure all files are owned by the www-data group and user (cleans up the ownership) sudo chown -R www-data:www-data /var/www
- Enable all members of the www-data group to read and write files sudo chmod -R g+rw /var/www
- Note: You are done. But if you want all future files created in this directory to keep the current setup do the following as well:
- This is what I do to ensure that all files created keep the current user and permissions (it’s really lame to create new files, say from Git, and then have to update the user, groups and permissions of the new files every time to ensure they can be run by the server) sudo chmod -R g+rws /var/www
- Final Note: For security reasons it may be better to keep /var/www owned by root:root (depending on what you are doing) If you want to keep /var/www owned by root replace step 2 with *sudo chgrp -R www-data /var/www/ **