How to password-protect directories with Apache on Rocky Linux 8.6

To password protect directories with Apache on Rocky Linux 8.6

Introduction:

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

Installation Procedure :

STep 1 : check the OS version by using the following command

[root@linuxhelp ~]# cat /etc/os-release 
NAME="Rocky Linux"
VERSION="8.6 (Green Obsidian)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="8.6"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Rocky Linux 8.6 (Green Obsidian)"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:rocky:rocky:8:GA"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
ROCKY_SUPPORT_PRODUCT="Rocky Linux"
ROCKY_SUPPORT_PRODUCT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8"

Step 2 : Install apache package by using the following command

[root@linuxhelp ~]# yum install httpd*
Rocky Linux 8 - AppStream                                           5.0 kB/s | 4.8 kB     00:00    
Rocky Linux 8 - AppStream                                           3.7 MB/s |  11 MB     00:03    
Rocky Linux 8 - BaseOS                                              4.9 kB/s | 4.3 kB     00:00    
Rocky Linux 8 - BaseOS                                              6.6 MB/s | 9.0 MB     00:01    
Rocky Linux 8 - Extras                                              3.7 kB/s | 3.5 kB     00:00    
Rocky Linux 8 - Extras                                               13 kB/s |  12 kB     00:00    
Dependencies resolved.
====================================================================================================
 Package               Arch       Version                                       Repository     Size
====================================================================================================
Installing:
 httpd                 x86_64     2.4.37-47.module+el8.6.0+985+b8ff6398.2       appstream     1.4 M
 httpd-devel           x86_64     2.4.37-47.module+el8.6.0+985+b8ff6398.2       appstream     223 k
 httpd-filesystem      noarch     2.4.37-47.module+el8.6.0+985+b8ff6398.2       appstream      40 k
 httpd-manual          noarch     2.4.37-47.module+el8.6.0+985+b8ff6398.2       appstream     2.4 M
 httpd-tools           x86_64     2.4.37-47.module+el8.6.0+985+b8ff6398.2       appstream     107 k
Installing dependencies:
 apr                   x86_64     1.6.3-12.el8                                  appstream     128 k
 apr-devel             x86_64     1.6.3-12.el8                                  appstream     245 k
 apr-util              x86_64     1.6.1-6.el8.1                                 appstream     104 k
 apr-util-devel        x86_64     1.6.1-6.el8.1                                 appstream      85 k
 cyrus-sasl-devel      x86_64     2.1.27-6.el8_5                                baseos        127 k
 expat-devel           x86_64     2.2.5-8.el8_6.2                               baseos         56 k
 libdb-devel           x86_64     5.3.28-42.el8_4                               appstream      46 k
 mod_http2             x86_64     1.15.7-5.module+el8.6.0+823+f143cee1          appstream     153 k
 openldap-devel        x86_64     2.4.46-18.el8                                 baseos        810 k
 rocky-logos-httpd     noarch     86.2-1.el8                                    baseos         24 k
Installing weak dependencies:
 apr-util-bdb          x86_64     1.6.1-6.el8.1                                 appstream      23 k
 apr-util-openssl      x86_64     1.6.1-6.el8.1                                 appstream      26 k
Enabling module streams:
 httpd                            2.4                                                              

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

