HTML Protection¶
Use the Apache web server with .htaccess and .htpasswd files to protect the access to HTML pages.
1. Create a .htpasswd Password File¶
Use the
htpasswdutility to create a new password file and add a user. Replaceusernamewith the username to be added:sudo htpasswd /etc/apache2/.htpasswd usernameIn case it is the first user use this command with flag
-cto also create a file containing usernames and encrypted passwords:sudo htpasswd -c /etc/apache2/.htpasswd usernameDelete the user
usernamefrom.htpasswd:sudo htpasswd -D /etc/apache2/.htpasswd username
2. Create an .htaccess File¶
In the directory containing the HTML files create an
.htaccessfile if it doesn’t already exist.sudo nano /var/www/html/.htaccessThis file will specify the access control rules. Add the following lines to the
.htaccessfile:AuthType Basic AuthName "Restricted Access" AuthUserFile /etc/apache2/.htpasswd Require valid-user
3. Enable .htaccess¶
By default, Apache doesn’t allow
.htaccessfiles to override settings. Enable this by editing the Apache configuration file:sudo nano /etc/apache2/apache2.confFind the
<Directory>section for the directory (usually/var/www/html) and changeAllowOverride NonetoAllowOverride All.<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory>Save the file and exit.
4. Restart Apache¶
After making these changes, restart the Apache service to apply the new configuration:
sudo systemctl restart apache2When someone tries to access the HTML pages from the web server, they will be prompted for a username and password.
Remember to replace
/var/www/htmlwith the actual directory where the HTML files are located. Additionally, ensure that.htpasswdfile is not accessible from the web, as it contains sensitive information.
5. Disable .htaccess Protection¶
Open the .htaccess file in the protected directory:
sudo nano /var/www/html/.htaccessComment out the auth directives:
# AuthType Basic # AuthName "Restricted Area" # AuthUserFile /full/path/to/.htpasswd # Require valid-userAfter making these changes, restart the Apache service to apply the new configuration:
sudo systemctl restart apache2