AAPT Home > Support Information > Web Hosting > Password Protection

Adding password protection to your Unix web site

Introduction

Getting it to work

Step One: setup a .htaccess file
Step Two: create a password file
Step Three: test it out

Further Information


Introduction

By default, the files on your web site are accessible to everyone. While this is suitable for most web sites, you may wish to password protect areas of your site so that only authorised users may gain access. With your Unix based AAPT Web service, this can be done by created or editing a file called .htaccess. This file is used to make changes to the Apache web server software that only affect your web site.

The method of password protection described below is called "Basic Authentication". When a visitor tries to access a protected area of your site, she will be prompted for a username and password. If authentication succeeds, she will be able to access that part of the site until she closes her browser. This because the browser resends the username and password each time it requests a file.

The configuration options in a .htaccess file apply to the directory and all its subdirectories. Apache will look for .htaccess in every directory, so you could (for example) specify an ErrorDocument that applies to your whole site and require authentication for a portion of your site.

This document only descibes authentication based access control. Another method of access control is allowing or denying access based on IP address.AAPT strongly recommends against using IP based access control, since it is a weak form of security, since the source IP address will vary depending on whether end users go via a proxy.

Getting it to work

In this example, we will protect a directory called members, located directly under the document root (htdocs). The sample site used is www.example.com, and its FTP login name is example.

Step One: Set up .htaccess

Where to put .htaccess: The .htaccess file should be placed in the directory you wish to protect. In this example, we upload it to /www.example.com/htdocs/members/

Create or update: In this example, we assume there is no .htaccess file already. If there is, don't delete it, but instead modify it by adding the lines below. The parts you can modify to fit your site are shown in italics.

AuthUserFile /import/clients/e/example/www.example.com/.htpasswd
AuthGroupFile /dev/null
AuthName "Example Member Area"
AuthType Basic
require valid-user

Now to explain the above configuration directives in more detail:

Step Two: create a password file

To authenticate a user, Apache checks the username and password entered by the user against details in a text file. The name and location of the password file is specified within .htaccess with AuthUserFile.

Each line in the password file contains username and password pairs. The password is stored in an excrypted format and is separated from the username by a colon (:).

The usual way to create a password file is to use a command called htpasswd. These steps show how to make a password file called .htpasswd:

  1. Telnet to hosting.connect.com.au and log in with your site's username and the requested One Time Password.

  2. Go to the directory in which you want to create the password file. In this example, we use: "cd /www.example.com/".

  3. To create the password file, type "htpasswd -c .htpasswd username". The "-c" options tells htpasswd to create the file; omit it if you wish to add a username to an existing password file.

  4. You will then be prompted to enter a password for the specified username and enter it again to confirm.

  5. If desired, you can add further usernames with "htpasswd .htpasswd username"

Step Three: test access to the directory

The directory you have protected (and its subdirectories) should now only be accessible if a valid username/password pair is entered. To test it:

  1. In your browser, go to the URL for the protected directory. In this example: http://www.example.com/members/
  2. Enter an invalid username: you should be denied access.
  3. Enter a correct username and password: you should be granted access.

If you get an error, or you can't get access with the correct username and password, you will need to review the configuration you created in the above two steps. Common errors that prevent successful authentication include:

Further Information

Contact AAPT Support if you have questions, or encounter problems. While it is your responsibilty to maintain your chosen method of access control, we may be able to help by pointing out possible errors.

The Apache documentation is an invaluable resource, which explains all directives including AuthUserFile, AuthType, require, etc. Particularly useful for this topic is the article on "Authentication, Authorization, and Access Control" available at: http://httpd.apache.org/docs/howto/auth.html




Copyright © AAPT Limited