Thursday, 24 January 2013

How to Password Protect a Directory on Your Website

If you want to password protect a directory in Apache then we need to modify below file

    [root@localhost ~]#  vim   /etc/httpd/conf/httpd.conf



Now restart the Apache service 

        [root@localhost ~]# service httpd restart

        Stopping httpd:                                            [  OK  ]
        Starting httpd: httpd:                                    [  OK  ]
        [root@localhost ~]# 

Now we need to create ".htaccess" file under the folder ( /var/www/html/test ).

        [root@localhost ~]# vim  /var/www/html/test/.htaccess

        AuthName  "Caution! Authorized users only"
        Authtype  basic
        AuthUserFile  /etc/httpd/conf/passwd-test
        require  valid-user
            
                 "passwd-test"  is a file which will store the Passwords.

Now we will create passwd-test file using below command

         [root@localhost ~]#  htpasswd  -c  /etc/httpd/conf/passwd-test  manivel

New password: Re-type new password: Adding password for user manivel
 [root@localhost ~]#
 
-c  --> create new file
Manivel  --> username  ( based on system user)

passwd-test --> user name and password store file

If you want multiple user to access "test" directory you can create password using htpasswd command

         [root@localhost ~]#  htpasswd   /etc/httpd/conf/passwd-test  another-username
 
You can check user list 
         
         [root@localhost ~]#cat  /etc/httpd/conf/passwd-test
test:$apr1$yaW9ZSps$I9bCkCYgKhwkj1Lk0cD5s1
manivel:mX7oxMcGIoZdo
[root@localhost ~]#

Now we will check http://localhost/test  in browser. It will ask password prompt.

No comments:

Post a Comment