you're reading...

Apache – Basic Authentication

When I was searching my internal wiki I stumbled upon some useful pages that I had written. Here is the first among many that I wrote.

There are two configuration steps which you must complete in order to protect a resource using basic authentication. Or three, depending on what you are trying to do.

  1. Create a password file
  2. Configure the Server to use the password file for processing authentication requests

Create a password file

htpasswd -c <location to password file>  <username>

For example:

htpasswd -c /etc/apache2/.passwords sampleUser

htpasswd will prompt you to enter a password, and then ask you confirm it. Note the use of the -c flag in the command shown above. This flag tells htpasswd to create the file /etc/apache/.passwords. This is only required when you need to create a new password file. Subsequent users can be added to this password file without the use of the -c flag. Note that using the -c flag with htpasswd on an existing file will clear the file’s contents completely and create a new one. So be careful while using this flag.

Please note that the command is htpasswd2 in Suse Linux

Configure the Server

Once you have created the password file, you need to inform Apache about it, and also configure it to this file in order to process user credentials for admission. This configuration is done with the following directives:

Authentication type being used. In this case, it will be set to Basic

The authentication realm or name

The location of the password file

The location of the group file, if any

The requirement(s) which must be satisfied in order to grant admission

eg: Add these lines in your httpd.cong file using Directory tag withe full path you want to secure

AuthUserFile /etc/apache2/.passwords
AuthName "This is a protected area"
AuthGroupFile /dev/null
AuthType Basic
Require valid-user

Once the configuration is complete, restart Apache.



No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: