How To Set Up Password Authentication with Apache on Ubuntu 20.04

Password authentication is a crucial security measure when it comes to protecting your web resources. In a production environment, you may have certain directories or entire websites that you want to restrict access to only authorized users. Apache, one of the most popular web servers, provides a straightforward way to implement password authentication on an Ubuntu 20.04 system. This blog post will guide you through the step-by-step process of setting up password authentication with Apache on Ubuntu 20.04.

Table of Contents#

  1. Prerequisites
  2. Installing Apache
  3. Creating a Password File
  4. Configuring Apache for Password Authentication
  5. Restarting Apache
  6. Testing the Password Authentication
  7. Common Practices and Best Practices
  8. Conclusion
  9. References

1. Prerequisites#

Before you begin, make sure you have the following:

  • A server running Ubuntu 20.04.
  • Root or sudo privileges to execute administrative commands.
  • Basic knowledge of the Linux command line.

2. Installing Apache#

If you haven't installed Apache on your Ubuntu 20.04 system, you can do so using the following commands:

sudo apt update
sudo apt install apache2

These commands first update the package list and then install the Apache web server. After the installation is complete, you can verify that Apache is running by opening your web browser and navigating to your server's IP address. You should see the default Apache welcome page.

3. Creating a Password File#

Apache uses the htpasswd utility to manage password files. This utility is part of the apache2-utils package, which you can install if it's not already installed:

sudo apt install apache2-utils

Once the package is installed, you can create a password file and add a user to it. For example, to create a password file named .htpasswd in the /etc/apache2 directory and add a user named testuser, run the following command:

sudo htpasswd -c /etc/apache2/.htpasswd testuser

The -c option is used to create a new password file. If the file already exists, this option will overwrite it. You will be prompted to enter and confirm the password for the user.

If you want to add more users to the existing password file, you can run the same command without the -c option:

sudo htpasswd /etc/apache2/.htpasswd anotheruser

4. Configuring Apache for Password Authentication#

Now that you have a password file, you need to configure Apache to use it for authentication. You can do this by creating or editing an Apache configuration file. For example, if you want to protect a specific directory named /var/www/html/protected, you can create a new configuration file in the /etc/apache2/sites-available directory:

sudo nano /etc/apache2/sites-available/protected.conf

Add the following content to the file:

<Directory /var/www/html/protected>
    AuthType Basic
    AuthName "Restricted Access"
    AuthUserFile /etc/apache2/.htpasswd
    Require valid-user
</Directory>
  • AuthType Basic: Specifies the authentication type as basic, which means the username and password are sent in plain text (although they are base64-encoded).
  • AuthName: Sets the authentication realm name that will be displayed in the login prompt.
  • AuthUserFile: Specifies the path to the password file.
  • Require valid-user: Requires any valid user from the password file to access the directory.

Save and close the file. Then, enable the configuration by creating a symbolic link in the /etc/apache2/sites-enabled directory:

sudo ln -s /etc/apache2/sites-available/protected.conf /etc/apache2/sites-enabled/

5. Restarting Apache#

After making the configuration changes, you need to restart the Apache service for the changes to take effect:

sudo systemctl restart apache2

6. Testing the Password Authentication#

To test the password authentication, open your web browser and navigate to the protected directory, for example, http://your-server-ip/protected. You should see a login prompt asking for a username and password. Enter the username and password you created earlier, and if the credentials are correct, you should be able to access the directory.

7. Common Practices and Best Practices#

Common Practices#

  • Use Strong Passwords: Encourage users to choose strong passwords that are at least 8 characters long and include a combination of uppercase and lowercase letters, numbers, and special characters.
  • Regularly Update Passwords: Set a policy to update passwords periodically to enhance security.
  • Limit Access to the Password File: Make sure the password file has restricted permissions. You can set the permissions using the following command:
sudo chmod 640 /etc/apache2/.htpasswd

This ensures that only the owner (root) and the group (usually www-data) can read and write to the file.

Best Practices#

  • Use HTTPS: Basic authentication sends the username and password in plain text (base64-encoded), which can be intercepted if the connection is not encrypted. To protect the credentials, use HTTPS by installing an SSL/TLS certificate on your server.
  • Implement Multi-Factor Authentication: Consider implementing multi-factor authentication in addition to password authentication to add an extra layer of security.

8. Conclusion#

Setting up password authentication with Apache on Ubuntu 20.04 is a relatively simple process that can significantly enhance the security of your web resources. By following the steps outlined in this blog post and adhering to common and best practices, you can ensure that only authorized users have access to your protected directories.

9. References#