How to Set a Password to Protect the Apache's Document Root Directory Using .htaccess File

Set a Password to Protect the Apache's Document Root Directory on CentOS 7.6

Apache’s Document root directory On CentOS 7.6 is /var/www/html.In this video, it covers about how to protect the apache’s Document root directory using htaccess On CentOS 7.6.

Requirements : A Working Apache Web Server

Procedure:

1)Configuration File

2)Create login credentials outside of the document root directory

3)Tell Apache to request a password when someone tries to access Document Root Directory

Let us start the installation process by first modifying the main configuration file of Apache’s server accordingly.

[root@linuxhelp ~]# vim /etc/httpd/conf/httpd.conf 
Directory "/var/www/html">
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks Multiviews
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    AllowOverride All
    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory> 

Now, to generate the username and password out of the apache’s Document Root Directory, I've used the Hoem directory in the following manner.

[root@linuxhelp ~]# cd /home

Here you need to create a user to protect the document root directory.

[root@linuxhelp home]# mkdir user2

And then, generate a username and password using htpasswd command as done in below.

[root@linuxhelp home]# htpasswd -c /home/user2/passwd user2
New password: 
Re-type new password: 
Adding password for user user2

Once it is done, you need to change the directory to Apache’s document root directory.

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

Create a file named .htaccess to make Apache prompt whoever tries to access the Apache’s document root directory.

[root@linuxhelp html]# vim .htaccess
AuthType Basic
AuthNAme  "restricted Acces"
AuthUserFile /home/user2/pass
Require user user2

Restart the service of Apache.

[root@linuxhelp html]# systemctl restart httpd

Once all the configuration is done, open your browser and enter the IP address of the Apache server then hit enter.

After that, enter the Login credentials That you have set using htpasswd command when Apache server prompts the request.

Switch Over to the terminal,Open the passwd file that resides under the home/user2

[root@linuxhelp html]# cd /home
[root@linuxhelp home]# cd user2
[root@linuxhelp user2]# vim passwd
user2:$apr1$20redQCe$XVSVDmoQ4V3df0jsmqI2L/

Thus, protection of Apache’s Document root directory on CentOS 7.6 using .htaccess Comes to end.

FAQ
Q
Why is Authconfig set in the .htaccess file in web Server On CentOS 7.6?
A
AllowOveride is valid only in sections. Authconfig is used for the authorization directives In the .htaccess On web server.
Q
Why is Authconfig set in the .htaccess file in web Server On CentOS 7.6?
A
AllowOveride is valid only in sections. Authconfig is used for the authorization directives In the .htaccess On web server.
Q
Why is -c option used after htpasswd on CentOS 7.6?
A
-c option encrypts the password that we have set and saves it in the file that we have created to store the password.
Q
What is the use of htpasswd command on CentOS 7.6?
A
htpasswd function allows users to generate a password and a username On CentOS 7.6
Q
What are the facilities that a .htaccess has on Web server?
A
The facilities .htaccess has on a Web server are Redirection, URL rewriting, Service Custom Error Page, REstricting access to specific resources, Block Access to certain Entities and deny requests based on user-agent.