Total download size: 5.9 M
Installed size: 19 M
Is this ok [y/N]: y
Downloading Packages:
(1/17): apr-1.6.3-12.el8.x86_64.rpm                                 571 kB/s | 128 kB     00:00    
(2/17): apr-util-1.6.1-6.el8.1.x86_64.rpm                           459 kB/s | 104 kB     00:00    
(3/17): apr-devel-1.6.3-12.el8.x86_64.rpm                           935 kB/s | 245 kB     00:00    
(4/17): apr-util-bdb-1.6.1-6.el8.1.x86_64.rpm                       588 kB/s |  23 kB     00:00    
(5/17): apr-util-devel-1.6.1-6.el8.1.x86_64.rpm                     1.7 MB/s |  85 kB     00:00    
(6/17): apr-util-openssl-1.6.1-6.el8.1.x86_64.rpm                   605 kB/s |  26 kB     00:00    
(7/17): httpd-filesystem-2.4.37-47.module+el8.6.0+985+b8ff6398.2.no 1.2 MB/s |  40 kB     00:00    
(8/17): httpd-devel-2.4.37-47.module+el8.6.0+985+b8ff6398.2.x86_64. 2.6 MB/s | 223 kB     00:00    
(9/17): httpd-tools-2.4.37-47.module+el8.6.0+985+b8ff6398.2.x86_64. 2.2 MB/s | 107 kB     00:00    
(10/17): httpd-2.4.37-47.module+el8.6.0+985+b8ff6398.2.x86_64.rpm   9.0 MB/s | 1.4 MB     00:00    
(11/17): libdb-devel-5.3.28-42.el8_4.x86_64.rpm                     1.3 MB/s |  46 kB     00:00    
(12/17): mod_http2-1.15.7-5.module+el8.6.0+823+f143cee1.x86_64.rpm  3.8 MB/s | 153 kB     00:00    
(13/17): httpd-manual-2.4.37-47.module+el8.6.0+985+b8ff6398.2.noarc  15 MB/s | 2.4 MB     00:00    
(14/17): cyrus-sasl-devel-2.1.27-6.el8_5.x86_64.rpm                 2.0 MB/s | 127 kB     00:00    
(15/17): expat-devel-2.2.5-8.el8_6.2.x86_64.rpm                     1.3 MB/s |  56 kB     00:00    
(16/17): rocky-logos-httpd-86.2-1.el8.noarch.rpm                    724 kB/s |  24 kB     00:00    
(17/17): openldap-devel-2.4.46-18.el8.x86_64.rpm                     17 MB/s | 810 kB     00:00    
----------------------------------------------------------------------------------------------------
Total                                                               2.9 MB/s | 5.9 MB     00:02     
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.1.x86_64                                         2/17 
  Installing       : apr-util-openssl-1.6.1-6.el8.1.x86_64                                     3/17 
  Installing       : apr-util-1.6.1-6.el8.1.x86_64                                             4/17 
  Running scriptlet: apr-util-1.6.1-6.el8.1.x86_64                                             4/17 
  Installing       : apr-devel-1.6.3-12.el8.x86_64                                             5/17 
  Installing       : httpd-tools-2.4.37-47.module+el8.6.0+985+b8ff6398.2.x86_64                6/17 
  Installing       : rocky-logos-httpd-86.2-1.el8.noarch                                       7/17 
  Installing       : expat-devel-2.2.5-8.el8_6.2.x86_64                                        8/17 
  Installing       : cyrus-sasl-devel-2.1.27-6.el8_5.x86_64                                    9/17 
  Installing       : openldap-devel-2.4.46-18.el8.x86_64                                      10/17 
  Installing       : libdb-devel-5.3.28-42.el8_4.x86_64                                       11/17 
  Installing       : apr-util-devel-1.6.1-6.el8.1.x86_64                                      12/17 
  Running scriptlet: httpd-filesystem-2.4.37-47.module+el8.6.0+985+b8ff6398.2.noarch          13/17 
  Installing       : httpd-filesystem-2.4.37-47.module+el8.6.0+985+b8ff6398.2.noarch          13/17 
  Installing       : mod_http2-1.15.7-5.module+el8.6.0+823+f143cee1.x86_64                    14/17 
  Installing       : httpd-2.4.37-47.module+el8.6.0+985+b8ff6398.2.x86_64                     15/17 
  Running scriptlet: httpd-2.4.37-47.module+el8.6.0+985+b8ff6398.2.x86_64                     15/17 
  Installing       : httpd-devel-2.4.37-47.module+el8.6.0+985+b8ff6398.2.x86_64               16/17 
  Installing       : httpd-manual-2.4.37-47.module+el8.6.0+985+b8ff6398.2.noarch              17/17 
  Running scriptlet: httpd-2.4.37-47.module+el8.6.0+985+b8ff6398.2.x86_64                     17/17 
  Running scriptlet: httpd-manual-2.4.37-47.module+el8.6.0+985+b8ff6398.2.noarch              17/17 
  Verifying        : apr-1.6.3-12.el8.x86_64                                                   1/17 
  Verifying        : apr-devel-1.6.3-12.el8.x86_64                                             2/17 
  Verifying        : apr-util-1.6.1-6.el8.1.x86_64                                             3/17 
  Verifying        : apr-util-bdb-1.6.1-6.el8.1.x86_64                                         4/17 
  Verifying        : apr-util-devel-1.6.1-6.el8.1.x86_64                                       5/17 
  Verifying        : apr-util-openssl-1.6.1-6.el8.1.x86_64                                     6/17 
  Verifying        : httpd-2.4.37-47.module+el8.6.0+985+b8ff6398.2.x86_64                      7/17 
  Verifying        : httpd-devel-2.4.37-47.module+el8.6.0+985+b8ff6398.2.x86_64                8/17 
  Verifying        : httpd-filesystem-2.4.37-47.module+el8.6.0+985+b8ff6398.2.noarch           9/17 
  Verifying        : httpd-manual-2.4.37-47.module+el8.6.0+985+b8ff6398.2.noarch              10/17 
  Verifying        : httpd-tools-2.4.37-47.module+el8.6.0+985+b8ff6398.2.x86_64               11/17 
  Verifying        : libdb-devel-5.3.28-42.el8_4.x86_64                                       12/17 
  Verifying        : mod_http2-1.15.7-5.module+el8.6.0+823+f143cee1.x86_64                    13/17 
  Verifying        : cyrus-sasl-devel-2.1.27-6.el8_5.x86_64                                   14/17 
  Verifying        : expat-devel-2.2.5-8.el8_6.2.x86_64                                       15/17 
  Verifying        : openldap-devel-2.4.46-18.el8.x86_64                                      16/17 
  Verifying        : rocky-logos-httpd-86.2-1.el8.noarch                                      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.1.x86_64                                                                     
  apr-util-bdb-1.6.1-6.el8.1.x86_64                                                                 
  apr-util-devel-1.6.1-6.el8.1.x86_64                                                               
  apr-util-openssl-1.6.1-6.el8.1.x86_64                                                             
  cyrus-sasl-devel-2.1.27-6.el8_5.x86_64                                                            
  expat-devel-2.2.5-8.el8_6.2.x86_64                                                                
  httpd-2.4.37-47.module+el8.6.0+985+b8ff6398.2.x86_64                                              
  httpd-devel-2.4.37-47.module+el8.6.0+985+b8ff6398.2.x86_64                                        
  httpd-filesystem-2.4.37-47.module+el8.6.0+985+b8ff6398.2.noarch                                   
  httpd-manual-2.4.37-47.module+el8.6.0+985+b8ff6398.2.noarch                                       
  httpd-tools-2.4.37-47.module+el8.6.0+985+b8ff6398.2.x86_64                                        
  libdb-devel-5.3.28-42.el8_4.x86_64                                                                
  mod_http2-1.15.7-5.module+el8.6.0+823+f143cee1.x86_64                                             
  openldap-devel-2.4.46-18.el8.x86_64                                                               
  rocky-logos-httpd-86.2-1.el8.noarch                                                               

Complete!

Step 3 : Enable the apache service by using the below command

[root@linuxhelp ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

Step 4 : Start the apache service by using the below command

[root@linuxhelp ~]# systemctl start httpd.service

Step 5 : get into the apache root directory

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

Step 6 : create reserved directory and get into that directory

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

Step 7 : edit the index.html file

[root@linuxhelp reserved]# vim index.html

Step 8 : Set password for the user1

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

Step 9 : edit the test virtual host configuration

[root@linuxhelp reserved]# vim /etc/httpd/conf.d/test.conf

Step 10 : edit the htaccess file by seeing the below process

[root@linuxhelp reserved]# vim .htacess

Step 11 : Restart the apache service by running the below command

[root@linuxhelp reserved]# systemctl restart httpd.service

Conclusion :

We have reached the end of this article. In this guide, we have walked you through the steps required to password-protect directories with Apache on Rocky Linux 8.6. 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.