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.
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):
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
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>
$ 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.