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 determine what is using the most disk space on a Linux server?

There are times when you receive following error while accessing the cPanel WebMail or WHM. You...

On which platform Linux VPS are configured?

We configure Linux VPS on XEN virtualization technique. However, Linux Cloud VPS are configured...

How to use gzip in Linux?

Linux has many commands line tools to compress/decompress files and gzip is one of them. In this...

Will internal traffic between two VPS viz. a Database Server and a Web Server be counted in my bandwidth usage?

Yes it will. The traffic will be counted in the bandwidth usage of your VPS. Even if you use...

How do I enable apache file and directory indexing in Linux or UNIX?

There are 2 ways to enable apache file and directory indexing. Using Apache config file : You...

  • 0 Users Found This Useful

Was this answer helpful?