Wednesday, September 22, 2010

Password protect a directory using basic authentication

In this How-To guide, we will show you how to set up a password protected directory using basic authentication.

Authentication directives in Apache can be used in the following contexts - directory and htaccess. For directory context this means in , , and blocks in your httpd.conf or your distro's main Apache config file or virtual host config file. Additionally, for Apache 2.2, blocks are also included in the directory context. The htaccess context is self explanatory. This means you can use authentication directives in htaccess files. In this tutorial, we will show recipes for both contexts.

The first thing in this example we need to do is to create a directory to password protect in our document root. Let's say our document root is /var/www/html. We'll create a directory called protected in the document root - /var/www/html/protected.

The next thing to do is to create a password file with users. We will use the htpasswd utility provided in the core Apache package. The password file can be stored anywhere on your hard drive. In our example we will create our htpasswd file in /etc/htpasswd.

Note that the location of the htpasswd file can be anywhere you want on your local drive. You just need to specify the full path to the htpasswd file with the AuthUserFile directive. Choose whatever you deem to be a sane location for your password files.

/path/to/ is the full path to the htpasswd utility. The full path to the htpasswd utility is necessary if htpasswd is in a nonstandard location. After running the htpasswd command, you will be prompted to enter the user's password. Notice the difference between both commands. The first command uses the -c flag. This flag is used when creating a new htpasswd file. After that, the -c flag is not used for subsequent users you wish to add. Also, you need to make sure Apache has read access to this file, so make sure your permissions are correct.

This is the recipe to use for setting up a password protected directory in the directory context:

Now we need to create a block in httpd.conf or your distro's main apache config file or your virtual host config file in order to have Apache process this htaccess file.

AllowOverride AuthConfig # The Options below is an example. Use what you deem is necessary. Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec Order allow,deny Allow from all

Notice the AllowOverride line. It tells Apache to process the htaccess file and to allow htaccess to set the authentication for that directory.

Remember to restart Apache after making any changes to httpd.conf or your distro's main Apache config file or your virtual host config file.

Using either recipe, you can now go to http://localhost/protected and be prompted by the browser to enter your credentials. If you enter correct credentials you will be granted access to protected. If you don't enter correct credentials, you will be continually prompted to enter credentials until you enter correct credentials or click the Cancel button.

For more complete information on the Apache directives used, see the Apache Docs.