The 3:00 AM Pager Duty
It is a sound every sysadmin dreads: the Nagios alert. Your load average just hit 20.0, and the swap usage is climbing. You SSH in, run top, and see the culprit immediately. It is Apache. It is always Apache.
The traditional LAMP stack is robust, but it creates a process or thread for every single connection. When you get hit by the "Slashdot effect" or a traffic spike from a VG.no feature, Apache eats your RAM until the kernel OOM-killer steps in. It is messy.
You do not need to rewrite your entire application. You just need to change the front door. By placing Nginx in front of Apache as a reverse proxy, you can serve static content efficiently and only wake up the heavy Apache processes for dynamic PHP.
The Architecture: Nginx + Apache
We are not replacing Apache; we are shielding it. Nginx uses an asynchronous, event-driven architecture (specifically the epoll event notification mechanism on Linux). This allows a single process to handle thousands of concurrent connections with a tiny memory footprint.
Pro Tip: Do not just install yum install nginx from the default repositories if they are outdated. Add the EPEL repository or compile Nginx 0.8.53 from source to get the latest stable features.Configuration Guide
Here is the battle-tested configuration we use at CoolVDS for clients running heavy Magento or Drupal sites on CentOS 5.5.
Step 1: Move Apache
First, edit your httpd.conf. Change the Listen port so Apache stops hogging port 80.
Listen 127.0.0.1:8080Step 2: Configure Nginx Proxy
Create a new virtual host file in /etc/nginx/conf.d/. We need to tell Nginx to forward PHP requests to Apache while serving images and CSS directly.
server {
listen 80;
server_name example.no www.example.no;
# Serve static files directly - Fast!
location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
root /var/www/html;
access_log off;
expires 30d;
}
# Pass the rest to Apache
location / {
proxy_pass http://127.0.0.1:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}The Storage Bottleneck: Why HDD Kills Performance
Configuration is only half the battle. When Nginx buffers responses or when Apache writes session files, you are hitting the disk. If you are running on a standard 7,200 RPM SATA drive, your I/O Wait (wa in top) will skyrocket during traffic spikes.
In 2010, storage is the single biggest bottleneck in hosting. This is why we built the CoolVDS platform differently. While others resell standard SATA VPS nodes, we are aggressive adopters of Enterprise SSDs. The random read/write speeds on SSDs are exponentially higher than mechanical disks.
When your database tries to perform a complex JOIN on a 2GB table:
- SATA Drive: The drive head physically moves. Latency: 12ms+. Result: Your site hangs.
- CoolVDS SSD: No moving parts. Latency: <0.1ms. Result: Instant page load.
Local Latency and Data Law
For our Norwegian clients, physics still applies. Hosting in the US or Germany adds 30-100ms of latency to every handshake. If your server is in Oslo, connected directly to NIX (Norwegian Internet Exchange), your local users get a snappy experience. Furthermore, adhering to the Personopplysningsloven (Personal Data Act) is simpler when your data physically resides within Norwegian borders.
Final Check
Before you restart services, test your Nginx syntax:
service nginx configtestIf it returns OK, flip the switch. Your RAM usage should drop, and your capacity for concurrent users should double. If you are still seeing I/O wait issues, it is not your config—it is your hardware. Stop fighting physics on spinning rust.
Need a test environment? Deploy a CentOS VDS with SSD storage on CoolVDS today. We are peered at NIX in Oslo for minimum latency.