Lighttpd with Apache

Comments

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 the previous post That I wrote about how to configure the 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 Apache configured on my Slicehost VPS, Which Is a company That I highly recommend. If you decide to sign-up with Them, it would b awesome if you use my email (inerte@gmail.com) as a referral, and / or use this link Which takes care of setting your referral to me.

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. Were made no changes on my website code, server (Python) or client (HTML) side.

Install Lighttpd

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

  $ Apt-get install lighttpd 

Edit Lighttpd's configuration file:

  $ Vi / etc / lighttpd / lighttpd.conf 

Server.port Uncomment the line (mine Was the 60th):

  server.port = 81 

Mod_evhost Enable the module. Will this 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 yourdomain.com Should look for files on / var / www / yourdomain.com / web /. I did on the 118th line, after the evhost.path-pattern examples.

  $ HTTP ["host"] = ~ "yourdomain \. Com" (
     evhost.path-pattern = "/ var / www / yourdomain.com / web /"
 ) 

Start lighttpd:

  $ / Etc / init.d / lighttpd start 

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

http://your.ip.address:81/

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

Lighttpd default page

If You Have directories under / var / www /, ITS names to append the url to see if lighttpd is serving Correctly them (it should):

http://your.ip.address:81/yourdomain.com

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 work Will and Who Has access to it. Replace the * with 127.0.0.1, and uncomment the line Allow from. End result is this:

  <Proxy 127.0.0.1>
         AddDefaultCharset off
         Order deny, allow
         Deny from all
         Allow from 127.0.0.1
 </ Proxy> 

Configure the VirtualHost

  $ Vi / etc/apache2/sites-available/yourdomain.com 

Add this somewhere inside <VirtualHost>:

  ProxyRequests Off
 On ProxyPreserveHost
 ProxyPass / web http://127.0.0.1:81/
 ProxyPassReverse / http://127.0.0.1:81/ 

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 you tell Apache is serving the page:

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

While This Should you tell Lighttpd is serving the file:

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

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

  • Thank you;). Good post!
  • Erik A.
    I do not see the part Where I tell lighty to connect to Apache .. am I missing something? Is there some sort of magic going on?
  • 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.

    thanks
  • Adam
    Thanks so much, I Had Been pulling my hair Trying to October of this for hours, just got it working thanks to your instructions:)
  • viru
    Some Internet providers Gives more bandtwith on port 80 and less on any other ports. I Think That You Should Separate or use proxy IP with port 80 for static files.
  • Julian
    That's why I made it port 8080 now;)
    Works like a charm with lighty, and now one looks at the URLs anyways.
  • inert
    Not bad, just the images would That Have 81 at the URL. It's just prettier without it.
  • Julian
    Why do not just use lighty directly, I mean Specifying all links with port 81? Would That Be Bad?
  • Prayer
    Can lighttpd is faster and handle more requests per second and consumes less resources, Apache is the heavy process and consumes much resources, so it is better to proxy from Apache to lighttpd.
  • inert
    Hi Animesh! I have no idea:) I guess that's a good question, and it deserves some benchmarks. Sadly I will not Have enough time over the next weeks, Them Otherwise I would of myself.

    Personally, my bet Is That It does not matter. Either way, Will Be requests 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.
  • Animesh
    Hi

    My doubt Has Been .. Should We Have lighttpd (or any other fast / light webserver) proxying the heavy dynamic content to the webserver, or Should Be the heavy webserver proxying to lighttpd (as you've done it).

    What would b better. lighty in front of apache or apache in front of lighty?
blog comments powered by Disqus

Portugus flagItaliano flagCoreano flagChins (simplificado) flagEnglish flagAlemo flagFrancs flagEspanhol flag
Japons flagrabe flagRusso flagHolands flagBlgaro flagTcheco flagCroata flagDinamarqus flag
Finlands flagHindu flagPolons flagRomeno flagSueco flagGrego flagNoruegus flag 
By N2H
Dolet 96 discount on hosting Dreamhost!
Use the "PROMO CODE" inertia. LAMP with 20GB of space and 1TB of transfer.

Related Articles

  • No Related Posts

Categories