AMP AMP

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

To Create Password Protect Directories With Apache On Oracle Linux 8.8

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.

Creation Steps:

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

[root@linuxhelp ~]#cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="8.8"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="8.8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Oracle Linux Server 8.8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:8:8:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://github.com/oracle/oracle-linux"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 8"
ORACLE_BUGZILLA_PRODUCT_VERSION=8.8
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=8.8

Step 2: Install Apache by using the below command

[root@linuxhelp ~]# dnf install httpd* -y
Oracle Linux 8 BaseOS Latest (x86_64)            22 kB/s | 3.6 kB     00:00    
Oracle Linux 8 BaseOS Latest (x86_64)            20 MB/s |  62 MB     00:03    
Oracle Linux 8 Application Stream (x86_64)       36 kB/s | 3.9 kB     00:00    
Oracle Linux 8 Application Stream (x86_64)       22 MB/s |  48 MB     00:02    
Latest Unbreakable Enterprise Kernel Release 7   32 kB/s | 3.0 kB     00:00    
Last metadata expiration check: 0:00:01 ago on Monday 21 August 2023 12:46:33 AM IST.
Dependencies resolved.
================================================================================
 Package            Arch   Version                      Repository         Size
================================================================================
Installing:
 httpd              x86_64 2.4.37-56.0.1.module+el8.8.0+21057+13668aee.6
                                                        ol8_appstream     1.4 M
 httpd-devel        x86_64 2.4.37-56.0.1.module+el8.8.0+21057+13668aee.6
                                                        ol8_appstream     226 k
 httpd-filesystem   noarch 2.4.37-56.0.1.module+el8.8.0+21057+13668aee.6
                                                        ol8_appstream      43 k
 httpd-manual       noarch 2.4.37-56.0.1.module+el8.8.0+21057+13668aee.6
                                                        ol8_appstream     2.4 M
 httpd-tools        x86_64 2.4.37-56.0.1.module+el8.8.0+21057+13668aee.6
                                                        ol8_appstream     110 k
Installing dependencies:
 apr                x86_64 1.6.3-12.el8                 ol8_appstream     129 k
 apr-devel          x86_64 1.6.3-12.el8                 ol8_appstream     246 k
 apr-util           x86_64 1.6.1-6.el8_8.1              ol8_appstream     105 k
 apr-util-devel     x86_64 1.6.1-6.el8_8.1              ol8_appstream      86 k
 cyrus-sasl-devel   x86_64 2.1.27-6.el8_5               ol8_baseos_latest 128 k
 expat-devel        x86_64 2.2.5-11.0.1.el8             ol8_baseos_latest  57 k
 libdb-devel        x86_64 5.3.28-42.el8_4              ol8_appstream      47 k
 mod_http2          x86_64 1.15.7-8.module+el8.8.0+21057+13668aee.3
                                                        ol8_appstream     155 k
 openldap-devel     x86_64 2.4.46-18.el8                ol8_baseos_latest 811 k
 oracle-logos-httpd noarch 84.5-1.0.1.el8               ol8_baseos_latest  29 k
Installing weak dependencies:
 apr-util-bdb       x86_64 1.6.1-6.el8_8.1              ol8_appstream      25 k
 apr-util-openssl   x86_64 1.6.1-6.el8_8.1              ol8_appstream      27 k
Enabling module streams:
 httpd                     2.4                                                 

Transaction Summary
================================================================================
Install  17 Packages

