To protect a directory under Apache Web Server we need to use .htaccess & .htpasswd. So here i describe how an we do it :
Step 1 : Create new Directory or We can use existing directory suppose we have a directory as “dev”
Step 2: Create .htaccess or open existing .htaccess by editor under dev directory and put this text:
AuthName "My Files"
Step 3 : Then we need to create .htpasswd file in /root or somewhere and must be accessible and open with editor put this text:
So heres the big question how can we create encrypted password for .htpasswd. We can do this using a command like:
htpasswd /full_directory_path/.htpasswd user_name
so it will create an encrypted password against user_name and update into .htpasswd file. More information about htpasswd command is here http://httpd.apache.org/docs/2.2/programs/htpasswd.html. Otherwise you can find many online tools create password like:
Here i mention some important notes & troubleshooting topics:
- .htpasswd Format: username:encrypted_password
- case sensitive username i mean user_name, user-name, User_Name totally different
- Location of .htpasswd its depend on you but recommend outside of your web documents accessible area.
- Make sure .htpasswd path is correct i mean in .htaccess file AuthUserFile is full path of .htpasswd
- Make sure about permission & you can change like those command
Shell12chmod 0644 .htaccesschmod 0644 .htpasswd
- It doesn’t seem to be Working – Error 404? so you need to below line top of the .htaccess file
Apache1ErrorDocument 401 default
So i hope it will work as like you want.