How to create Password Protect Directories with Apache on AlmaLinux 9

Estimated reading: 3 minutes 251 views

Apache is the most commonly used Web server on Linux systems. Web servers are used to help Web pages requested by client computers. The htpasswd command will allow us to create a password file that Apache can use to authenticate users.

In this article, we will guide you to Create Password-protected Directories With Apache On AlmaLinux 9.

Create Password Protect Directories With Apache AlmaLinux 9

Step 1: First, install the Apache web server by using the below command

dnf install httpd* -y

Step 2: Enable and start the Apache service by using the below command

systemctl enable httpd
systemctl start httpd

Step 3: Check the status of the Apache service by using the below command

systemctl status httpd

Step 4: Move to the Apache Document Root location with the below command

cd /var/www/html/

Step 5: Create the new directory here by using the below command

mkdir Greentest

Step 6: Change to the newly created directory by using the below command

cd Greentest/

Step 7: Create the index.html file by using the vi text editor.

vi index.html

Insert the following lines:

<html>
<head></head>
<body>
<h2>Hello All</h2>
<b>Welcome to GreenCloudVPS!</b>
</body>
</html>

Step 8: Create the htpasswd file with the authentication data, then use the htpasswd command to create authentication credentials by using the below command.

htpasswd -c /etc/httpd/.htpasswd GreenVPS

Step 9: Configure Apache to restrict access to the directory only to users entered in the .htpasswd file. To do so, create a configuration file in the following location by using the vi text editor.

vi /etc/httpd/conf.d/test.conf

Insert the following lines:

<Directory "/var/www/html/Greentest">
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/httpd/.htpasswd
Require valid-user
</Directory>

Step 10: Go to the Greentest folder and create the “.htaccess” file by using the below command.

vi .htaccess

Insert the following lines:

AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/httpd/.htpasswd
Require valid-user

Step 11: Restart the Apache service to apply all changes by using the below command

systemctl restart httpd

Step 12: Now go to the web browser and enter the corresponding ip address along with the directory that is, http: //IP Address/Greentest/.

Here enter the previously created Greentest directory credentials.

Step 13: In case of correct credentials, the Greentest area will be shown.

Conclusion

In this guide, we have walked you through the steps required to create password-protect directories with Apache on AlmaLinux 9. Good luck!