Light weight alternatives to Apache
When it comes to choosing a Linux web server, Apache, as we all know, is almost a de facto standard. There is even a chance that your web host does not even offer (or worse, know of) an alternative web server. So, it’s not a big surprise that many developers don’t know too much about what other alternatives are available to them.
There is of course no denying that Apache is indeed an old war horse, and an extremely robust and mature one at that. But, even as stable as Apache is, it has one main drawback – it occupies a large memory footprint due to its request-per-process model, and is therefore mediocre at handling multiple concurrent requests when you have a limited RAM. This can be a problem when you are starting out small, with a rented 256 MB VPS for your application. Even if you are a big player, light weight Apache alternatives can help increase the throughput of your servers. Youtube, for instance, was using Lighttpd, a light-weight web server to serve millions of videos everyday (prior to their acquisition by Google).
Ok enough digressing, let’s now take a look at some of the open source alternatives to Apache.
Lighttpd
Lighttpd, pronounced as Lighty, is a single threaded web server optimized for a large number of keep-alive connections, which is often a requirement for high traffic web applications. It was originally developed by the German programmer Jan Kneschke to solve the C10k problem (handle 10,000 parallel connections using a single server). Apart from Youtube as I have already mentioned, Wikipedia, Meebo and Sourceforge all use Lighttpd. Lighttpd is rich with features, and offers mod_rewrite (URL rewriting), FastCGI support, HTTP compression, amongst many other features. I was able to install and configure Lighttpd in a few minutes, and it is extremely light-weight (less than 1MB). I was able to get PHP/MySQL running in a couple of minutes, using the FastCGI interface.
One drawback, which I have seen repeatedly mentioned in many forums and blogs on the web (I could not verify these claims with my own tests), is that Lighttpd seems to suffer from memory leaks – which often required scheduled server restarts.
Nginx
Nginx, pronounced as Engine-X, is a web server originating from Russia, and is written by Igor Sysoev. Unlike Lighttpd, Nginx is not a single threaded webserver (it instead uses a single master process which delegates work to a small number of worker processes), but is another popular light weight web server alternative. It is supported by more than 20% of Russian virtual hosts, and is gaining lots of attention these days in other parts of the world too.
Nginx is excellent at serving static files and also supports reverse proxying. Like Lighttpd, I was able to get Nginx up and running in a few minutes. However, it does not come up with default FastCGI support – so getting PHP/MySQL working was a little tricky. It involves spawning a FastCGI process manually. I found this tutorial helpful.
Erlang based alternatives
Erlang is a concurrent programming language designed by Ericsson to support distributed, soft real time applications, which has been opensourced since 1998. I definitely want to write more about how scalable Erlang is, but this is not the article for that. So, let’s just say that Erlang is tailor-made for high demand applications which need to handle multiple concurrent connections – like a web server.
YAWS (Yet Another Web Server) and Mochiweb are two Erlang based web servers which I found to be highly scalable and light-weight.
A load test conducted matching Yaws against Apache found that Apache failed at 4,000 concurrent connections, while Yaws continued functioning with over 80,000 concurrent connections (source). I did have some trouble installing and configuring YAWS though. The documentation is scarce, to say the least.
Mochiweb is actually an Erlang library for building your own light-weight HTTP server. Don’t be put off by that statement! A simple server can be built with just a few lines of code. I installed and tested Mochiweb too, and I found it pretty easy. If you are interested, here is a tutorial which I found helpful.
So should you ditch Apache?
This decision depends on a number of factors. Like I have already stated, if you want to make the best out of the limited resources you have, then a light-weight web server is definitely advantageous. However, from my experience with playing with the above alternatives, if you do decide to go with one of them, you should be prepared to roughen it out. Poor documentation and support (the mailing lists and forums are generally helpful, but you can never completely rely on them when you are suddenly facing an issue in a production environment) could end up making things difficult for you. Don’t get me wrong, I am not saying that these servers are not production-ready yet (they are pretty stable), but there is always an off-chance you might just hit upon some snag. In that case, you probably have to roughen it out.
A reasonable choice would be to front Apache with one of these light weight web servers. In effect, you can use them for load balancing. Using reverse-proxying, you can handle the static files (like CSS and images) using a light weight web server, and route the dynamic requests to Apache.
So, in future, I hope you will consider an Apache alternative, should your needs be better satisfied by a light-er web server. If you have experience using any of the above alternatives, do share your views in the comments!
Tags: apache, lighttpd, nginx, web-server


