Tag Archive for 'server'

Drupal Multisite in lighttpd

I’m currently investigating switching from Apache to Lighttpd, and in the process I decided I’d have a go at simplifying my virtual host configuration. Lighttpd’s config files are already simpler than Apache’s, but I figured I could move from a separate config file for each virtual host to specifying settings in groups. Perhaps this is also possible with Apache, but I’ve not tried as one file per vhost was working fine.

I also need more advanced URL rewriting than lighttpd’s mod_rewrite offers, as some Drupal modules rely on it (Apache has such URL rewriting inbuilt in its mod_rewrite). This part of the switch is simple, someone else has already done the hard work: installing lighttpd-mod-magnet and setting up this script is all that needs to be done (instructions are in the script, near the top).

I wasn’t happy with either of the vhost modules provided by lighttpd, they appear to only work well if all of your sites are laid out in your filesystem in exactly the same way. I could have worked with that and created symlinks from my drupal dir to each site’s name in the vhost dir, but that feels messy, and would mean I could not set up access rules for the /sites directory.

Enter include_shell. This beautifully simple Lighttpd configuration option enables configuration to be generated by a script. My /etc/lighttpd/conf-available/15-drupal6.conf has one line:

include_shell "/etc/lighttpd/shell/drupal6"

The drupal6 script is where the magic happens:

#!/bin/sh
# Script for generating drupal multisite niceness

# Space separated list of disabled sites:
DISABLED=""

# Space separated list of sites with custom config files:
CUSTOM=""

# Location of custom config files:
CUSTOMDIR="/home/matt/Websites/testing/customconfig"

DRUPAL_DIR="/home/matt/Websites/testing/drupal6"
DRUPAL_LUA_PATH="/home/matt/Websites/tools/drupal/drupal.lua"

#Don't edit this, edit the other DISABLED="" VAR up there ^
DISABLED="all default "$DISABLED

for VHOST in `find ${DRUPAL_DIR}/sites/ -mindepth 1 -maxdepth 1  \( -type d -or -type l \) -exec basename "{}" \;` ; do
    for SITE in $DISABLED ; do
        if [ $SITE = $VHOST ]
        then
            #Just calling continue here can't work, we're inside a child loop
            DIS=y
        fi
     done

    if [ "$DIS" = "y" ]
    then
        DIS=""
        continue
    fi

    echo "\$HTTP[\"host\"] =~ \"$VHOST\" {
          var.vhost_name = \"$VHOST\"
          server.document-root = \"$DRUPAL_DIR\" 

          magnet.attract-physical-path-to = (\"${DRUPAL_LUA_PATH}\")

          \$HTTP[\"url\"] =~ \"^/sites/.*\" {
          \$HTTP[\"url\"] !~ \"^/sites/(`echo $VHOST| sed 's:\.:\\\.:g'`|all)/.*\" { url.access-deny = (\"\") }
          }"
          # This allows access to /sites/all and /sites/(this domain) which is all we need access to

    for SITE in $CUSTOM ; do
        if [ "$SITE" = "$VHOST" ]
        then
            cat $CUSTOMDIR/$SITE
        fi
    done

    echo "}"

done

The variables in the script should be self explanatory, though the $CUSTOM variable is untested – it’s just there in case I have a need for it.

Lighttpd parses the output of the script as part of its configuration. As I’ve included a magnet configuration in each vhost using this script it’s not needed anywhere else (you just need to enable the magnet module).

This setup won’t work with drupal installations in subdirectories, but I don’t use subdirectories for my drupal sites, so that’s not an issue for me.

Hopefully others will find this useful ⢁)

Update: I’ve replaced the alias lines with a url.access-deny statement, which actually works, with help from darix on irc.freenode.net/#lighttpd