in Web Hosting ago
The Prefork MPM (Multi-Processing Module) is one of the oldest and most stable ways Apache handles requests. It creates multiple child processes with one thread each, isolating them for safety and compatibility — especially with non-thread-safe libraries like some older PHP versions.

1 Answer

0 votes
ago

Apache Prefork MPM Configuration for Optimal Server Performance

Here is a complete article explaining the Apache Prefork MPM configuration, focusing on each directive and its impact on performance:


⚙️ Understanding Apache Prefork MPM Configuration for Optimal Server Performance

The Prefork MPM (Multi-Processing Module) is one of the oldest and most stable ways Apache handles requests. It creates multiple child processes with one thread each, isolating them for safety and compatibility — especially with non-thread-safe libraries like some older PHP versions.

In this guide, we’ll break down the key directives:

# prefork MPM
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 0

We’ll explain what each does and how you can tune them for the best performance.


What is Prefork MPM?

Apache supports several MPMs (like event, worker, prefork), and Prefork handles each request in its own process. There’s no sharing between requests, making it very stable and secure — ideal for:

  • Legacy PHP applications

  • Systems where thread safety is a concern

  • Simpler traffic models


️ Breakdown of Key Directives

StartServers

StartServers 5
  • Defines how many child server processes are created on startup.

  • Set this to the expected average traffic level to reduce spawning delay.

Tip: Set higher if you expect instant traffic after reboot (e.g., busy sites).


MinSpareServers

MinSpareServers 5
  • Minimum number of idle (spare) child processes to keep ready.

  • Apache will spawn new processes if fewer than this number are idle.

Tip: Prevents delays under bursty traffic. Don’t set too low.


MaxSpareServers

MaxSpareServers 10
  • Maximum number of idle child processes allowed.

  • If exceeded, Apache will kill extra idle processes.

Tip: Helps control memory usage during low activity periods.


MaxRequestWorkers (formerly MaxClients)

MaxRequestWorkers 150
  • The maximum number of simultaneous requests Apache will handle.

  • If all slots are used, new requests are queued until one becomes free.

Tip: This directly affects site concurrency and resource usage.
Set based on your available RAM, PHP memory limit, and average request size.

Formula:
MaxRequestWorkers = Total RAM available for Apache / Memory per process

Example:
If each Apache process uses ~30MB and you have 4GB RAM for Apache:
4096 MB / 30 MB = ~136, so 150 is reasonable.


MaxConnectionsPerChild

MaxConnectionsPerChild 0
  • How many requests a single child process will handle before it is restarted.

  • 0 means unlimited — the process never dies unless Apache is restarted.

Tip: Helps prevent memory leaks in long-running processes.
Set to a high number (e.g., 1000–10000) in production.


Example Use Case: Busy WordPress Site

StartServers 10
MinSpareServers 10
MaxSpareServers 20
MaxRequestWorkers 200
MaxConnectionsPerChild 5000

Why:

  • Higher spare servers for sudden traffic spikes

  • Reasonable limit on MaxRequestWorkers to avoid memory overload

  • Rotating processes to avoid leaks


How to Check Active MPM?

Run this command:

apachectl -V | grep MPM

You should see:

Server MPM: prefork

If not, you can switch with:

sudo a2dismod mpm_event
sudo a2enmod mpm_prefork
sudo systemctl restart apache2

⚠️ Performance Considerations

  • Prefork is not ideal for very high concurrency or modern async apps.

  • Each connection = 1 process = 1 chunk of RAM.

  • For better performance under high load, consider switching to MPM Event or using PHP-FPM with Worker MPM.


✅ Summary: Recommended Defaults for Prefork MPM

DirectiveRecommended
StartServers5–10
MinSpareServers5–10
MaxSpareServers10–20
MaxRequestWorkersDepends on RAM (100–300)
MaxConnectionsPerChild1000–10000 or 0 for unlimited

Conclusion

Apache’s Prefork MPM is a solid, stable choice for traditional PHP applications. Tuning the settings like MaxRequestWorkers and StartServers helps you strike a balance between performance, memory usage, and reliability.

While newer MPMs like event offer better performance in modern setups, Prefork remains relevant when thread safety is required — especially on shared hosting or legacy systems.

Related questions

Category

Follow Us

Stay updated via social channels

Twitter Facebook Instagram Pinterest LinkedIn
...