Setup website authentication on Apache

If you want to setup a basic authentication mechanism on an Apache website you can follow these simple steps.

You can create a user using htpasswd:

sudo htpasswd -c /etc/apache2/passwords username
You can then add the following lines in your Apache site config file (eg /etc/apache2/sites-available/default under the <Directory /> section)
AuthType Basic
   AuthName "Restricted Files"
   AuthUserFile /etc/apache2/passwords
   Require valid-user

Now you just need to reload the updated Apache config

sudo service apache2 reload