Optimizing PHP5-FPM Configuration to Prevent Crashes Under High Traffic WordPress Loads


2 views

When handling ~500 concurrent users on a WordPress site with limited server resources (Dual Core, 4GB RAM), PHP5-FPM processes were crashing intermittently, resulting in blank pages. The issue manifests when the current configuration can't handle the traffic spikes.

Examining the configuration files revealed several suboptimal settings:

; Problematic original settings:
pm.max_children = 25
request_terminate_timeout = 30s
pm.max_requests = 100

For a 4GB server running WordPress, we need smarter process management:

[www]
user = www-data
group = www-data
listen = /var/run/php5-fpm.sock
pm = dynamic
pm.max_children = 40  ; Calculated based on available memory
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 500  ; Prevent memory leaks
request_terminate_timeout = 120s  ; Give WP enough time
request_slowlog_timeout = 30s
slowlog = /var/log/php-fpm/slow.log

These fastcgi params help prevent timeouts:

fastcgi_connect_timeout 75s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;

Based on production experience with similar setups:

  1. Enable OPcache with aggressive settings
  2. Implement Redis object caching for WordPress
  3. Adjust Linux kernel parameters for better connection handling

Add these emergency settings in php-fpm.conf:

[global]
emergency_restart_threshold = 3
emergency_restart_interval = 1m
process_control_timeout = 10s

Implement log monitoring for early crash detection:

# Sample logwatch configuration
LogFile = /var/log/php5-fpm.log
*OnlyService = php-fpm
*ApplyStdDate = yes

Add these to wp-config.php:

define('WP_MEMORY_LIMIT', '128M');
define('WP_MAX_MEMORY_LIMIT', '256M');
define('WP_CACHE', true);

When migrating from LiteSpeed to Nginx for a WordPress site with ~500 concurrent users, many administrators encounter PHP5-FPM crashes during traffic surges. The symptoms typically include blank pages, unresponsive services, and visible memory exhaustion in system monitoring tools like top or htop.

The current setup shows several potential bottlenecks:

pm = dynamic
pm.max_children = 25
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 5
pm.max_requests = 100

For a 4GB RAM server handling WordPress, these values are too conservative. Let's calculate the optimal settings:

# Calculate based on average PHP process memory (50MB observed)
Total available RAM = 4GB (4000MB)
Memory for OS/Other = 1GB
Available for PHP = 3000MB
Average PHP process = 55MB (from top output)
Max children = 3000 / 55 ≈ 54

Here's the recommended configuration for /etc/php5/fpm/pool.d/www.conf:

[www]
user = www-data
group = www-data
listen = /var/run/php5-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.max_requests = 500
pm.process_idle_timeout = 30s
request_terminate_timeout = 120s
request_slowlog_timeout = 30s
slowlog = /var/log/php-fpm/slow.log
catch_workers_output = yes

Complement your PHP-FPM with these Nginx optimizations:

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    
    # Timeout and buffer optimizations
    fastcgi_read_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_connect_timeout 60;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
}

Add these to your wp-config.php:

// Reduce database overhead
define('WP_CACHE', true);
define('WP_MEMORY_LIMIT', '128M');
define('WP_MAX_MEMORY_LIMIT', '256M');

// Object cache and query optimizations
define('SAVEQUERIES', false);
define('WP_POST_REVISIONS', 5);
define('AUTOSAVE_INTERVAL', 300);

Implement these emergency settings in /etc/php5/fpm/php-fpm.conf:

[global]
emergency_restart_threshold = 5
emergency_restart_interval = 2m
process_control_timeout = 20s

Use these commands to monitor and debug:

# Real-time monitoring
sudo watch -n 1 "ps aux | grep php-fpm | wc -l"

# Check for slow requests
sudo tail -f /var/log/php-fpm/slow.log

# Verify socket permissions
ls -l /var/run/php5-fpm.sock