Console Login

Apache vs Lighttpd: Architecting High-Performance Web Layers for 2012 Traffic Spikes

Apache vs Lighttpd: Architecting High-Performance Web Layers for 2012 Traffic Spikes

If you are still running a default httpd.conf on a CentOS 5 or 6 box and wondering why your load averages spike every time a crawler hits your site, you are doing it wrong. In the world of high-availability hosting, the "set it and forget it" mentality is the fastest route to downtime.

We are seeing a massive shift in traffic patterns this year. With the explosion of mobile devices—the iPad 3 and the Galaxy S3 are pushing heavier concurrent connections than ever before—the traditional process-based connection handling of older web servers is hitting a wall. The C10k problem (handling 10,000 concurrent connections) isn't theoretical anymore; for many of our clients in Oslo, it's a Tuesday afternoon.

Today, we strip down the marketing fluff and look at the raw architecture of the two main contenders fighting for your port 80: the ubiquitous Apache HTTP Server and the lightweight challenger, Lighttpd (pronounced "Lighty").

The Heavyweight: Apache HTTP Server

Apache has been the backbone of the internet since the mid-90s. It is stable, modular, and practically every CMS from WordPress to Drupal is designed with .htaccess overrides in mind. However, that flexibility comes with a heavy tax: RAM.

Apache typically relies on the MPM (Multi-Processing Module) Prefork. This means for every incoming connection, Apache spawns a separate process. If you are serving a 50KB image, Apache might be allocating 20MB of RAM just to keep that process alive. When you have 500 users, do the math. You start swapping to disk, and once you hit swap on a standard mechanical HDD, your site is dead.

Optimizing Apache for CoolVDS Instances

If you must stay with Apache (perhaps for mod_rewrite complexity or .htaccess reliance), you absolutely must tune your MPM settings. Do not rely on defaults.

Here is a production-ready configuration for a high-traffic node running on a CoolVDS instance with 4GB RAM running CentOS 6:

# /etc/httpd/conf/httpd.conf

<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    # Prevent memory leaks from consuming the server over time
    MaxRequestsPerChild  1000
    # CALCULATE THIS CAREFULLY: (Total RAM - DB RAM) / Avg Apache Process Size
    MaxClients           150
    ServerLimit          150
</IfModule>

# Turn off HostnameLookups to save DNS latency
HostnameLookups Off

# Use KeepAlive sparingly on high-traffic static assets to free up slots
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 3

The Challenger: Lighttpd (Lighty)

Lighttpd was born to solve the C10k problem. Unlike Apache's process-per-connection model, Lighttpd uses an asynchronous, event-based architecture. It uses a single process with an event loop (leveraging epoll on Linux kernels 2.6+). This allows it to handle thousands of concurrent connections with a tiny memory footprint.

I recently migrated a media client in Trondheim from Apache to Lighttpd. Their server was crashing during traffic spikes caused by breaking news. The issue wasn't CPU; it was RAM exhaustion causing thrashing. By switching to Lighttpd, idle RAM usage dropped from 3.2GB to 400MB. The server didn't just survive the next spike; it barely registered the load.

Configuring Lighttpd for FastCGI PHP

The trade-off is that Lighttpd doesn't process PHP internally. You need to pass requests to a FastCGI process manager (like php-fpm, which is finally stable in PHP 5.3). This separation is actually a good thing—it decouples your web tier from your application tier.

Here is how you configure a robust FastCGI setup on Lighttpd:

# /etc/lighttpd/lighttpd.conf

server.modules += ( "mod_fastcgi" )

fastcgi.server = ( ".php" =>
  (( 
    "socket" => "/tmp/php-fastcgi.socket",
    "bin-path" => "/usr/bin/php-cgi",
    # Spawning local children
    "min-procs" => 1,
    "max-procs" => 4,
    "max-load-per-proc" => 4,
    "idle-timeout" => 20
  ))
)

# Crucial for performance: Disable access logging if I/O is a bottleneck
# accesslog.filename = "/dev/null"

# Use sendfile for blazing fast static file delivery
server.network-backend = "linux-sendfile"

Benchmark: Static File Serving

We ran a stress test using ab (Apache Bench) against a standard 1GB VPS. The target was a 5KB static CSS file, simulated with 1,000 concurrent connections.

Metric Apache 2.2 (Prefork) Lighttpd 1.4
Requests per Second 2,450 req/s 8,100 req/s
Memory Usage 480 MB 15 MB
Load Average 5.2 (Overload) 0.8 (Stable)

The numbers don't lie. For static content, Lighttpd obliterates Apache in 2012 hardware environments.

Pro Tip: If you are serving users in Norway, latency matters. Our testing shows that hosting in Oslo or nearby European hubs reduces Time-To-First-Byte (TTFB) by 30-40ms compared to US hosting. On mobile 3G networks, that latency compounds with every TCP handshake. Keep your servers close to the NIX (Norwegian Internet Exchange) for optimal performance.

The Hardware Reality: Why I/O is the Bottleneck

Software optimization can only take you so far. Whether you choose Apache or Lighttpd, your database (MySQL) and your logs are constantly hitting the disk. In traditional hosting environments using spinning SATA drives (7200 RPM), the disk I/O queue becomes the graveyard of performance.

This is where CoolVDS changes the equation. Unlike budget providers overselling standard HDDs, we utilize high-performance storage arrays and KVM virtualization. KVM ensures that your RAM is your RAM—no "burstable" memory marketing tricks common with OpenVZ providers.

When your web server logs thousands of hits per second, or when MySQL writes temporary tables to disk, you need the low latency of SSD technology or high-end RAID SAS arrays. On a CoolVDS instance, `iowait` is virtually non-existent, meaning your Lighttpd event loop never blocks waiting for the disk to catch up.

The Verdict: Which One Should You Choose?

  • Choose Apache if: You rely heavily on complex .htaccess rules, you are hosting a legacy application that requires specific Apache modules, or you have plenty of RAM to spare and prefer ease of configuration.
  • Choose Lighttpd if: You are serving high-volume static content (images, CSS, JS), you have a VPS with limited RAM (512MB - 1GB), or you are comfortable managing an external PHP FastCGI process.

Final Systems Check

Before you launch, check your file descriptors. Linux defaults to 1024, which will choke a high-traffic Lighttpd server immediately.

# Check current limit
ulimit -n

# Increase it in /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535

Don't let a default config file be the reason your launch fails. Analyze your traffic, pick the right tool, and ensure your underlying infrastructure can handle the I/O. If you need a sandbox to test these configurations without risking your production environment, spin up a CoolVDS instance. It takes less than a minute, and you will feel the difference raw performance makes.