How to add, manage and delete users for Apache2 on Ubuntu?

One of the most basic tasks that you should know how to do on your new Ubuntu server is to add or remove users.

    $ sudo adduser NEWUSERNAME

You will be asked a series of questions. Choose a secure password. The other questions are optional and you can just hit Enter to skip them.

If you’d like for your users to have a web accessible folder, you can enable the Userdir feature:

    $ sudo a2enmod userdir

The default directory is set as public_html in the user’s home directory, which you’ll have to create. The contents of the folder will then be accessible from http://example.com/~NEWUSERNAME.

    $ mkdir ~/public_html

To enable a custom domain for the user, you would have to create a new virtual host file.

    $ sudo nano /etc/apache2/sites-available/NEWUSERNAME.com.conf

    <VirtualHost *:80>
        ServerAdmin admininstrator@example.com
        ServerName NEWUSERNAME.com
        ServerAlias www.NEWUSERNAME.com
        DocumentRoot /home/NEWUSERNAME/public_html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>

    $ sudo a2ensite NEWUSERNAME.com.conf

And as always, remember to restart the server to ensure all changes take effect correctly:

    $ sudo systemctl restart apache2

When it’s time to delete the user, you can run the following command.

    $ sudo deluser --remove-home NEWUSERNAME

This will also remove the NEWUSERNAME’s home directory.

That’s it.

So simple!

14 March 2019

index