This guide has been written to help simplify protecting an admin directory or an entire website.
First steps:
Run the following commands in a terminal. (assuming you have already installed an apache2 server)
sudo apt update
sudo apt install apache2-utils
Next, create a .htpasswd file, this can be in any directory, but a good idea is to put it in /etc/apache2/
This command will ask you to supply a password, and confirm it of course.
the last part of the command is used to make a username.
sudo htpasswd -c /etc/apache2/.htpasswd admin
Drop the -c flag if you wish to add more users.
Next step is to create a .htaccess file for the directory(s) that you wish to protect with username and password authentication.
Edit the Virtual Host definition, found in the /etc/apache2/sites-enabled/000-default.conf file.
sudo nano /etc/apache2/sites-enabled/000-default.conf
You should see something similar to bellow code:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Now add the following lines:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/var/www/html">
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
</VirtualHost>
ctrl+x then y then return to save the file, now restart apache server:
sudo service apache2 restart
Thats it! Following the same basic methods, you could protect any directory or the entire website.
Thanks for reading! =)
For more information check out Man pages
© Autonetix.co - All rights reserved | 2022 - 2025