How to Set Up Apache Basic Authentication in Ubuntu 24.04

This article explains how to set up Apache basic authentication to protect directories in Ubuntu 24.04.

Apache Basic Authentication is a method used to restrict access to web resources on an Apache server by requiring users to provide a username and password.

Setting up Basic Authentication is straightforward and requires minimal configuration in the Apache server. Username and password are sent with plain text on Basic Authentication, so you want to set up SSL/TLS before transmitting credentials.

This is because HTTPS encrypts the data exchanged between the user’s browser and the server, adding a significant layer of security.

Protect directories with Apache basic authentication

With Apache installed, run the command below to install Apache utilities.

sudo apt install apache2-utils

After installing the utilities, continue below to protect a directory with a username and password.

For this post, we’ll protect /var/www/html/sensitive-doc with a password. Anyone who wants to access this directory must type a username and password.

To do that, first, create an Apache virtual host file for this configuration. Run the command below to create a file called basic-auth.conf.

sudo nano /etc/apache2/sites-available/auth-basic.conf

Next, please copy and paste the content below into the file and save it.

<Directory /var/www/html/sensitive-doc>
AuthType Basic
AuthName "Basic Authentication"
AuthUserFile /etc/apache2/.htpasswd
require valid-user
</Directory>

Create accounts

After setting up the config above, use the command below to create an account to log on to the directory.

sudo htpasswd -Bc /etc/apache2/.htpasswd username

Replace username with the actual username you want to use.

When prompted, type and confirm a new password for the account.

Create the protected directory

Create the protected directory using the command below.

sudo mkdir /var/www/html/sensitive-doc

Once created, enable the Apache virtual host and reload its services by running the commands below.

sudo a2ensite auth-basic.conf
sudo systemctl reload apache2

Next, run the command below and add a basic HTML ‘index.html‘ file.

sudo nano /var/www/html/sensitive-doc/index.html

Copy and paste the lines below into the file and save.

<html>
<title>My basic authentication HTML page</title>
<body>
<p style="width: 100%; font-weight: bold; font-size: 60px; text-align: center;">
Basic authentication is enabled!
</p>
</body>
</html>

Save the file and exit.

Test Apache basic authentication

Finally, open your browser and browse to the protected directory.

http://example.com/sensitive-doc/

You will see a prompt to enter a username and password.

Apache basic authentication

You must enter the correct username and password to sign in.

That should do it!

Conclusion:

Setting up Apache Basic Authentication on Ubuntu 24.04 is an effective way to secure sensitive directories. You’ve taken significant measures to protect your web resources by following the steps above. Here’s a quick summary:

  • Enhanced Security: Basic Authentication adds a layer of protection by requiring users to authenticate with a username and password.
  • SSL/TLS Implementation: Remember to set up SSL/TLS to encrypt the data exchanged, ensuring that credentials are not sent in plain text.
  • Directory Protection: You can easily protect any directory by creating a virtual host configuration and an account for access.
  • Test Access: Verify your setup by accessing the protected directory and ensuring the authentication prompts function as intended.
  • Ongoing Management: Maintain your credentials and manage user access as needed for optimal security.

With these measures, you can confidently secure your web applications and sensitive data.

Frequently Asked Questions

What is Apache Basic Authentication?

Apache Basic Authentication is a method used to restrict access to web resources on an Apache server by requiring users to provide a username and password. This ensures that only authorized users can access sensitive directories.

How do I install Apache utilities for Basic Authentication?

To install Apache utilities, run the command 'sudo apt install apache2-utils' in your terminal. This will provide the necessary tools to create and manage user accounts for Basic Authentication.

How do I create a protected directory with Basic Authentication?

To create a protected directory, first set up an Apache virtual host configuration that specifies the directory and authentication requirements. Then, create the directory and use the 'htpasswd' command to add a username and password for access.

Is it safe to use Basic Authentication without SSL/TLS?

No, using Basic Authentication without SSL/TLS is not safe because usernames and passwords are transmitted in plain text. It's highly recommended to set up HTTPS to encrypt the data exchanged between the user's browser and the server.

How can I test if Apache Basic Authentication is working?

To test if Apache Basic Authentication is working, open your web browser and navigate to the protected directory. You should see a prompt asking for a username and password; entering the correct credentials will grant you access.

Categories:

One response to “How to Set Up Apache Basic Authentication in Ubuntu 24.04”

  1. […] Apache Basic Authentication is a method used to restrict access to web resources on an Apache server by requiring users to provide a username and password. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version