Friday, November 2, 2007

apache password protecting directory

What do you need

1. File having password

you can create password file manually with constants like

username1:encryptedpassword1
username2:encryptedpassword2
username3:encryptedpassword3

The password encryption is the same as you'll find in PHP's crypt() function.

or you can use sites like
http://home.flash.net/cgi-bin/pw.pl
or
http://www.htaccesstools.com/htpasswd-generator/

Eighter you can go with command
htpasswd -c passwordFile user_name

(where user_name is a name of user you want to create and
passwordFile is file name taha will generate)

The system will ask you to enter the password for this user. It will then ask you a second time to confirm your typing.
Continue to add new users, but with this version of the command. The -c option is only for the initial creation of the file.

htpasswd passwordFile new_name

YOU HAVE COMPLETE 1st Requirement

NOW you have two ways to apply that passwords

using .htaccess file OR using httpd.conf file

using .htaccess

Create a file called .htaccess (lower case letters of course- with the leading period) that looks just like this in the folder you want to protect:
AuthUserFile passwordFilePath
AuthName "The Secret Page"
AuthType Basic


require valid-user



using httpd.conf

edit it with contents


AuthType Basic
AuthName "admin"
AuthUserFile "passwordFilePath "
require valid-user


now simply restart the apache service

service httpd stop
service httpd start

ONE IMPORTANT THING "DON'T FORGET TO RESTART BROWSER TOO"

2 comments:

akashtakyar said...

Were you able to make the EC2 password protected?

Hardeep Singh said...

you can see working example at

http://ec2-67-202-14-198.z-1.compute-1.amazonaws.com/ls