How to Set Up Basic Authentication with Apache on AlmaLinux
Categories:
Basic Authentication is a simple yet effective way to restrict access to certain parts of your website or web application. It prompts users to enter a username and password to gain access, providing a layer of security without the need for complex login systems. Apache HTTP Server, paired with AlmaLinux, offers a straightforward method to implement Basic Authentication.
In this guide, we’ll walk you through configuring Basic Authentication on Apache running on AlmaLinux, ensuring secure access to protected resources.
Why Use Basic Authentication?
Basic Authentication is ideal for:
- Restricting Access to Sensitive Pages: Protect administrative panels, development environments, or internal resources.
- Quick and Simple Setup: No additional software or extensive coding is required.
- Lightweight Protection: Effective for low-traffic sites or internal projects without full authentication systems.
Prerequisites
Before setting up Basic Authentication, ensure the following:
A Server Running AlmaLinux
With root or sudo privileges.Apache Installed and Running
If not installed, install Apache with:sudo dnf install httpd -y sudo systemctl start httpd sudo systemctl enable httpd
Administrative Access
Familiarity with Linux commands and file editing tools likenano
orvim
.
Step 1: Enable the mod_authn_core
and mod_auth_basic
Modules
Apache’s Basic Authentication relies on the mod_authn_core
and mod_auth_basic
modules. These modules
These modules should be enabled by default in most Apache installations. Verify they are loaded:
httpd -M | grep auth
Look for authn_core_module
and auth_basic_module
in the output. If these modules are not listed, enable them by editing the Apache configuration file:
Open the Apache configuration file:
sudo nano /etc/httpd/conf/httpd.conf
Add the following lines (if not already present):
LoadModule authn_core_module modules/mod_authn_core.so LoadModule auth_basic_module modules/mod_auth_basic.so
Save the file and restart Apache to apply the changes:
sudo systemctl restart httpd
Step 2: Create a Password File Using htpasswd
The htpasswd
utility is used to create and manage user credentials for Basic Authentication.
Install
httpd-tools
Thehtpasswd
utility is included in thehttpd-tools
package. Install it with:sudo dnf install httpd-tools -y
Create a Password File
Usehtpasswd
to create a file that stores user credentials:sudo htpasswd -c /etc/httpd/.htpasswd username
- Replace
username
with the desired username. - The
-c
flag creates a new file. Omit this flag to add additional users to an existing file.
You’ll be prompted to enter and confirm the password. The password is hashed and stored in the
/etc/httpd/.htpasswd
file.- Replace
Verify the Password File
Check the contents of the file:cat /etc/httpd/.htpasswd
You’ll see the username and the hashed password.
Step 3: Configure Apache for Basic Authentication
To restrict access to a specific directory, update the Apache configuration.
Edit the Apache Configuration File
For example, to protect the/var/www/html/protected
directory, create or modify the.conf
file for the site:sudo nano /etc/httpd/conf.d/protected.conf
Add Authentication Directives
Add the following configuration to enable Basic Authentication:<Directory "/var/www/html/protected"> AuthType Basic AuthName "Restricted Area" AuthUserFile /etc/httpd/.htpasswd Require valid-user </Directory>
- AuthType: Specifies the authentication type, which is
Basic
in this case. - AuthName: Sets the message displayed in the login prompt.
- AuthUserFile: Points to the password file created with
htpasswd
. - Require valid-user: Allows access only to users listed in the password file.
- AuthType: Specifies the authentication type, which is
Save the File and Restart Apache
Restart Apache to apply the changes:sudo systemctl restart httpd
Step 4: Create the Protected Directory
If the directory you want to protect doesn’t already exist, create it and add some content to test the configuration.
Create the directory:
sudo mkdir -p /var/www/html/protected
Add a sample file:
echo "This is a protected area." | sudo tee /var/www/html/protected/index.html
Set the proper ownership and permissions:
sudo chown -R apache:apache /var/www/html/protected sudo chmod -R 755 /var/www/html/protected
Step 5: Test the Basic Authentication Setup
Open a web browser and navigate to the protected directory:
http://<your-server-ip>/protected
A login prompt should appear. Enter the username and password created with
htpasswd
.If the credentials are correct, you’ll gain access to the protected content.
Advanced Configuration Options
1. Restrict Access to Specific Users
If you want to allow access to specific users, modify the Require
directive:
Require user username1 username2
Replace username1
and username2
with the allowed usernames.
2. Restrict Access by IP and User
You can combine IP-based restrictions with Basic Authentication:
<Directory "/var/www/html/protected">
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /etc/httpd/.htpasswd
Require valid-user
Require ip 192.168.1.0/24
</Directory>
This configuration allows access only to users with valid credentials from the specified IP range.
3. Secure the Password File
Ensure the password file is not accessible via the web by setting appropriate permissions:
sudo chmod 640 /etc/httpd/.htpasswd
sudo chown root:apache /etc/httpd/.htpasswd
4. Use HTTPS for Authentication
Basic Authentication transmits credentials in plaintext, making it insecure over HTTP. To secure authentication, enable HTTPS:
Install Certbot and the Apache plugin:
sudo dnf install certbot python3-certbot-apache -y
Obtain an SSL certificate from Let’s Encrypt:
sudo certbot --apache
Test the HTTPS configuration by navigating to the secure URL:
https://<your-server-ip>/protected
Troubleshooting Common Issues
Login Prompt Doesn’t Appear
- Check if the
mod_auth_basic
module is enabled. - Verify the
AuthUserFile
path is correct.
- Check if the
Access Denied After Entering Credentials
- Ensure the username exists in the
.htpasswd
file. - Verify permissions for the
.htpasswd
file.
- Ensure the username exists in the
Changes Not Reflected
Restart Apache after modifying configurations:sudo systemctl restart httpd
Password File Not Found Error
Double-check the path to the.htpasswd
file and ensure it matches theAuthUserFile
directive.
Conclusion
Setting up Basic Authentication with Apache on AlmaLinux is a straightforward way to secure sensitive areas of your web server. While not suitable for highly sensitive applications, it serves as an effective tool for quick access control and lightweight security.
By following this guide, you’ve learned to enable Basic Authentication, create and manage user credentials, and implement additional layers of security. For enhanced protection, combine Basic Authentication with HTTPS to encrypt user credentials during transmission.