Console Login
Home / Blog / Performance Optimization / Stop Letting Apache Eat Your RAM: Moving to PHP-FPM and Nginx on PHP 5.3
Performance Optimization 9 views

Stop Letting Apache Eat Your RAM: Moving to PHP-FPM and Nginx on PHP 5.3

@

The Age of mod_php is Over. Welcome to PHP-FPM.

If you are still running a high-traffic site on a standard LAMP stack (Linux, Apache, MySQL, PHP) with the default mod_php handler, you are throwing money away. I see it every day. A client complains their server is crawling. I check top, and Apache is spawning dozens of child processes, each consuming 30MB to 50MB of RAM just to serve a static image. It’s inefficient. It’s heavy. And as of July 2010, with the release of PHP 5.3.3, it is officially obsolete.

The FastCGI Process Manager (PHP-FPM) has finally been merged into the PHP core. No more patching. No more hacks. It is time to get serious about resource management.

The Problem: The Apache Memory Wall

Apache with mod_php embeds the PHP interpreter inside every single Apache process. If you have 2 GB of RAM and each process takes 40MB, you can handle roughly 50 concurrent users. That is it. When the 51st user arrives, your server starts swapping to disk. Once you hit swap on a standard mechanical drive—even a 15k RPM SAS drive—your load average spikes, and latency goes from 50ms to 5 seconds.

Your users in Oslo don't care that your code is clean. They care that the site didn't load.

The Solution: Nginx and PHP-FPM

Nginx event-driven architecture handles thousands of connections with a tiny memory footprint. It serves static files (images, CSS, JS) instantly. When it hits a PHP file, it passes the request to PHP-FPM via a TCP or Unix socket. The beauty? The PHP process only lives as long as it takes to execute the script, and it doesn't get bogged down serving JPEGs.

Step 1: Installing on Ubuntu 10.04 LTS (Lucid Lynx)

If you are on Lucid, you might need the Brian Mercer PPA to get the latest versions, but let's stick to the basics. Ensure you have the php5-fpm package.

sudo apt-get install nginx php5-fpm

Step 2: Configuring the Pool

The magic happens in /etc/php5/fpm/pool.d/www.conf. The default settings are often too aggressive for a small VPS. You need to do the math based on your available RAM, not what the default config says.

For a VPS with 512MB RAM, try this:

pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 500

Setting pm.max_requests is crucial. PHP has a history of memory leaks. This setting recycles the process after 500 requests, freeing up leaked memory before it crashes your server.

Step 3: Nginx Block Configuration

Tell Nginx to talk to FPM. In your server block:

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Pro Tip: Don't use Unix sockets (.sock) if you expect extremely high load on a multi-core system. TCP stacks (127.0.0.1:9000) often scale better across cores in current Linux kernels compared to the overhead of socket file locking.

Hardware Matters: The I/O Bottleneck

Software optimization can only take you so far. If your database is thrashing the disk, PHP-FPM will just sit there waiting for MySQL to return data. In 2010, storage is still the biggest bottleneck in hosting.

This is where infrastructure choice becomes a business decision. Many "cheap" VPS providers in Europe oversell their storage arrays. They put 50 customers on a single SATA RAID array. If one neighbor runs a backup script, your PHP application hangs.

At CoolVDS, we refuse to play the overselling game. We utilize high-performance Enterprise RAID 10 storage and strictly limit the number of VMs per physical node. We use Xen virtualization, which provides strict memory isolation—unlike OpenVZ, where a neighbor can technically burst into your RAM allocation.

Norwegian Compliance and Latency

If your target audience is in Norway, hosting in Germany or the US adds 30-100ms of latency. That sounds small, but with the TCP handshake and SSL negotiation, it adds up. Hosting locally or in nearby Nordic hubs ensures your Time To First Byte (TTFB) stays green.

Furthermore, consider the Personopplysningsloven (Personal Data Act). While we don't have the heavy GDPR framework yet, the Datatilsynet is very clear about the responsibility of data processors. Hosting on a provider that respects data sovereignty and physical security is not just optional; for many Norwegian businesses, it is a legal requirement.

Summary

PHP 5.3.3 and FPM are game changers. By switching from Apache mod_php to Nginx + FPM, you can effectively double the traffic capacity of your current server without spending an extra Krone on hardware. But when you do need that hardware, ensure it is built on architecture that respects your I/O needs.

Ready to drop your load average? Deploy a Xen-based instance on CoolVDS today and see what true resource isolation feels like.

/// TAGS

/// RELATED POSTS

Taming Latency: Tuning NGINX as an API Gateway on Linux (2015 Edition)

Is your REST API choking under load? We dive deep into Linux kernel tuning, NGINX upstream keepalive...

Read More →

Stop Letting Apache mod_php Eat Your RAM: The PHP-FPM Performance Guide

Is your server swapping during peak hours? We ditch the bloated Apache mod_php model for the lean, m...

Read More →

Stop Wasting RAM: Migrating from Apache mod_php to Nginx & PHP-FPM on CentOS 6

Is your server swapping out under load? The old LAMP stack architecture is dead. Learn how to implem...

Read More →

PHP-FPM vs mod_php: Tuning High-Performance LAMP Stacks in 2011

Is your Apache server thrashing under load? Stop relying on the bloated mod_php handler. We dive dee...

Read More →

Stop Using mod_php: Optimizing PHP Performance with FPM and Nginx

Is your web server struggling under load? Learn why moving from Apache's mod_php to PHP-FPM and Ngin...

Read More →

Stop Watching 'wa' in Top: Why Spinning Disks Are the Bottleneck in 2011

Is your server load spiking despite low CPU usage? The culprit is likely I/O wait. We break down why...

Read More →
← Back to All Posts