Total download size: 6.0 M
Installed size: 19 M
Downloading Packages:
(1/17): expat-devel-2.2.5-11.0.1.el8.x86_64.rpm 359 kB/s |  57 kB     00:00    
(2/17): oracle-logos-httpd-84.5-1.0.1.el8.noarc 965 kB/s |  29 kB     00:00    
(3/17): cyrus-sasl-devel-2.1.27-6.el8_5.x86_64. 532 kB/s | 128 kB     00:00    
(4/17): apr-1.6.3-12.el8.x86_64.rpm             2.2 MB/s | 129 kB     00:00    
(5/17): openldap-devel-2.4.46-18.el8.x86_64.rpm 2.7 MB/s | 811 kB     00:00    
(6/17): apr-util-1.6.1-6.el8_8.1.x86_64.rpm     2.5 MB/s | 105 kB     00:00    
(7/17): apr-util-bdb-1.6.1-6.el8_8.1.x86_64.rpm 978 kB/s |  25 kB     00:00    
(8/17): apr-util-openssl-1.6.1-6.el8_8.1.x86_64 918 kB/s |  27 kB     00:00    
(9/17): apr-devel-1.6.3-12.el8.x86_64.rpm       2.2 MB/s | 246 kB     00:00    
(10/17): httpd-devel-2.4.37-56.0.1.module+el8.8 3.7 MB/s | 226 kB     00:00    
(11/17): httpd-filesystem-2.4.37-56.0.1.module+ 1.2 MB/s |  43 kB     00:00    
(12/17): httpd-2.4.37-56.0.1.module+el8.8.0+210  11 MB/s | 1.4 MB     00:00    
(13/17): httpd-tools-2.4.37-56.0.1.module+el8.8 1.8 MB/s | 110 kB     00:00    
(14/17): httpd-manual-2.4.37-56.0.1.module+el8.  13 MB/s | 2.4 MB     00:00    
(15/17): libdb-devel-5.3.28-42.el8_4.x86_64.rpm 440 kB/s |  47 kB     00:00    
(16/17): mod_http2-1.15.7-8.module+el8.8.0+2105 3.8 MB/s | 155 kB     00:00    
(17/17): apr-util-devel-1.6.1-6.el8_8.1.x86_64.  20 kB/s |  86 kB     00:04    
--------------------------------------------------------------------------------
Total                                           1.3 MB/s | 6.0 MB     00:04     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : apr-1.6.3-12.el8.x86_64                               1/17 
  Running scriptlet: apr-1.6.3-12.el8.x86_64                               1/17 
  Installing       : apr-util-bdb-1.6.1-6.el8_8.1.x86_64                   2/17 
  Installing       : apr-util-openssl-1.6.1-6.el8_8.1.x86_64               3/17 
  Installing       : apr-util-1.6.1-6.el8_8.1.x86_64                       4/17 
  Running scriptlet: apr-util-1.6.1-6.el8_8.1.x86_64                       4/17 
  Installing       : apr-devel-1.6.3-12.el8.x86_64                         5/17 
  Installing       : httpd-tools-2.4.37-56.0.1.module+el8.8.0+21057+136    6/17 
  Installing       : libdb-devel-5.3.28-42.el8_4.x86_64                    7/17 
  Running scriptlet: httpd-filesystem-2.4.37-56.0.1.module+el8.8.0+2105    8/17 
  Installing       : httpd-filesystem-2.4.37-56.0.1.module+el8.8.0+2105    8/17 
  Installing       : oracle-logos-httpd-84.5-1.0.1.el8.noarch              9/17 
  Installing       : mod_http2-1.15.7-8.module+el8.8.0+21057+13668aee.3   10/17 
  Installing       : httpd-2.4.37-56.0.1.module+el8.8.0+21057+13668aee.   11/17 
  Running scriptlet: httpd-2.4.37-56.0.1.module+el8.8.0+21057+13668aee.   11/17 
  Installing       : expat-devel-2.2.5-11.0.1.el8.x86_64                  12/17 
  Installing       : cyrus-sasl-devel-2.1.27-6.el8_5.x86_64               13/17 
  Installing       : openldap-devel-2.4.46-18.el8.x86_64                  14/17 
  Installing       : apr-util-devel-1.6.1-6.el8_8.1.x86_64                15/17 
  Installing       : httpd-devel-2.4.37-56.0.1.module+el8.8.0+21057+136   16/17 
  Installing       : httpd-manual-2.4.37-56.0.1.module+el8.8.0+21057+13   17/17 
  Running scriptlet: httpd-2.4.37-56.0.1.module+el8.8.0+21057+13668aee.   17/17 
  Running scriptlet: httpd-manual-2.4.37-56.0.1.module+el8.8.0+21057+13   17/17 
  Verifying        : cyrus-sasl-devel-2.1.27-6.el8_5.x86_64                1/17 
  Verifying        : expat-devel-2.2.5-11.0.1.el8.x86_64                   2/17 
  Verifying        : openldap-devel-2.4.46-18.el8.x86_64                   3/17 
  Verifying        : oracle-logos-httpd-84.5-1.0.1.el8.noarch              4/17 
  Verifying        : apr-1.6.3-12.el8.x86_64                               5/17 
  Verifying        : apr-devel-1.6.3-12.el8.x86_64                         6/17 
  Verifying        : apr-util-1.6.1-6.el8_8.1.x86_64                       7/17 
  Verifying        : apr-util-bdb-1.6.1-6.el8_8.1.x86_64                   8/17 
  Verifying        : apr-util-devel-1.6.1-6.el8_8.1.x86_64                 9/17 
  Verifying        : apr-util-openssl-1.6.1-6.el8_8.1.x86_64              10/17 
  Verifying        : httpd-2.4.37-56.0.1.module+el8.8.0+21057+13668aee.   11/17 
  Verifying        : httpd-devel-2.4.37-56.0.1.module+el8.8.0+21057+136   12/17 
  Verifying        : httpd-filesystem-2.4.37-56.0.1.module+el8.8.0+2105   13/17 
  Verifying        : httpd-manual-2.4.37-56.0.1.module+el8.8.0+21057+13   14/17 
  Verifying        : httpd-tools-2.4.37-56.0.1.module+el8.8.0+21057+136   15/17 
  Verifying        : libdb-devel-5.3.28-42.el8_4.x86_64                   16/17 
  Verifying        : mod_http2-1.15.7-8.module+el8.8.0+21057+13668aee.3   17/17 

