How to add new user to www-data group in Ubuntu

In some of the customised Ubuntu systems, such as Amazon EC2 instance, the name of the default system user is ubuntu.

On the other hand www-data is the user and group name that web servers on Ubuntu (Apache, nginx, for example) use by default for normal web server operations. It also means that the web server processes can access any file that www-data can access.

NOTE: This tutorial applies to any new user you may have created so read follow this tutorial irrespective of the name ubuntu I am using here.

add ubuntu user to www-data group

Due to the difference in name and group access the default ubuntu and any other user may not be able to perform file write operations inside your web root folder or the folders which require www-data ownership rights. For example the ubuntu user cannot pull changes from a github repository to your web root folder.

This is default behavior in an Ubuntu system and we do not need to worry about it. However in some cases, you may wish to do some file create or write operations inside your webroot folder. So here we are.

In this simple tutorial we will give ubuntu user (or any other user you create) access to www-data group so that the new user could do file write operations on folders under web root directory.

Optional – if user (user with any other name) does not exist do this additional step to add user:

sudo adduser [username] www-data

Adding user to www-data group

Add ubuntu user to www-data group

sudo usermod -a -G www-data ubuntu

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: Unto this step, you should be all set to perform file write operations such as git pull as ubuntu user. But, probably, may wish that all files created in this directory (in future) would listen to the current ownership settings. If you do not do it, you will have to set file ownership manually every time 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.

Conclusion

In this Linux tutorial we have learned how to add ubuntu user to www-data group to fix write permission errors occurring due to the mismatch between name and permissions of current user ubuntu and web server group www-data.

Leave a Reply