Apache Webserver is the most popular web server and has a market share of around 60%. Here I will explain a small but very useful feature of the apache web server - which is restricting access to (a part of) your website to only the privileged few by implementing username and passwords.There are two ways of restricting access to documents.
- Either by the hostname of the browser being used
- By asking a username and password
Here I will explain the second method - ie, assigning username and passwords to users who are authorized to access the documents. This is known as user authentication.Setting up user authentication takes two steps :
- Create a file containing username and passwords - Apache webserver has a utility called htpasswd to create the file containing username and passwords. Here I am creating a file called 'users' in the /usr/local/etc/ directory.
The first time you run the htpasswd utility, you run it using the -c flag as follows:
# htpasswd -c /usr/local/etc/users gopinath
The -c argument tells htpasswd to create a new users file. When you run the above command, you will be prompted to enter a password for the user gopinath, and confirm it by entering it again. Other users can be added to the existing file in the same way, except that the -c argument is not needed. The same command can also be used to modify the password of an existing user.After adding a few users, the /usr/local/etc/users file might look like this :
gopinath:WrU90BHQai36The first field is the username and the second field is the encrypted password.
Now comes the Apache server's configuration part. Open the apache server's configuration file /etc/httpd/conf/httpd.conf (your configuration file will be in different location depending on the distribution you are using) and look for the line :
AllowOverride NoneAnd change it to
If you want to protect the document root itself, create a file '.htaccess' in the top directory path and include the following lines to it:
AuthName "Only Valid Access"
AuthName - directive specifies a realm name for this protection. Once a user has entered a valid username and password, any other resource within the same realm name can be accessed with the same username and password.
AuthType - directive tells the server what protocol is to be used for authentication. At the moment, 'Basic' is the only method available. However, a new method, 'Digest' is about to be standardized, and once browsers start to implement it, Digest authentication will provide more security than Basic authentication.
AuthUserFile - tells the server the location of the user file created by htpasswd utility. A similar directive, AuthGroupFile, can be used to tell the server the location of a group's file.
Lastly don't forget to restart the web server which rereads the changed configuration files :
# service httpd restart
From here on when you try to access your protected site or the directory via the web browser, you will be asked for authentication first. And only those users whose name has been entered in the file pointed to by the AuthUserFile directive will be allowed access to your site.