How to create password protect directories with Apache on Oracle Linux 8.5

To Password Protect Directories With Apache On Oracle Linux 8.5

Introduction:

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.

Procedure:

Step 1: Check the Oracle Linux Version by using the below command

[root@linuxhelp ~]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="8.5"
ID="ol"

Step 2: Using terminal move to the Apache DocumentRoot with the below command

[root@linuxhelp ~]# cd /var/www/html

Step 3: Create the "reserved" folder and go inside by using the below command

[root@linuxhelp html]#   mkdir reserved && cd reserved

Step 4: Use the Nano editor to create the index.html file

[root@linuxhelp reserved]#  nano index.html

Step 5: Paste this sample content for the reserved area into the html file. Save the file by pressing CTRL + X, then Y and finally ENTER.

<html>
<head></head>
<body>
 <h2>Reserved Area</h2>
 <b>Welcome!</b>
</body>
</html>

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

[root@linuxhelp ]# sudo htpasswd -c /etc/httpd/.htpasswd user1
New password:
Re-type new password:
Adding password for user user1

Step 7: 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 and paste the below content.

[root@linuxhelp ]# sudo nano /etc/httpd/conf.d/test.conf
<Directory "/var/www/html/reserved">
 AuthType Basic
 AuthName "Restricted Content"
 AuthUserFile /etc/httpd/.htpasswd
 Require valid-user
</Directory>

Step 8: Then, go to the "reserved" folder and create the ".htaccess" file and paste the below content

[root@linuxhelp]# cd /var/www/html/reserved
[root@linuxhelp reserved]#  nano .htacess
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/httpd/.htpasswd
Require valid-user

Step 9: Restart the Apache service by using the below command

[root@linuxhelp] # systemctl restart httpd

Step 10: To verify that the reserved area has been correctly configured, browse to the corresponding internet address, that is, http: //IP.SERVER/reserved.

Step 11: A message requesting to enter the previously created credentials will appear. In case of correct credentials, the reserved area will be shown.

Step 12: Incase of Wrong Credentials. An error will be displayed as shown in the below image.

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to create password protect directories with Apache on Oracle Linux 8.5. Your feedback is much welcome.

FAQ
Q
How do I access the Apache server?
A
To connect to the server and access the default page, launch a browser and enter this URL:http://localhost/ Apache should respond with a welcome page and you should see "It Works!"
Q
How do I remove a user from htpasswd?
A
To remove a user: Just open the password file using nano or vim and simply remove the user line that you want to delete...
Q
Is .htaccess necessary?
A
htaccess is not required for having a general website
Q
Why is .htaccess not working?
A
Improper syntax being used
Q
Is htpasswd secure?
A
htpasswd is actually yielding a screen for your username and password, it is secure.