Skip to main content

Working with Multi-Processing Modules

1. Understanding MPM Architecture

MPM Description Use Case Notes
worker Uses multiple child processes, each with multiple threads High traffic sites with moderate memory usage Default MPM in many distributions
prefork Uses multiple child processes, each handling one request at a time Compatibility with legacy modules or when stability is preferred Higher memory usage due to process duplication
event Uses multiple child processes, each with multiple threads and event-based handling High-concurrency environments with long-lived connections Available since Apache 2.4; supports HTTP/1.1 keep-alive

Example: MPM Configuration

<IfModule mpm_worker_module>
    ServerLimit 16
    ThreadLimit 64
    MaxRequestWorkers 1024
    ThreadsPerChild 64
</IfModule>
Note: MPMs are loaded as modules and can be configured via directives in the main server configuration file.

2. Checking Active MPM

Command Description Output
httpd -V Displays compiled-in modules including MPM
-D APACHE24
-D HTTPD_ROOT="/usr/local/apache2"
-D SERVER_CONFIG_FILE="conf/httpd.conf"
-D MPM_NAME="event"
httpd -M List all loaded modules, including MPM
mpm_event_module (static)
rewrite_module (shared)

Example: Determine MPM from config

<IfModule mpm_event_module>
    ServerLimit 16
</IfModule>

3. Configuring Prefork MPM

Directive Description Default Value Notes
StartServers Number of child processes to start at startup 5 Controls initial server capacity
MinSpareServers Minimum number of idle servers to keep running 5 Ensures availability during low traffic
MaxSpareServers Maximum number of idle servers to allow 10 Prevents over-allocation of resources
MaxRequestWorkers Maximum number of simultaneous requests that can be served 150 Also known as MaxClients in older versions
ServerLimit Maximum value for MaxRequestWorkers 256 Must be set before MaxRequestWorkers
MaxConnectionsPerChild Number of connections a child process will handle before dying 10000 Helps prevent memory leaks

Example: Basic Prefork MPM Configuration

<IfModule mpm_prefork_module>
    StartServers 5
    MinSpareServers 5
    MaxSpareServers 10
    MaxRequestWorkers 150
    ServerLimit 256
    MaxConnectionsPerChild 10000
</IfModule>
Note: The Prefork MPM is ideal for servers where each request needs to be handled by a separate process. It's not suitable for high-concurrency environments due to its memory usage.

4. Configuring Worker MPM

Directive Values Description
ServerLimit 200 Maximum number of server processes that can be configured
MaxRequestWorkers 150 Maximum number of simultaneous requests that can be handled
ThreadsPerChild 25 Number of threads per child process
MaxConnectionsPerChild 10000 Maximum number of connections a child process will handle before dying

Example: Basic Worker MPM Configuration

<IfModule mpm_worker_module>
    ServerLimit 200
    MaxRequestWorkers 150
    ThreadsPerChild 25
    MaxConnectionsPerChild 10000
</IfModule>
Note: The mpm_worker_module is designed for systems with multiple CPU cores and high traffic loads. It uses a hybrid of processes and threads to handle requests efficiently.

5. Configuring Event MPM

Directive Values Description
ServerLimit 2000 Maximum number of server processes allowed
MaxRequestWorkers 1500 Maximum number of connections that will be handled
ThreadsPerChild 25 Number of threads per child process
ListenBacklog 511 Maximum length of the queue of pending connections

Example: Basic Event MPM Configuration

<IfModule mpm_event_module>
    ServerLimit 2000
    MaxRequestWorkers 1500
    ThreadsPerChild 25
    ListenBacklog 511
</IfModule>
Note: The Event MPM is designed to handle a large number of concurrent connections efficiently by using threads within processes.

6. Setting StartServers Value

Directive Values Description
StartServers 5 Number of child processes to start at server startup

Example: Setting StartServers in httpd.conf

<IfModule mpm_prefork_module>
    StartServers 10
</IfModule>
Note: The StartServers directive is specific to the mpm_prefork_module. For mpm_worker_module or mpm_event_module, use StartServers in conjunction with ServerLimit and MaxRequestWorkers.

7. Configuring Spare Server Settings

Directive Values Description
ServerLimit 256 Maximum number of server processes that can be configured. Default is 256.
MinSpareServers 5 Minimum number of idle server processes to maintain. Default is 5.
MaxSpareServers 10 Maximum number of idle server processes to allow. Default is 10.
MaxRequestWorkers 150 Maximum number of requests that can be handled simultaneously. Default is 150.
MaxConnectionsPerChild 10000 Number of connections a child process will handle before exiting. Default is 10000.

Example: Basic Configuration

<IfModule mpm_prefork_module>
    ServerLimit 256
    MinSpareServers 5
    MaxSpareServers 10
    MaxRequestWorkers 150
    MaxConnectionsPerChild 10000
</IfModule>
Note: These directives are specific to the mpm_prefork_module. For other MPMs like mpm_worker_module, different directives apply.

8. Setting ServerLimit Value

