Ever since I moved over to Slicehost I have been trying to expand my horizons as much as possible. I’ve been playing with Python and Ruby much more than in the past, primarily because I have an Ubuntu VPS all to myself. I can destroy and wreak havoc and I don’t really have to worry about losing anything (this blog may be down for a few hours, but no biggy).
One of the areas in which I have been experimenting is web server. For the most part, Apache is king, and it is a very powerful piece of software - no doubt. But, is it really your best option? It’s extremely bloated - on my 256MB VPS I was consistently maxing out my RAM, hosting nothing more than 3 blogs. Now, I’m sure there are some configuration options I could change to bring the memory usage down (releasing HTTP connections more often, decreasing the number of work handlers, etc) but it seemed like a great opportunity to see what’s out there.
The two leaders within the alternative web servers are Lighttpd and Nginx. Nginx was developed by a Russian programmer for his own site, primarily because he was tired of the large amount of resources Apache was draining. It is now used by more than 85% of the websites hosted out of Russia. No doubt, Nginx is a piece of software to keep your eyes on and it has a large number of fans here in the States. Unfortunately, not all of the documentation has been translated over from it’s Russian origins and I found it a bit difficult to find information on some of the more obscure functionality of the server.
Therefore, I opted to go with Lighttpd which also powers some of the hottest Web 2.0 properties like YouTube, Wikipedia, and Meebo. One of the first things I noticed about Lighttpd was how easy it was to configure. After just a few hours of first reading about Lighttpd I had it installed on my server and had a psuedo-Virtual Server system setup based on the Debian-based practices of storing websites within a user’s home directory and making them available/enabling them via a set of configuration files and symlinks within a root accessible directory. Of course, Lighttpd supports the standard Virtual Server configuration that Apache supports but it just doesn’t work for my needs.
One downfall of Lighttpd is that it does not support Apache .htaccess files - so in many cases, you are left to some self-education as to how to get your open source software to work properly. Thus, brings us to CodeIgniter - while Lee over at MadeByFresh is putting the final touches on my new design I wanted to get to work on rolling my own blog engine. As a CodeIgniter developer, using my blog to promote my services, I feel it’s important to “eat one’s own dog food.”
Remarkably, Lighttpd supported CodeIgniter right out of the box without any configuration changes. This is the same functionality you would receive with Apache if you just uploaded the framework to your public directory and started serving files. But, of course, I wanted to get rid of the dreaded index.php.
So, our URL requests will be similar to: domain.com/home or domain.com/users/edit, which are processed by CodeIgniter’s index.php because those directories and files really don’t exist (at least not via the path passed in the URL). Well, that sounds a lot like a 404 error to me, right? Trying to access a file that doesn’t exist…
lighttpd.conf
server.error-handler-404 = "/index.php"
Lighttpd allows you to define a custom 404 Error Handler and in this instance, we’re just going to pass them all to index.php. Don’t worry - Lighttpd doesn’t pass a 404 header so the browser still thinks we’re headed to a legitimate page.
So, what happens when we’re request CSS, images, Javascript, etc? What you’d expect! In those instances you are linking to real, legitimate files, via their actual path on the server (domain.com/assets/css/style.css for example). So, Lighttpd just serves up a static instance of that file (automatically compressing it, if you have that module enabled - so much goodness in Lighttpd).
Now that we’re throwing all of our potential 404 errors at index.php we need to tell CodeIgniter how to figure out the controller and method to load. If you’ve ever ran a CodeIgniter application off of Dreamhost you will be familiar with the following setting:
config/config.php
$config['uri_protocol'] = "REQUEST_URI";
This simply tells CodeIgniter to scope out the $_REQUEST variable and determine where we are supposed to be going. CodeIgniter will load your controller/method just like normal, everything will work in your application as you’d expect, and you’ll meet a hot blonde in Tahiti, elope to Vegas, and live happily ever after.
Of course, CodeIgniter is such a smart little bastard, if it comes across a controller/method combination that doesn’t exist it is going to load up our errors/error_404.php file. Let’s go check it out and make sure Google and everyone else understands that if we hit this page we are at a legitimate 404 error page and to throw a 404 header. Up until now, we are still a legitimate request for a file on the server (only the server itself knows how sneaky you’ve become, using the error-handler).
<?php header("HTTP/1.1 404 Not Found"); ?>
Holy roasted potatoes Batman! Yes, EllisLabs are a group of geniuses and they have thought well ahead of us in advance. 404 headers are being thrown to tell Google to GTFO.
Getting CodeIgniter up and running on Lighttpd is extremely simple (btw: you use the same process to run Wordpress, phpBB, and damn near any other software that uses an index.php entry-point for clean URLs). Plus, you are going to see your memory usage drop significantly on your server. I am currently running 10 Lighttpd work handlers in the same amount of memory as 1 of Apache’s.