Optimizing Apache Process Management: Solving Excessive Memory Usage in LAMP Stack WordPress Hosting


6 views

When you see multiple Apache processes running (each consuming around 200-250MB RAM), this is actually Apache's prefork MPM (Multi-Processing Module) working as designed. The prefork MPM creates child processes in advance to handle incoming requests, which explains why you see many www-data processes even during low traffic periods.

First, let's check your current Apache MPM settings:

apache2ctl -V | grep MPM
apache2ctl -M | grep mpm_

For prefork MPM (common in PHP environments), the key parameters are in /etc/apache2/mods-available/mpm_prefork.conf:

StartServers        5
MinSpareServers     5
MaxSpareServers    10
MaxRequestWorkers   150
MaxConnectionsPerChild 0

For a WordPress site with 2GB RAM (deducting 500MB for MySQL), you might calculate:

MaxRequestWorkers = (Available RAM) / (Average Apache process size)
                = (2048MB - 500MB) / 250MB 
                ≈ 6 workers

Update your mpm_prefork.conf:

StartServers        2
MinSpareServers     2
MaxSpareServers     4
MaxRequestWorkers   6
MaxConnectionsPerChild 1000

For PHP 7.0+ with mod_php, consider the more efficient event MPM:

sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
sudo systemctl restart apache2

Add these to wp-config.php:

define('WP_MEMORY_LIMIT', '64M');
define('WP_MAX_MEMORY_LIMIT', '128M');
define('WP_CACHE', true); // Enable caching

Install mod_status for real-time monitoring:

sudo a2enmod status
sudo systemctl restart apache2

Then access http://yourserver/server-status to see active processes.

For high-traffic sites, consider PHP-FPM with per-site pools:

[www_yoursite]
user = www-data
group = www-data
listen = /run/php/php7.4-fpm_yoursite.sock
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
  • Adjust MPM settings based on available RAM
  • Enable caching (OPcache + WordPress cache plugin)
  • Monitor with tools like htop and apachetop
  • Consider PHP-FPM for better process isolation

Apache HTTP Server typically spawns multiple processes to handle concurrent requests, especially when using the prefork MPM (Multi-Processing Module). The process count you're observing is directly related to your Apache configuration:

# Typical prefork configuration in /etc/apache2/mods-available/mpm_prefork.conf
StartServers        5
MinSpareServers     5
MaxSpareServers    10
MaxRequestWorkers 150
MaxConnectionsPerChild 0

The memory footprint shown (~250MB per process) suggests potential memory leaks or unoptimized PHP configuration. WordPress is particularly prone to this when using many plugins. Check memory usage patterns with:

# Check real-time memory usage
watch -n 5 "ps -ylC apache2 --sort:rss | awk '{print \$8,\$2,\$1,\$NF}'"

# Alternative with detailed output
apachectl status | grep -E "PID|Memory"

For a WordPress site on Ubuntu, these adjustments typically help:

# In /etc/apache2/mods-available/mpm_prefork.conf

    StartServers        3
    MinSpareServers     3
    MaxSpareServers     5
    MaxRequestWorkers   30
    MaxConnectionsPerChild 1000


# In php.ini (adjust values based on your RAM)
memory_limit = 128M
max_execution_time = 60

Add these to wp-config.php:

define('WP_MEMORY_LIMIT', '96M');
define('WP_MAX_MEMORY_LIMIT', '128M');
define('WP_CACHE', true); // Enable caching

// Disable post revisions
define('WP_POST_REVISIONS', 3);

For modern systems, consider switching from prefork to event MPM:

sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
sudo systemctl restart apache2

Note: This requires PHP-FPM installation for proper PHP processing.

Implement these monitoring solutions:

# Install apachetop for real-time monitoring
sudo apt install apachetop
apachetop -f /var/log/apache2/access.log

# For MySQL monitoring
sudo apt install mytop
mytop -u root -p

Remember to adjust these values based on your server's available RAM and expected traffic load. A good rule is to allocate no more than 60-70% of total RAM to Apache processes during peak loads.