Lighttpd with Apache

One of my websites is experiencing phenomenal growth and after optimizing the database queries and using memcached, I decided it was time to use another webserver to serve static media. Here’s how to do it.

I recommend that you take a look on a previous post that I wrote about how to configure a webserver to work with Django because I will be manipulating some files which I talked about there. If you’re unsure about the correct paths necessary for your server, use the other post as a guide. I installed Lighttpd and configured Apache on my Slicehost VPS, which is a company that I highly recommend. If you decide to sign-up with them, it would be awesome if you use my email ([email protected]) as a referral, and/or use this link which takes care of setting me as your referral.

We’re going to use Lighttpd on a different port than Apache to serve files from a specific directory based on the domain name, all transparent to the users browsing your website. No changes were made on my website code, server (Python) or client (HTML) side.

Install Lighttpd

My server runs Ubuntu so I just ran the following command to install Lighttpd:

$ apt-get install lighttpd

Edit Lighttpd’s configuration file:

$ vi /etc/lighttpd/lighttpd.conf

Uncomment the server.port line (mine was the 60th):

server.port               = 81

Enable the mod_evhost module. This will help us use a different directory depending on the domain name. Just uncomment the 19th line (inside server.modules).

Add the following somewhere, which says requests from should look for files on /var/www/ I did on the 118th line, after the evhost.path-pattern examples.

$HTTP["host"] =~ "yourdomain\\.com" {
    evhost.path-pattern = "/var/www/"

Start Lighttpd:

$ /etc/init.d/lighttpd start

Point your browser to your server to see if Lighttpd is working:


You need to see something like this (click the image for a larger version):

Lighttpd default page

If you have directories under /var/www/, append its names to the url to see if lighttpd is correctly serving them (it should):


Configure Apache

To transparently serve files using Lighttpd from Apache, you need to enable proxy on Apache:

$ ln -s /etc/apache2/mods-available/proxy.load /etc/apache2/mods-enabled/
$ ln -s /etc/apache2/mods-available/proxy.conf /etc/apache2/mods-enabled/
$ ln -s /etc/apache2/mods-available/proxy_connect.load /etc/apache2/mods-enabled/
$ ln -s /etc/apache2/mods-available/proxy_http.load /etc/apache2/mods-enabled/

Unsecured proxies are used by spammers so Apache comes guarded against bad configurations. You need to edit this file:

$ vi /etc/apache2/mods-available/proxy.conf

To say where the proxy will work and who has access to it. Replace the * with, and uncomment the Allow from line. Final result is this:

        AddDefaultCharset off
        Order deny,allow
        Deny from all
        Allow from

Configure the VirtualHost

$ vi /etc/apache2/sites-available/

Add this somewhere inside <VirtualHost>:

ProxyRequests Off
ProxyPreserveHost On
ProxyPass /web
ProxyPassReverse /

You’re done with all configurations. Now reload the Apache’s modules and restart the server:

$ /etc/init.d/apache2 reload
$ apache2ctl restart

Everything should be working. If you get an error, try to undo some steps and after each change, reload Apache and restart Apache and Lighttpd to see when it broke. If you browse to http://your.ip.address, you should see all your content from /web/ being loaded correctly. How do you tell it’s being served by Lighttpd instead of Apache?

This should tell you Apache is serving the page:

curl -I http://your.ip.address/

While this should tell you Lighttpd is serving the file:

curl -I http://your.ip.address/web/some.file

If you have any doubts or have problems, please comment and I will try to help you.






14 responses to “Lighttpd with Apache”

  1. Animesh Avatar


    My doubt has been.. should we have lighttpd (or any other fast/light webserver) proxying the dynamic content to the heavy webserver, or should the heavy webserver be proxying to lighttpd (as you’ve done it).

    What would be better. lighty in front of apache or apache in front of lighty??

  2. inerte Avatar

    Hi Animesh! I have no idea 🙂 I guess that’s a good question, and it deserves some benchmarks. Sadly I won’t have enough time over the next weeks, otherwise I would do them myself.

    Personally, my bet is that it doesn’t matter. Either way, requests will be passed to each web server, so I guess they would work the same. The only difference is who’s doing the proxy, something that’s probably very fast on both servers.

  3. Reza Avatar

    lighttpd is faster and can handle more requests per second and consumes less resources, Apache is a heavy process and consumes much resources, so it is better to proxy from lighttpd to apache.

  4. Julian Avatar

    Why don’t just use lighty directly, I mean specifying all links with port 81? Would that be bad?

  5. inerte Avatar

    Not bad, just that the images would have 81 at the URL. It’s just prettier without it.

  6. Julian Avatar

    That’s why I made it port 8080 now 😉
    Works like a charm with lighty, and now one looks at the URLs anyways.

  7. viru Avatar

    Some Internet providers gives more bandtwith on port 80 and less on any other ports. I think that you should use proxy or separate IP with port 80 for static files.

  8. Adam Avatar

    Thanks so much, I had been pulling my hair out trying to do this for hours, just got it working thanks to your instructions 🙂

  9. Jorge Diaz Avatar

    i am moving from mediatemple to slicehost, i could need some help. Maybe i can signup through your link, send me a mail to talk about this.


  10. Erik Allik Avatar
    Erik Allik

    I don't see the part where I tell lighty to connect to Apache.. am I missing something? Is there some sort of magic going on?

  11. Alvaro Avatar

    Thank you ;). Good post!

  12. […] I’ve written how to configure Apache, mod_python and Django and how to put lighttpd behind Apache. […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.