How to set up Apache password Authentication on Ubuntu 18.08?

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
</Directory>
</VirtualHost>

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

Related Articles

How to verify Linux System Is 32-bit or 64-bit?

You will find 64-bit OS in most modern computers. It is always recommended to have a 64-bit OS...

bash: netstat: command not found.

You may face the below error on your Linux OS if you haven’t installed the netstat command...

How to configure Exim outgoing IP address from WHM?

By Default, All the outgoing emails from the Exim will be sent from the server’s primary IPv4...

How to Configure VSFTPD with SSL/TLS encrypted connection?

Before you proceed with these steps, ensure that you have installed the VSFTP on your Linux...

How to configure Automatic Updates with yum-cron on CentOS 7?

It is very essential to update your Centos system for the security perspective. Your system may...

  • 0 Users Found This Useful

Was this answer helpful?