Updated: 26 March 2024
Create a password file for user mrfoo
sudo htpasswd -c /etc/apache2/.htpasswd mrfoo
In the Virtual Host definition, set the directory for protection and require a valid user
<VirtualHost *:80>
ServerAdmin chris@christaylordeveloper.co.uk
ServerName example.com
DocumentRoot /var/www/example.com/public
<Directory "/var/www/example.com/public">
AuthType Basic
AuthName "Private area"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
</VirtualHost>