Console Login
Home / Blog / Server Administration / Scaling PHP in 2009: Why We Ditched Apache mod_php for Nginx & PHP-FPM
Server Administration 1 views

Scaling PHP in 2009: Why We Ditched Apache mod_php for Nginx & PHP-FPM

@

Stop Letting Apache Swallowing Your RAM

If you have ever stared at top while your server spirals into swap death during a traffic spike, you know the pain. The standard LAMP stack (Linux, Apache, MySQL, PHP) has served us well for years, but in 2009, the web is changing. We aren't just serving static HTML anymore; we are dealing with heavy dynamic applications, AJAX polling, and the dreaded C10k problem.

Here is the reality: Apache mod_php is a memory hog.

When you use the Prefork MPM (the default for PHP), Apache spawns a distinct process for every single connection. If your PHP script requires 25MB of RAM, and you have 100 concurrent users (including slow clients on EDGE connections), you are suddenly allocating 2.5GB of RAM. On a standard VPS, that means OOM killer visits or heavy swapping.

There is a better way. It’s called PHP-FPM (FastCGI Process Manager).

The Architecture Shift: Nginx + PHP-FPM

Unlike Apache, Nginx (Engine X) is event-driven. It doesn't spawn a new process for every connection. It handles thousands of connections with a tiny memory footprint. But Nginx can't embed PHP like Apache does. It needs to talk to a backend.

Standard FastCGI is okay, but it lacks robust process management. That is where Andrei Nigmatulin's PHP-FPM patch comes in. It introduces features crucial for high-load environments:

  • Adaptive process spawning (start fast, scale down when idle).
  • Graceful stop/start (no downtime during PHP config changes).
  • Slow log (logging scripts that take too long to execute).
  • Emergency restart (if opcache corrupts, it revives itself).
War Story: We recently migrated a high-traffic forum hosting client in Oslo from a Dual-Xeon Apache setup to a smaller CoolVDS Xen instance running Nginx + PHP-FPM. Their load average dropped from 15.0 to 0.8. The "server busy" errors vanished immediately.

Implementation: Patching PHP 5.2.10

Since PHP-FPM isn't in the core (yet—fingers crossed for PHP 5.3.3), you can't just yum install it on CentOS 5 without a custom repo. Real SysAdmins build from source to get the compile flags right.

1. The Build Process

First, grab the PHP source and the FPM patch. We are assuming you are running on a CoolVDS High-Performance VPS (CentOS 5.3, 64-bit).

wget http://us.php.net/get/php-5.2.10.tar.gz/from/this/mirror
tar zxvf php-5.2.10.tar.gz
wget http://php-fpm.org/downloads/php-5.2.10-fpm-0.5.11.diff.gz
gzip -cd php-5.2.10-fpm-0.5.11.diff.gz | patch -d php-5.2.10 -p1
cd php-5.2.10
./configure --enable-fastcgi --enable-fpm --with-mcrypt --with-mysql ...

2. Configuring PHP-FPM (The XML Way)

Unlike the standard php.ini, FPM currently uses an XML configuration file. This allows for granular control over worker pools.

Edit /usr/local/etc/php-fpm.conf:

<value name="max_children">50</value>
<value name="process_control_timeout">5s</value>
<value name="rlimit_files">1024</value>

Optimization Tip: Don't set max_children blindly. Calculate it: (Total RAM - OS RAM - DB RAM) / Average PHP Process Size. If you overshoot on a VPS, you will swap, and swapping on disk (even fast SAS drives) is the enemy of latency.

3. Nginx Configuration

Tell Nginx to pass PHP requests to the FPM daemon listening on localhost.

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/public_html$fastcgi_script_name;
include fastcgi_params;
}

Why Infrastructure Matters (The CoolVDS Edge)

You can tune your software all day, but if the underlying metal is weak, your latency will suffer. In Norway, where the Datatilsynet (Data Inspectorate) keeps a close watch on data handling under the Personal Data Act (Personopplysningsloven), knowing where your data lives is paramount.

At CoolVDS, we don't oversell our host nodes. Many providers use OpenVZ to cram hundreds of containers onto one kernel, leading to "noisy neighbor" issues where one site's I/O kills your database performance.

We use Xen virtualization. This ensures strict isolation. Your RAM is your RAM. Furthermore, our storage arrays utilize Enterprise 15k RPM SAS drives in RAID-10. While not as instant as RAM, this is the fastest storage currently available for reliable hosting, vastly outperforming the SATA drives found in budget hosting.

Latency to NIX

For Norwegian businesses, pings matter. Our datacenter is directly peered at NIX (Norwegian Internet Exchange). This means when a user in Trondheim or Bergen requests your PHP-FPM driven site, the packets don't route through Frankfurt or London. They stay local. Lower latency = faster page loads = happier users.

The Verdict

Apache is still great for local development or simple shared hosting. But if you are building a business-critical application in 2009, you need the efficiency of Nginx and the stability of PHP-FPM.

Don't let legacy configurations hold back your growth. Compile custom, tune your pools, and host on iron that respects your need for dedicated resources.

Ready to compile your own stack? Deploy a CoolVDS Xen instance with CentOS 5 today and experience true root access.

/// TAGS

/// RELATED POSTS

Surviving the Spike: High-Performance E-commerce Hosting Architecture for 2012

Is your Magento store ready for the holiday rush? We break down the Nginx, Varnish, and SSD tuning s...

Read More →

Automate or Die: Bulletproof Remote Backups with Rsync on CentOS 6

RAID is not a backup. Don't let a typo destroy your database. Learn how to set up automated, increme...

Read More →

Xen vs. KVM: Why Kernel Integration Wars Define Your VPS Performance

Red Hat Enterprise Linux 6 has shifted the battlefield from Xen to KVM. We analyze the kernel-level ...

Read More →

Escaping the Shared Hosting Trap: A SysAdmin’s Guide to VDS Migration

Is your application choking on 'unlimited' shared hosting? We break down the technical migration to ...

Read More →

IPTables Survival Guide: Locking Down Your Linux VPS in a Hostile Network

Stop script kiddies and botnets cold. We dive deep into stateful packet inspection, fail2ban configu...

Read More →

Sleep Soundly: The Paranoid SysAdmin's Guide to Bulletproof Server Backups

RAID is not a backup. If you accidentally drop a database table at 3 AM, mirroring just replicates t...

Read More →
← Back to All Posts