How to Limit Apache 2.2 Processes: Optimizing MaxClients and ThreadsPerChild for Resource Efficiency


2 views

In Apache 2.2, the server uses a prefork MPM (Multi-Processing Module) by default, which creates multiple child processes to handle requests. Each process consumes memory and CPU resources. The key directives controlling this behavior are:

# Main configuration parameters for Apache 2.2 prefork
StartServers       5
MinSpareServers    5
MaxSpareServers   10
MaxClients       150
MaxRequestsPerChild  0

First, check your current settings by running:

apache2 -V | grep -i mpm
apache2 -l | grep prefork

Then examine your Apache configuration files (typically in /etc/apache2/):

grep -i "MaxClients\|StartServers\|MinSpareServers\|MaxSpareServers" /etc/apache2/*.conf

For a memory-constrained system, you'll want to adjust these values:

# Example optimized configuration for low-resource systems
<IfModule mpm_prefork_module>
    StartServers       2
    MinSpareServers    2
    MaxSpareServers    4
    MaxClients         8
    MaxRequestsPerChild 1000
</IfModule>

To determine optimal MaxClients:

  1. Check average Apache process size: ps -ylC apache2 --sort:rss
  2. Calculate: MaxClients = (Available Memory - System Reserve) / Average Process Size

Consider these complementary optimizations:

# Reduce keepalive time
KeepAliveTimeout 2
MaxKeepAliveRequests 50

# Disable unused modules
a2dismod autoindex
a2dismod status

After making changes, test your configuration:

apache2ctl configtest
service apache2 restart

Monitor the new process count:

watch -n 1 "ps -ef | grep apache | grep -v grep | wc -l"

For better resource efficiency, consider switching to worker MPM:

apt-get install apache2-mpm-worker
a2enmod mpm_worker
a2dismod mpm_prefork

Then configure worker parameters:

# Worker MPM configuration example
<IfModule mpm_worker_module>
    StartServers         2
    MinSpareThreads     25
    MaxSpareThreads     75 
    ThreadLimit         64
    ThreadsPerChild     25
    MaxClients         150
    MaxRequestsPerChild  0
</IfModule>

In Apache 2.2, the MPM (Multi-Processing Module) controls how child processes are managed. From your mods-enabled list, I notice you're likely using prefork MPM (default for Apache 2.2) since there are no worker/event MPM modules loaded. The prefork MPM creates multiple child processes to handle requests, each consuming memory.

For Apache 2.2 with prefork MPM, these are the critical directives in your apache2.conf or httpd.conf:

<IfModule prefork.c>
StartServers       5
MinSpareServers    5
MaxSpareServers   10
MaxClients       150
MaxRequestsPerChild  0
</IfModule>

To reduce the number of Apache processes, focus on these parameters:

# Recommended settings for a low-memory server:
<IfModule prefork.c>
StartServers       2
MinSpareServers    2
MaxSpareServers    5
MaxClients        10
MaxRequestsPerChild 1000
</IfModule>

After modifying these values:

  1. Check current settings: apache2ctl -V | grep -i mpm
  2. Validate configuration: apache2ctl configtest
  3. Restart Apache: service apache2 restart

Use these commands to verify the new process count:

# Show Apache processes
ps aux | grep apache2 | wc -l

# Check memory usage
top -bn1 | grep apache2

For additional resource savings:

  • Disable unused modules (like autoindex, negotiation)
  • Implement caching (mod_cache)
  • Consider switching to lighter MPM like worker if possible
  • Set KeepAliveTimeout to a lower value (5-10 seconds)