Wednesday, March 5, 2008

Manually Password Protecting a Directory in Apache

Password Protecting a Directory in Apache

The following procedure details how to password protect a directory on a *nix server running Apache. If you have a control panel such as Plesk or cPanel, this can be handled from within the panel, but for a plain installation you will need to follow the steps below:

Use cd to move to the directory that you will be protecting.
>cd /home/user/directory

Create a file called .htaccess with the following contents. If .htaccess already exists you can simply add this to the end of the existing file:

>vi .htaccess

AuthName "Login Message"
AuthType Basic
AuthUserFile /home/user/directory/.htpasswd
AuthGroupFile /dev/null

require user user-name


“Login Message” should be replaced with the message that you want to show in the login dialog box that the browser will show. /home/user/directory/ should be the same path that your .htaccess file was created in. user-name needs to be replaced by the user that will be able to access the folder.

The last step is creating a .htpasswd file for the folder. This file stores the password for folder in an encrypted format:

>htpasswd -c .htpasswd user-name

You will be asked to enter and then confirm the password.

No comments: