Apache to Lighttpd migration

Lighttpd is smaller and faster alternative to Apache web server. You can handle bigger traffic with the same memory and CPU constraints (it's important on virtual servers where resources are limited). Let's see how we can convert existing Apache+FastCGI stacks into Lighttpd:

Global options

There's special syntax to add options to all virtuals:

global {
    dir-listing.activate = "disable"
    server.follow-symlink = "enable"

In above example two config values are set: one is responsible for disabling directory listings ("Options +Indexes" in Apache), second for FollowSymlink counterpart.

Declaring virtual hosts

Lighttpd uses conditionals to specify configuration in general and the same strategy applies for virtuals definitions:

$HTTP["host"] =~ ".*aplikacja.info" {

Above syntax matches domain and all subdomains. Pretty simple isn't it?

mod_rewrite rewritten

Let's see how RewriteRules could be translated into Lighttpd configuration syntax:

url.rewrite-once = (
    "^/$" => "/index.php",
    "^/\?(.*)$" => "/index.php?$1",

Above code converts URL without script path to /index.php and any other url into /index.php?ANY-URL.

Old school cgi-s

Sometimes you want (have to) call scripts using good old protocol: CGI (Common Gateway Interface). It's inefficient method (process creation overhead every request), but have one benefit: memory is freed directly after proces finish. Example:

cgi.assign = ( ".cgi" => "" )

This syntax defines that any script with given extension will be executed as CGI. "" means we use default script interpreter (declared by #!/path/to/file syntax).

Hide some files from HTTP

Sometimes you want to block access by HTTP to only specified URLs (and do not allow, for instance, to download you config file with database login & password). This can be achieved by the following syntax:

$HTTP["url"] !~ "^/index.php|^/static" {
    url.access-deny = ("")

In above example only /index.php and all content from static subdirectory is available (other URLs will give 401 unauthorized error).

PHP in FastCGI mode

It's very easy to setup PHP in FastCGI mode in Debian:

lighttpd-enable-mod fastcgi

Above command symlinks /etc/lighttpd/conf-available/10-fastcgi.conf to /etc/lighttpd/conf-enabled/10-fastcgi.conf. You must restart Lighttpd in order to see effects. Lets dig inside this config file:

server.modules   += ( "mod_fastcgi" )

## Start an FastCGI server for php (needs the php5-cgi package)
fastcgi.server    = ( ".php" =>
    "bin-path" => "/usr/bin/php-cgi",
    "socket" => "/tmp/php.socket",
    "max-procs" => 1,
    "idle-timeout" => 20,
    "bin-environment" => (
    "PHP_FCGI_CHILDREN" => "1",
    "PHP_FCGI_MAX_REQUESTS" => "10000"
    "bin-copy-environment" => (
    "PATH", "SHELL", "USER"
    "broken-scriptfilename" => "enable"

This entry was posted in en and tagged , . Bookmark the permalink.

2 Responses to Apache to Lighttpd migration

  1. Very useful, thank you. Have you done the above with an actual application? I'd be much interested to see some benchmark results and how much Lighthttpd is faster than Apache.

  2. admin says:

    Unfortunately production installation with Lighttpd on a VPS (a2b2 disaster few months ago) was moved to shared account, so currently no benchmarks are possible. I hope to install LH on my next VPS and will share some measures.

Comments are closed.