It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In RegisterIt looks like you're new here. If you want to get involved, click one of these buttons!
Apache is a free, open-source webserver. Any user can access your application over the Internet when it is hosted on an Apache webserver.
In some cases, you might need to secure your application, that only authenticated users can access the application hosted on your server. You can protect your application by using Apache htpasswd.
So in this tutorial, we will show you how to set up the password authentication with Apache on Ubuntu 20.04
apt-get update -y
apt-get install apache2 apache2-utils -y
systemctl start apache2
systemctl enable apache2
htpasswd -c /etc/apache2/.htpasswd test_user
This will create a .htpasswd file with user credentials. Those credentials will be used to access your site.
mkdir /var/www/html/domain_name
nano /var/www/html/domain_name/index.html
chown -R www-data:www-data /var/www/html/domain_name
nano /etc/apache2/sites-available/domain.conf
< VirtualHost *:80>
ServerAdmin webmaster@domain_name
ServerName domain_name
DocumentRoot /var/www/html/domain_name
DirectoryIndex index.html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
< Directory "/var/www/html/domain_name">
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
< /Directory>
< /VirtualHost>
apachectl -t
You should get the output:
a2ensite example.conf
systemctl restart apache2
Now your site should be secured with Apache basic authentication.