Installed:
  apr-1.6.3-12.el8.x86_64                                                       
  apr-devel-1.6.3-12.el8.x86_64                                                 
  apr-util-1.6.1-6.el8_8.1.x86_64                                               
  apr-util-bdb-1.6.1-6.el8_8.1.x86_64                                           
  apr-util-devel-1.6.1-6.el8_8.1.x86_64                                         
  apr-util-openssl-1.6.1-6.el8_8.1.x86_64                                       
  cyrus-sasl-devel-2.1.27-6.el8_5.x86_64                                        
  expat-devel-2.2.5-11.0.1.el8.x86_64                                           
  httpd-2.4.37-56.0.1.module+el8.8.0+21057+13668aee.6.x86_64                    
  httpd-devel-2.4.37-56.0.1.module+el8.8.0+21057+13668aee.6.x86_64              
  httpd-filesystem-2.4.37-56.0.1.module+el8.8.0+21057+13668aee.6.noarch         
  httpd-manual-2.4.37-56.0.1.module+el8.8.0+21057+13668aee.6.noarch             
  httpd-tools-2.4.37-56.0.1.module+el8.8.0+21057+13668aee.6.x86_64              
  libdb-devel-5.3.28-42.el8_4.x86_64                                            
  mod_http2-1.15.7-8.module+el8.8.0+21057+13668aee.3.x86_64                     
  openldap-devel-2.4.46-18.el8.x86_64                                           
  oracle-logos-httpd-84.5-1.0.1.el8.noarch                                      
Complete!

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

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

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

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

Step 5: Use the vim editor to create the index.html file

[root@linuxhelp reserved]# vim index.html

Step 6: Paste this sample content for the reserved area into the html file. Save the file

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

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

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

Step 8: 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 reserved]# sudo vim /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 9: Then, go to the "reserved" folder and create the ".htaccess" file and paste the below content

[root@linuxhelp reserved]# vim .htaccess
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/httpd/.htpasswd
Require valid-user

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

[root@linuxhelp reserved]# systemctl restart httpd

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

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

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