Let’s start with the obvious: if you run more than one Drupal site on a server, you should be running those sites in a Drupal multisite configuration.

This should make perfect sense from an administrative point of view: you share the same core Drupal installation, and if configured correctly, you can share one copy of commonly used modules and themes, too. This means with one drush or cvs command, you can update core Drupal (or modules, etc.) in one place on the server — and then every Drupal site that is configured underneath immediately shares the same update. It’s an immense administrative time-saving feature, and the best part is that it’s built-in — no hacks necessary!

If this is news to you, make sure to acquaint yourself with Drupal.org’s multisite documentation.

Getting your server to play along

To get Drupal multisite up and running, you will need to make two changes to the default server-side configurations. First, you need to configure the document root to point to the Drupal root for each virtual host or site. Second, you will need to modify mod_rewrite directives if you want Clean URLs (and you do).

Under Apache, both of these tasks are easy to do and quite well documented. However, despite Apache’s advantages in this respect, it isn’t always the best choice in terms of performance or memory usage. In comes Lighttpd, which excels at performance and enjoys a very light memory footprint.

Because Lighttpd’s configuration syntax is a big departure from Apache’s, the existing documentation can’t be translated for Lighttpd’s purposes. Here’s how to run multisite Drupal properly under Lighttpd.

A quick note on requirements

Make sure you’re running a recent version of Lighttpd, especially if you’re eager to get Clean URLs working under Drupal. (Trust me, you want to enable Clean URLs.) According to Lighttpd’s mod_rewrite docs, you’ll need to have version 1.4.24 or newer to have the required url.rewrite-[repeat-]if-not-file option.

Also, this code has been tested to work under both Drupal 5.x and Drupal 6.x, but should work under all newer versions unless the multisite configuration feature is removed from Drupal core (unlikely).

I’m also making some assumptions of familiarity with a standard Drupal installation, including getting your hands on Drupal core, putting it on the server, and configuring a standalone Drupal site. If not, first check out Drupal’s installation instructions.

I also assume that you’re a server administrator, or at least able to edit the Lighttpd configuration files (e.g. /etc/lighttpd/lighttpd.conf). Standard “don’t edit config files without a backup” warnings apply. Nothing we’re doing can’t be undone, but you’ll want backups.

Configure Drupal’s sites directory

Let’s pretend that you intend to share the following domains/sub-domains under Drupal:

  1. www.example1.com
  2. www.example2.com
  3. aaa.example1.com

First things first, we need to set up the Drupal sites directory correctly. Read multisite documentation for a more detailed explanation of the correct layout. For the examples above, this will be our sites directory under our base Drupal installation:

  1. sites/www.example1.com/
  2. sites/www.example2.com/
  3. sites/aaa.example1.com/

Underneath each directory above, you’ll want to deploy the standard site structure: settings.php, files directory, etc. You won’t be able to complete the installations just yet, so just make sure the files and directories are in the right place. We’ll get to the installations once the server is configured correctly.

Configure Lighttpd to point the hosts to Drupal multisite

Open your Lighttpd configuration file — if you don’t know where it is, a good guess would be /etc/lighttpd/lighttpd.conf. Let’s assume that you have not yet configured the example domains in Lighttpd. Add the following code block at the end of the config file:

$HTTP["host"] =~ "(www\.example1\.com|www\.example2\.com|aaa\.example1\.com)" {
  # set the document-root to point to your Drupal multisite base installation
  server.document-root = "/var/www/drupal"

  # deny access to drupal files & raw CVS and php code
  url.access-deny = ( "~", ".inc", ".engine", ".install", ".module", ".info", ".sh", ".sql", ".theme",
    ".tpl.php", ".xtmpl", "Entries", "Repository", "Root" )
}

Save your configuration file and restart Lighttpd. You should now be able to navigate to www.example1.com/install.php and set up your new Drupal site. Do the same for all the other sites. You are now running your Drupal under a multisite configuration on Lighttpd.

That ugly regular expression

Eagle-eyed regular expression aficionados will undoubtedly cringe at that ugly regex in the code block above. Yes, I could have written it more succinctly, but some people are terrified of unreadable expressions such as: "(((www|aaa)\.example1)|(www\.example2))\.com"

Enable Clean URLs under Lighttpd

However, you will notice that the Clean URL test will fail during the installation process. We can fix this by adding a few mod_rewrite directives in the code block. Insert this code underneath the url.access-deny directive for your domains:

# make sure that your mod_rewrite module is enabled in Lighttpd
url.rewrite-if-not-file = ( "^/([^\?]+)(\?(.*))?" => "/index.php?q=$1&$3" )

Save your configuration file and restart Lighttpd. You should now be able to pass the Clean URL test for each of your domains.

Why rewrite-if-not-file is necessary

The rewrite-if-not-file option ensures that Lighttpd first checks to see if the requested URL is a real file or path, and only when the file doesn’t exist do we pass it off to Drupal. This protects calls to stylesheets, images, etc.

Before recent versions of Lighttpd, this option didn’t exist, and there was only a complicated way of passing off the request to a script that would then check if there was a real file or directory. Overhead is a bad thing, and we should all be very glad that this is now part of mod_rewrite. It also simplifies the configuration significantly.

And not to brag or anything, but achieving this file/directory check in Apache would require three separate mod_rewrite rules.

Conclusions

So, at this point, you should now have a working multisite Drupal under Lighttpd. Feel free to marvel at the speed of Lighttpd and the ease of multisite administration.