The ServerLimit directive in Apache HTTP Server defines the maximum value that can be set for the MaxRequestWorkers directive. It is used to configure the maximum number of worker processes or threads that can be created by the MPM (Multi-Processing Module).

Directive Values Description
ServerLimit integer Defines the maximum value for MaxRequestWorkers.

Example: Setting ServerLimit

<IfModule mpm_prefork_module>
    ServerLimit 1000
    MaxRequestWorkers 500
</IfModule>
Note: The ServerLimit directive must be set in the global server configuration context and cannot be overridden in virtual hosts. It is typically used in conjunction with MaxRequestWorkers to control the maximum number of concurrent requests handled by the server.
Warning: Setting ServerLimit too high can lead to resource exhaustion and system instability. Ensure that the server has sufficient memory and CPU resources to handle the configured limit.

9. Configuring ThreadLimit Value

Directive Values Description
ThreadLimit 1 to 20000 Sets the maximum number of threads that can be created by the server. Default is 64.

Example: Setting ThreadLimit in httpd.conf

<IfModule mpm_worker_module>
    ThreadLimit 1024
</IfModule>
Note: The ThreadLimit directive must be set before any ThreadsPerChild or MaxRequestWorkers directives in the configuration file.
Warning: Setting ThreadLimit too high can lead to resource exhaustion and system instability. Ensure sufficient memory is available.

10. Switching Between MPMs

Apache HTTP Server supports multiple Multi-Processing Modules (MPMs) that handle incoming requests. The choice of MPM can significantly impact server performance and resource usage.

MPM Description Use Case Notes
worker Uses a hybrid multi-process and multi-threaded model. Each child process contains multiple threads to handle requests. High traffic with moderate memory usage Default on most Unix systems
prefork Uses a multi-process model where each process handles one request at a time. Compatibility with non-thread-safe modules Higher memory usage than worker
event Uses a multi-process model with threads to handle requests, similar to worker but optimized for handling long-lived connections. High concurrency with persistent connections Available since Apache 2.4

Example: Enabling MPM

# Load the desired MPM module
LoadModule mpm_worker_module modules/mod_mpm_worker.so

# Or for prefork
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so

# Or for event
LoadModule mpm_event_module modules/mod_mpm_event.so
Note: MPMs cannot be switched at runtime. The server must be restarted after changing the MPM.
Warning: Mixing MPMs or using incompatible modules can lead to unexpected behavior or server crashes.

MPM Selection Guidelines

  • worker: Best for high traffic with moderate memory usage. Ideal for systems with sufficient RAM.
  • prefork: Use when compatibility with non-thread-safe modules is required. Consumes more memory.
  • event: Optimal for servers handling many persistent connections (e.g., WebSocket servers).

11. Tuning MPM for Performance

MPM Configuration Directive Default Value Description
worker ServerLimit 256 Maximum number of server processes that can be created
worker MaxRequestWorkers 150 Maximum number of requests that can be served simultaneously
worker ThreadsPerChild 64 Number of threads per child process
worker MaxConnectionsPerChild 0 Number of connections a child process will handle before dying
prefork ServerLimit 256 Maximum number of server processes that can be created
prefork StartServers 5 Number of child processes to start at startup
prefork MinSpareServers 5 Minimum number of idle server processes
prefork MaxSpareServers 10 Maximum number of idle server processes
prefork MaxRequestWorkers 150 Maximum number of requests that can be served simultaneously
prefork MaxConnectionsPerChild 0 Number of connections a child process will handle before dying
event ServerLimit 256 Maximum number of server processes that can be created
event MaxRequestWorkers 150 Maximum number of requests that can be served simultaneously
event ThreadsPerChild 64 Number of threads per child process
event MaxConnectionsPerChild 0 Number of connections a child process will handle before dying

Example: Tuning the Worker MPM

<IfModule mpm_worker_module>
    ServerLimit 1024
    MaxRequestWorkers 2048
    ThreadsPerChild 64
    MaxConnectionsPerChild 10000
</IfModule>

Example: Tuning the Prefork MPM

<IfModule mpm_prefork_module>
    ServerLimit 1024
    StartServers 10
    MinSpareServers 5
    MaxSpareServers 20
    MaxRequestWorkers 2048
    MaxConnectionsPerChild 10000
</IfModule>
Note: Adjust these values based on your server's available memory and CPU resources. Monitor performance to find optimal settings.

12. Understanding MPM and PHP Compatibility

MPM PHP Support Notes
worker NEW PHP 8.0+ Supports PHP via mod_php or PHP-FPM
prefork PHP 7.4+ Stable, widely compatible
event PHP 8.0+ High performance, minimal memory usage

Example: PHP-FPM with Event MPM

LoadModule mpm_event_module modules/mod_mpm_event.so
<IfModule mpm_event_module>
    ServerLimit 16
    ThreadLimit 64
    ThreadsPerChild 64
    MaxRequestWorkers 1024
</IfModule>

# PHP-FPM configuration
<FilesMatch \.php$>
    SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
Note: Event MPM is ideal for high-concurrency PHP applications, offering better memory efficiency than prefork.
Warning: mod_php is not compatible with the event MPM due to its thread-safety limitations.

Was this article helpful?