What kind of web server should I use for my app?
When it comes to synchronous servers, Apache is undoubtedly the most popular. In a synchronous server, each simultaneous connection requires a thread, which in turn, requires some overhead. On the other hand, an asynchronous server is event-driven, and mostly handles all requests in a single thread. Asynchronous web servers include the more lightweight Lighttpd and Nginx, as well as Node’s HTTP server. They use a non-blocking I/O event loop running in a single process to serve requests.
If all you need is a simple HTTP server to serve some static files, then perhaps you should go with Lighttpd. It has an easy setup and is a nice alternative to Apache because it is performant and simple. Furthermore, most of the good parts of Apache are also available in Lighttpd including modules, URL rewriting, and support for CGI or FastCGI. To get going, just install the lighttpd package in your Linux distribution, run the server start command, and watch pages get served! If you do use FastCGI, it will actually allow you to use separate processes for each request, processed on a separate server, which is good for isolating processes from your main server for reasons of security or stability.
Nginx, another asynchronous open-source, lightweight HTTP server, has many similarities to Lighttpd; It also uses one master process to serve requests; however, it actually delegates its work onto separate worker processes, which is great for taking advantage of a machine’s multiple cores. Just like Lighttpd, it’s fast and efficient at serving static content, but it seems to be more performant than Lighttpd. Nginx keeps a small memory footprint, even with lots of traffic, and is known to be pretty stable.
So let's take a look at Apache and Node in more detail.
Why is Apache the most popular web server? For starters its .htaccess file allows for flexible configuration, it’s the standard server bundled in typical LAMP setups, and it’s easy to find solutions to any problems with it. It generally uses PHP, which is easy for beginners and works well for serving web pages. It’s been around for a while, which means it’s quite stable. Since it is thread-based, an unexpected problem occurring while processing a request, will only crash one thread, and therefore isolate the issue to one request. In general, requests do not block each other in any way on the server. Apache has a lot of modules available for it so it is very configurable to fit your needs. Having said that, the knock on Apache is that it is “heavy” compared to servers like Lightpd and Nginx. It is quite resource-hungry by default, and can quickly gobble up RAM because it uses a different thread (and sometimes process) for each request.
Of course, there are some drawbacks with Node. Since all requests run on the same process, you better hope that you designed your application correctly, or there will be consequences. One bad request could hog up the resources needed to process other requests and even crash your whole application. This is why in node, it’s usually a good idea to use exception-based logic as well as delegate CPU-intensive requests to a worker or some other process. Another knock on Node is that it is still relatively new compared to other server/application architectures. Also, serving static files is slower in Node than it is in Apache.
…so there you have it. Hopefully you choose the right web server and application architecture that suits your needs!