• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

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

  • 00:42 vim /etc/httpd/conf/httpd.conf
  • 01:28 htpasswd -c /home/user2/passwd user2
  • 01:57 cd /var/www/html
  • 02:00 vim .htaccess
  • 02:42 vim passwd
{{postValue.id}}

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.

snap1

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

snap2

snap3

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.

Tags:
grayson
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

What is the purpose of .htaccess In Apache web server on CentOS 7.6?

A

.htaccess files are simple text files which configure Apache Web server.

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.

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

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

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.

Load more

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Ganesh Konka ?
Zentya 6.1 http proxy configuration

please send link for creating zentyal 6.1 for http proxy and firewall as gateway.

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.