Console Login

Apache vs Lighttpd: Surviving the C10k Problem Without Melting Your RAM

Apache vs Lighttpd: Surviving the C10k Problem Without Melting Your RAM

Let’s be honest: if you are still running a default Apache 2.2 installation with mod_php on a high-traffic site, you are essentially DDoSing yourself. I spent last Tuesday night debugging a Magento 1.7 cluster that went dark during a flash sale. The culprit? It wasn't the code. It was the web server forking hundreds of heavy processes until the server started swapping to disk. Once you hit swap, you're dead.

In the world of high-performance hosting, the debate between Apache HTTP Server and Lighttpd (pronounced "lighty") isn't just about preference; it's about architecture. Do you need the Swiss Army knife that is compatible with everything, or do you need the scalpel that slices through static content like a hot knife through butter?

The Architecture War: Process vs. Event

To understand why your server load is spiking, you have to look under the hood. The fundamental difference lies in how these two handle connections.

Apache: The Heavyweight Champion

Apache has been the backbone of the internet since the 90s. Its power lies in its modularity and the ubiquity of .htaccess files. However, the standard MPM Prefork module—which you likely use for PHP compatibility—is a resource hog.

Each client connection spawns a separate process. If a PHP process takes 20MB of RAM and you have 500 concurrent users, do the math. You need 10GB of RAM just to keep the lights on. If you are on a limited VPS, this model collapses under the weight of the "C10k problem" (handling 10,000 concurrent connections).

You can mitigate this by switching to the MPM Worker, but it's not a silver bullet if your modules aren't thread-safe.

# Typical Apache 2.2 Prefork Tuning
<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>

Lighttpd: The Asynchronous Challenger

Lighttpd was designed specifically to address Apache's memory bloat. It uses an asynchronous, event-driven architecture (select/poll/epoll). Instead of one process per connection, it handles all network connections in a single process loop.

I've seen Lighttpd serve 10,000 static file connections while consuming less than 20MB of RAM. It doesn't rely on .htaccess (which slows down Apache because it stats the disk for every request), which forces you to be cleaner with your main configuration.

# lighttpd.conf performance tweaking
server.max-fds = 2048
server.event-handler = "linux-sysepoll"
server.network-backend = "linux-sendfile"
server.stat-cache-engine = "simple"

# Minimize I/O blocking
server.max-keep-alive-requests = 16
server.max-keep-alive-idle = 5

Real World Performance: A Norwegian Case Study

We recently migrated a media client based in Oslo from a shared Apache environment to a dedicated CoolVDS instance. Their main pain point was latency during peak news cycles.

Here is a snippet of the load test results using ab (Apache Bench) against a static 50KB image file. The test simulated 1,000 concurrent requests.

Metric Apache 2.2 (Prefork) Lighttpd 1.4
Requests per second 1,240 req/s 4,850 req/s
Memory Usage 480 MB 18 MB
Load Average 3.50 0.25

The difference is staggering for static content. Lighttpd allows the CPU to actually sleep instead of context-switching constantly.

Pro Tip: If you are locked into Apache for .htaccess compatibility, consider a hybrid approach. Run Lighttpd on port 80 to serve images, CSS, and JS, and proxy dynamic PHP requests to Apache running on port 8080. This offloads 90% of the I/O operations from the heavy Apache processes.

The "CoolVDS" Factor: Hardware Still Matters

Software optimization can only get you so far. In 2012, the biggest bottleneck is still the hard drive. While many providers in Europe are still spinning 7.2k RPM SATA drives, the latency introduced by disk seeking can kill your Time to First Byte (TTFB).

This is where infrastructure choice becomes critical. At CoolVDS, we don't rely on standard spinning rust. We utilize enterprise-grade SSD storage (Solid State Drives) in RAID-10 arrays. When your web server—be it Apache or Lighttpd—needs to read a file, the seek time is virtually zero.

Why Location Matters: Latency to Oslo

For our Norwegian clients, physics is the law. Hosting in a datacenter in Texas adds 140ms of latency before the server even thinks about processing the request. By peering directly at NIX (Norwegian Internet Exchange), CoolVDS ensures that the network hop is as short as possible.

Furthermore, complying with Datatilsynet regulations and the Personal Data Act requires strict control over where your data lives. Keeping your customer data within the EEA (European Economic Area) isn't just good for performance; it's essential for legal compliance.

Configuring PHP: FastCGI is Mandatory

If you switch to Lighttpd, you lose mod_php. You must use FastCGI. Honestly, you should be using FastCGI with Apache too.

Using php-fpm (FastCGI Process Manager) separates the web server from the PHP interpreter. This prevents a slow PHP script from locking up the web server process.

# Lighttpd FastCGI Configuration for PHP
fastcgi.server = ( ".php" =>
  ( "localhost" =>
    (
      "socket" => "/tmp/php-fastcgi.socket",
      "bin-path" => "/usr/bin/php-cgi",
      "max-procs" => 4,
      "idle-timeout" => 20,
      "bin-environment" => (
        "PHP_FCGI_CHILDREN" => "4",
        "PHP_FCGI_MAX_REQUESTS" => "10000"
      ),
      "bin-copy-environment" => (
        "PATH", "SHELL", "USER"
      ),
      "broken-scriptfilename" => "enable"
    )
  )
)

Conclusion: Which One Should You Choose?

Use Apache if:

  • You rely heavily on .htaccess rewrite rules that you can't migrate.
  • You are running a legacy CMS that requires specific Apache modules.
  • You need the flexibility of dynamic module loading.

Use Lighttpd if:

  • You serve a high volume of static assets (images, video, downloads).
  • You are on a VPS with limited RAM (256MB or 512MB).
  • You want low latency and maximum throughput per dollar.

Ultimately, a high-performance web server needs a high-performance foundation. Don't let disk I/O be the reason your site feels slow. Deploy a VPS Norway instance on CoolVDS today, leverage our SSD storage, and see what pure speed feels like.