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 (inerte@gmail.com) 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 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):
If you have directories under /var/www/, append its names to the url to see if lighttpd is correctly serving 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 will work and who has access to it. Replace the * with 127.0.0.1, and uncomment the Allow from line. Final 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 ProxyPreserveHost On 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 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.
Leave a Reply
You must be logged in to post a comment.