Drupal 7 on Lighttpd with rewrite

Apache is a well known web server that I have used quite a lot. It does however have it’s problems. First of all Apache is a memory hog of dimensions and is not working very well on low memory servers like Virtual Private Servers (VPS). Second it’s quite very mainstream so some security holes are discovered. And third, it is very big and therefore also very slow.

As I have currently been experimenting with a VPS, I have been working on making Drupal 7 working properly on Lighttpd, with pretty URLs / mod_rewrite enabled with quite good results. I have found no best practices for running Drupal on Lighttpd, so here is my configuration.

My setup

Operating System Debian 6 – 64 bit
Web server Lighttpd 1.4.28
PHP version 5.3
PHP modules imagick, mysql, mysqli, cgi-fcgi, gd, curl, iconv, mcrypt, pdo, pdo_mysql

Configuring lighttpd with php and rewrite

server.modules = (
        "mod_access",
        "mod_alias",
        "mod_compress",
        "mod_redirect",
        "mod_rewrite",
        "mod_fastcgi"
)

server.document-root        = "/var/www"
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log"
server.pid-file             = "/var/run/lighttpd.pid"
server.username             = "www-data"
server.groupname            = "www-data"

index-file.names            = ( "index.php", "index.html",
                                "index.htm", "default.htm",
                               " index.lighttpd.html" )

url.access-deny             = ( "~", ".inc" )

static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

include_shell "/usr/share/lighttpd/use-ipv6.pl"

dir-listing.encoding        = "utf-8"
server.dir-listing          = "disable"

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/x-javascript", "text/css", "text/html", "text/plain" )

include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

fastcgi.server = ( ".php" => ((
                "bin-path" => "/usr/bin/php5-cgi",
                "socket" => "/tmp/php.socket"
        ))
)

There are some changes from the default configuration file. First of all the modules mod_rewrite and mod_fastcgi is enabled. Then PHP is configured as a fastcgi server module.

The site configuration

$HTTP["host"] =~ "^goodworkaround\.com$" {
        server.document-root = "/home/marius/websites/drupal7/"
        server.errorlog = "/var/log/lighttpd/goodworkaround.com.error.log"
        accesslog.filename = "/var/log/lighttpd/goodworkaround.com.access.log"
        server.error-handler-404 = "/e404.php"

        url.rewrite-if-not-file += (
                "^/([^.?]*)\?(.*)$" => "/index.php?q=$1&$2",
                "^/([^.?]*)$" => "/index.php?q=$1",
                "([a-zA-Z\-\.\/\?\=\&]*)" => "/index.php"
        )
}

The important part here is the rewrite rules that you will need to make pretty URLs work. This is my own creation, so please refer to this page if you copy it and post it somewhere. 😉

This rewrite rule will work with things like overlay, image styles etc, all with pretty URLs enabled.

Have fun!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s