The default user in a Ubuntu system is named ubuntu. On the other hand www-data is the user that web servers on Ubuntu (Apache, nginx, for example) use by default for normal operation. It also means that the web server process can access any file that www-data can access.
Due to the difference in name and group access the default ubuntu user cannot perform file write operation in the webroot folder or the folders which require www-data ownership. For example ubuntu user cannot pull changes from a git repository to web root folder.
This is the default behaviour in an Ubuntu system and we do not need to worry about it. However in some cased you may want to do file create or write operations on files and folders under your web root folder, so here we are.
In this simple tutorial we will give ubuntu user access to www-data group so that it can do file write operations on these folders.
Adding ubuntu user to www-data group
This example demonstrate how to give
ubuntu user the privilege to edit and add files in the
ubuntu user to
sudo usermod -a -G www-data ubuntu
Optional – if user (user with any other name) does not exist do this additional step to add user:
sudo adduser [username] www-data
Important step: Make sure all files are owned by the www-data group and user (refreshes the ownership settings)
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
Important: Upto this step, you should be all set to perform file write operations such as
git pull as user
ubuntu. But, probably, you would want all files created in this directory in future to listen to the current ownership settings. If you do not do it, you will have to set file ownership manually everytime you create new file or folder under your project root.
To enable automated permission granting, run the following:
sudo chmod -R g+rws /var/www
Logout and login back as user ubuntu to see new permissions in effect.
In this simple tutorial we have learned how to add ubuntu user to www-data group to fix the file the write permission errors occurring due to the mismatch between name and permissions of current user ubuntu and web server group www-data.