How to set up Apache password Authentication on Ubuntu 18.08? Print

  • 0

On the internet, almost all the applications have some parts which should be protected with a password like the directories which store the confidential documents files, pdf files, etc. In this article, we will show you how to protect these directories on the server level with Apache htpasswd.

What is htpasswd?

It is an Apache utility that allows you to protect a part of your application or the whole application with username and password.

Follow the below steps to set up the htpasswd on your ubuntu server. 

  • Install Apache2 utils 

It is very easy to install apache2 utils. You can do it by executing the following commands on your terminal.

$ sudo apt-get update
$ sudo apt-get install apache2-utils
  • Create Apache htpasswd file

Once the installation is done we need to create a htpasswd file to store the user information. 

$ sudo touch /etc/apache2/.htpasswd

Once the file is created we can now use the users to htpasswd file. We can create multiple users for multiple applications

  • Add htpasswd user

Adding a user to htpasswd is pretty simple you just need to execute the following command to add the user. 

$ sudo htpasswd /etc/apache2/.htpasswd USERNAME

Once you enter the above command it will ask you to set a password for that user twice. Just enter the strong password and you are good to go. 

Setup Apache Htpasswd protection with VirtualHost

Virtual host file will ask the apache web browser to redirect the request to the specific document root on the domain name. Here, we will add a few line of code to make the specific directory password protected. Run the below command for this. 

$ sudo nano /etc/apache2/sites-available/000-default.conf

Make sure you have the below code inside. With this, test folder will be password protected.

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/test
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

Access the folder now from the browser and you will see that, it will asking for the password.

Was this answer helpful?

« Back
Sign up for a newsletter