Follow
Ubuntu Linux

How to Set Up Apache Basic Authentication in Ubuntu 24.04

Richard
Written by
Richard
Feb 19, 2025 Updated Mar 20, 2026 3 min read
How to Set Up Apache Basic Authentication in Ubuntu 24.04
How to Set Up Apache Basic Authentication in Ubuntu 24.04

You set up Apache Basic Authentication in Ubuntu 24.04 to require a username and password before granting access to your web server’s content.

This method adds a simple layer of security, ideal for protecting specific directories like development staging areas or private dashboards on your site.

You’ll configure Apache to use the `.htaccess` file and the `htpasswd` utility to manage user credentials, typically within the `/etc/apache2/sites-available/` directory.

This approach ensures only authenticated users can view the protected files.

⚡ Quick Answer

Install Apache utilities with `sudo apt install apache2-utils`. Create a virtual host file in `/etc/apache2/sites-available/` to define the protected directory and authentication settings. Then, use `htpasswd -Bc /etc/apache2/.htpasswd username` to create user credentials.

Protect directories with Apache basic authentication

Now that you have Apache installed, let’s protect a directory. First, you’ll need to install some Apache utilities. Run the command below.

🐧Bash / Shell
sudo apt install apache2-utils

With the utilities installed, 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.

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

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

💻Code
<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 configuration above, use the command below to create an account for logging into the directory.

🐧Bash / Shell
sudo htpasswd -Bc /etc/apache2/.htpasswd username

Replace username with the username you’d like 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.

🐧Bash / Shell
sudo mkdir /var/www/html/sensitive-doc

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

🐧Bash / Shell
sudo a2ensite auth-basic.conf
sudo systemctl reload apache2

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

🐧Bash / Shell
sudo nano /var/www/html/sensitive-doc/index.html

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

💻Code
<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 navigate to the protected directory.

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

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

Apache basic authentication
Apache basic authentication

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

Terminal window showing successful Apache basic authentication setup on Ubuntu 24.04
Terminal window showing successful Apache basic authentication setup on Ubuntu 24.04

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 of what you’ve accomplished:

  • 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.

Was this guide helpful?

Was this helpful?
Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

📚 Related Tutorials

How to Switch between HTML & Plain Text in Outlook
Windows How to Switch between HTML & Plain Text in Outlook
How to Install GNOME Desktop on Ubuntu 24.04
Ubuntu Linux How to Install GNOME Desktop on Ubuntu 24.04
How to Install KDE Desktop on Ubuntu 24.04
Ubuntu Linux How to Install KDE Desktop on Ubuntu 24.04

1 Comment

Leave a Comment

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