Apache MPM Prefork Configuration: Why Excessive Processes Despite MinSpareServers Settings?


10 views

When examining your Apache MPM prefork configuration and the accompanying screenshots, I notice several key points that explain the process explosion:


    # Current problematic configuration
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    ServerLimit      1250
    MaxClients        1250
    MaxRequestsPerChild   1500

The primary issue stems from the combination of high ServerLimit/MaxClients and MaxRequestsPerChild settings. Even with conservative Min/MaxSpareServers values, these parameters create conditions for process accumulation:

  • Each child process handles up to 1500 requests before recycling (MaxRequestsPerChild)
  • Apache can spawn up to 1250 concurrent processes (MaxClients)
  • Traffic spikes create processes that persist due to high request limits

Here's a recommended configuration for your Debian 6 LAMP setup with 4GB RAM:


    StartServers            5
    MinSpareServers         5
    MaxSpareServers        10
    ServerLimit           150
    MaxClients            150
    MaxRequestsPerChild  5000
    # Additional performance tweaks
    KeepAlive            On
    KeepAliveTimeout     5
    MaxKeepAliveRequests 100

Use these commands to monitor and manage Apache processes:

# Real-time process monitoring
watch -n 1 "ps -ef | grep apache2 | wc -l"

# Memory usage per process
ps -ylC apache2 --sort:rss

# Detailed process list
apachectl fullstatus

For persistent issues, consider these additional measures:

  1. Install mod_status for real-time monitoring:
    a2enmod status
    service apache2 restart
  2. Set up process limits in /etc/security/limits.conf:
    www-data hard nproc 200
    www-data soft nproc 150
  3. Implement cron-based process cleanup:
    */10 * * * * root /usr/bin/pkill -9 -u www-data -f "apache2.*idle"

If processes continue accumulating after optimization:

# Check for module-specific leaks
apache2ctl -M | grep -E 'php|proxy'

# Analyze strace of runaway processes
strace -p $(pgrep -d, apache2) -c

# Monitor memory allocation
valgrind --leak-check=yes /usr/sbin/apache2 -X

Your current configuration shows:

<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    ServerLimit      1250
    MaxClients            1250
    MaxRequestsPerChild   1500
</IfModule>

This configuration suggests Apache should maintain between 5-10 idle processes, yet you're observing 260+ processes. This discrepancy typically occurs due to several factors.

The main culprits for unexpected process growth in prefork include:

  • Traffic spikes causing temporary process creation
  • Stuck processes waiting for slow clients
  • Modules that maintain persistent connections
  • Improper MaxRequestsPerChild settings causing frequent respawns

For a 4GB RAM system running LAMP, consider these optimized settings:

<IfModule mpm_prefork_module>
    StartServers            4
    MinSpareServers         4
    MaxSpareServers         8
    ServerLimit           400
    MaxClients            400
    MaxRequestsPerChild  10000
</IfModule>

To analyze your running processes:

# Show Apache processes sorted by memory usage
ps -ylC apache2 --sort:rss

# Monitor process creation patterns
watch -n 5 'pgrep apache2 | wc -l'

For processes that won't terminate:

  1. Check for stuck connections:
    netstat -anp | grep apache
  2. Monitor slow requests:
    tail -f /var/log/apache2/access_log | grep -E '([0-9]{2}|[3-9])[0-9]{2}\.'

Consider these additional measures:

  • Implement proper KeepAlive settings:
    KeepAlive On
    KeepAliveTimeout 2
    MaxKeepAliveRequests 100
  • Review loaded modules (apache2ctl -M) and disable unnecessary ones
  • Monitor memory usage per process to determine optimal MaxClients