Adding authentication to your website
Page Protection
Apache offers several options for adding a password-protected area to a website.
If you want to offer login access to restricted web pages, you don't need a MySpace account or a big corporate website. Apache provides several convenient alternatives for supporting user authentication. Although these login options require a few extra configuration steps, you can easily protect your pages without the need for add-on, proprietary applications. In this article, I will describe some techniques for password-protecting your pages.
The Apache web server goes through three phases to determine whether the current user is allowed to view the requested resource. The Access phase checks to see whether the requesting IP address is allowed to view the resource. The Authentication phase verifies that the username provided matches the password associated with the user. The Authorization phase is usually used to support user groups for easier administration. With a bit of custom coding, I will explain how you can extend any of these phases to do whatever you want.
Unless your connection is over SSL, all of these methods will send your password in the clear. Using SSL for all pages that require authentication is highly recommended.
Examples in this article were tested with Apache 2.2.8. If you are using an earlier version, some of these options might not be available or the configuration could differ slightly.
File-Based Authentication
File-based authentication, which is sometimes referred to as Basic Auth or htpasswd auth, is the most common technique for supporting user login in Apache. For example, if you want to protect all of the administration pages of our site, you can invoke file-based authentication with the following code in your Apache configuration file. (The Apache configuration file is located in different places on different systems. On Red Hat/Fedora systems, it is /etc/httpd/conf/httpd.conf, and it's /etc/apache2/apache2.conf on Ubuntu.)
<Location /admin> AuthType Basic AuthName "Admin Pages" AuthUserFile /path/to/our/password-file AuthBasicProvider file Require valid-user </Location>
If you're using Apache 2.0.x, you will need to remove the AuthBasicProvider directive.
After you set the AuthType to Basic, name this area (e.g., Admin Pages) so users know what they are logging into. Also, you must tell Apache where to find the password file for this area.
The final step is the Require directive, which tells Apache to allow any valid user in the file.
Apache comes with a program called htpasswd that helps you create and update the password file. Initially, you create the file with:
htpasswd -c /path/to/our/password-file <username>
After the file is created and you just want to add more users or change their passwords, use the same htpasswd command, but without the -c option. If you need to remove a user, you can either edit the file by hand in a text editor or use htpasswd with the -D option to delete the user.
Also, you could use this method (and the other methods I'll discuss later) with directives such as <Directory>, <DirectoryMatch>, <LocationMatch>, or <FilesMatch>. For example, you could require a password to view any .gif files on your site with:
<FilesMatch "\.gif$"> AuthType Basic AuthName "Images Require a Password" AuthUserFile /path/to/our/password-file AuthBasicProvider file Require valid-user </FilesMatch>
In addition to allowing all users in the password file into an area, you can also restrict access down to specific users.
The configuration in Listing 1 grants steve and bob access to the /admin section with the Require user entry, followed by the space-separated list of users. However, any authenticated user who logs in succesffully can access /content.
Listing 1
Restricting User Access
Another useful feature is that you can grant access to specific groups rather than individual users. To do this, create a group file. Listing 2 shows the preceding example using groups. The format of the group file is very simple:
<group name>: <user1>... <userN>
For this example, the contents of the file would be:
admin: steve bob
In this way, you can create as many groups as you like – one per line. The use of groups is a great way to keep your maintenance time down and make your configuration easier to read and understand.
Listing 2
Working with Groups
Note that it is important to make sure your password and group files aren't inside your Apache's DocumentRoot; otherwise, anyone could download them.
Authenticating with an SQL Database
Maybe your site already uses software that requires a password – a forum perhaps – and after you have several users in your forum, you decide to add another section to your website that is only for registered forum users.
Although you could have all of your users re-register with some form of file-based authentication (as described previously), this could turn into a maintenance nightmare, creating two places to add and modify users. Furthermore, some users might end up with different usernames and passwords for different parts of the site.
If your forum uses an SQL database and stores the password, you can configure Apache to access the password information from the database. The existing software will have to store its passwords with the same one-way hash as Apache – in this case, the crypt() function. Also, you'll need to enable two Apache modules: mod_dbd.so and mod_authn_dbd.so. To enable these modules, add these lines to your Apache configuration file:
LoadModule dbd_module modules/mod_dbd.so LoadModule authn_dbd_module modules/mod_authn_dbd.so
Note that the second parameter is either a relative or a full path to the module itself; the exact value might differ on your system, depending on your distribution or how you compile Apache.
After those modules are loaded, you need to configure Apache to reference your SQL database to retrieve passwords. If you're using a PostgreSQL database, your configuration would look something like Listing 3. First, you must define the database driver – in this case, pgsql – for the PostgreSQL database. If you're using a MySQL database, set this driver to mysql. Apache also has built-in support for Oracle, SQLite2, and SQLite3 databases.
Listing 3
Authenticating with SQL
After defining the driver, you must pass the driver some parameters about how to connect to the database. In the case of MySQL or PostgreSQL, you need to pass the database host, database name, user, and password.
The next few lines should be familiar by now; the only thing different is that AuthBasicProvider is set to use the dbd module. Setting up the actual user password query comes last. Because every database differs in table and column names, you must instruct Apache on how to go about retrieving a password from the database.
Authenticating with LDAP
Another of the more popular authentication modules is mod_authnz_ldap, which allows you to authenticate your website against an LDAP server. LDAP authentication can be very useful in corporate environments in which a central LDAP server handles all authentication across the company.
Enable Apache's LDAP module with:
LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
To configure your Apache server to authenticate users with LDAP, you need to set the LDAP URL:
<Location /content> AuthLDAPURL "ldap://ldap1.company.com/ou=People, o=Company" Require valid-user </Location>
Also, you can define redundant LDAP servers for fault tolerance by just adding another server to the URL:
<Location /content> AuthLDAPURL "ldap://ldap1.company.com ldap2.company.com/ou=People, o=Company" Require valid-user </Location>
The LDAP modules offer several options. By ensuring that the user has certain LDAP attributes, you can include group information and restrict access for a variety of situations.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.