Are you worried about your Apache server performance? Okay let’s talk about Apache Multi-Processing Modules (MPMs). There is a documentation for Apache MPM but who has got time to read the documentations. Let’s talk in simple plain english about Apache Multi-Processing Modules (MPMs). All you need is 15 mins to learn Apache MPMs(happy-face).
What
The MPMs are used to change the basic functionality of the web server. It’s possible due to Apache’s modular design. The MPM, or Multi Processing Module, you use is responsible for just about the entire HTTP session. Starting from listening on the network, taking requests in and most importantly, how to handle those requests. With the MPM you use Apache’s behaviour will change. Apache offers three MPMs to choose from; Prefork, Worker, and Event. You might be wondering which MPM module you should choose. Answer is right below.
How do I select which Apache MPM to use?
Above links explains about the three MPM modules and when to use them. If you don’t know about the Apache MPMs or which one to use , time to start reading.
How
1. Check what your Apache server has got
Most of the Apache server comes with Prefork module. To make sure whether your server has got Prefork, type the below command and see.
apache2ctl -l
See the output below
dasunhegoda@dasun:~$ apache2ctl l Compiled in modules: core.c mod_so.c mod_watchdog.c http_core.c mod_log_config.c prefork.c mod_logio.c mod_version.c mod_unixd.c mod_unixd.c
If the Prefork Module is installed it should be shown under compiled in modules. prefork.c is shown on the list.
2. Let’s install Apache Worker MPM
Let’s configure Worker MPM. So time to install Worker.
apt-get install apache-mpm-worker
If you are willing to install Prefork or Event MPMs it should be as below
apt-get install apache-mpm-prefork
apt-get install apache-mpm-event
When the installation is completed type apache2ctl -l and see whether the prefork.c/worker.c shows up under “Compiled in modules”.
Note in Ubuntu you can only have one MPM module at a time. It means if you install Worker while the server has got Prefork, Prefork will be automatically removed. When you are switching MPMs it’s a good idea to backup your .conf files.
3. Let’s understand Apache MPM directives
Please read the comments below to understand about the each directives and what they do. Below configurations are extracted from apache2.conf
# prefork MPM # StartServers: number of server processes to start # MinSpareServers: minimum number of server processes which are kept spare # MaxSpareServers: maximum number of server processes which are kept spare # MaxClients: maximum number of server processes allowed to start # MaxRequestsPerChild: maximum number of requests a server process serves # # # worker MPM # StartServers: initial number of server processes to start # MinSpareThreads: minimum number of worker threads which are kept spare # MaxSpareThreads: maximum number of worker threads which are kept spare # ThreadLimit: ThreadsPerChild can be changed to this maximum value during a # graceful restart. ThreadLimit can only be changed by stopping # and starting Apache. # ThreadsPerChild: constant number of worker threads in each server process # MaxClients: maximum number of simultaneous client connections # MaxRequestsPerChild: maximum number of requests a server process serves
If you need more information on directives check the documentation.
4. Time to customise the Apache Worker directives as we need
ServerLimit 10 StartServers 2 MaxClients 100 MinSpareThreads 25 MaxSpareThreads 75 ThreadsPerChild 20
Before customising the directives you need to understand how the directives work. Let me explain in plain English. Server will start 2 child processes which is determined by StartServers directive. Each process will start 20 threads which is determined by ThreadsPerChild directive so this means 2 process can service only 40 concurrent connections/clients(i.e. 20×2=40). So what if more requests come in.
Now if more concurrent users come, then another child process will start, that can serve another 20 users. But how many child processes can be started is controlled by ServerLimit parameter, this means that in the configuration above, I can have 10 child processes in total, with each child process can handle 20 thread, in total handling 10×20=200 concurrent users.
But there is a problem, number defined in MaxClients is 100 here, this means that after 5 child processes, no extra process will start since we have defined an upper cap of MaxClients. This also means that if I set MaxClients to 500, after 10 child processes and 200 connections, no extra process will start and we cannot service more than 200 concurrent clients even if we have increase the MaxClient parameter. In this case, we need to also increase ServerLimit to 500/20 i.e. MaxClients/ThreadsPerChild=25
Okay now you know the directives and how they work, the problem is how to calculate the directives. Let’s jump into calculating directive values.
You can use this shell script to determine an average amount of memory consumed by one Apache process. In addition to that it’ll show total amount of memory consumed by all Apache processes. Just unzip and execute with sh command. Accurate results will be shown when server is under heavy load.
The output
Apache Memory Usage (MB): 57.586 Average Proccess Size (MB): 10.2
if in average, let’s assume that one Apache process consumes 50MB RAM and server has got RAM is 2048MB, and you want to leave 512MB for the rest of the processes, then:
MaxClients = (2048MB – 512MB)/10MB = 153.6 ~ 153
So that’s how you configure Apache Multi-Processing Modules. If you have any questions let me know in the comments below. Your feedback is highly appreciated(happy-face).
Thanks for the concise description on how these different settings interact!
Good write up Dasun.
I’m in the process of setting up mpmworker and found your article. It all makes total sense right up until the end. You explain how to calculate one setting MaxClients, but what about the rest? My max clients works out to be 200… so what now? Thanks for any help you can offer.
You can decide other parameters accordingly.
Can you give an example Dasun?
I’m using cPanel server with Apache 2.2.31/RAM 24GB/Server MPM:Prefork. This server shows me Apache MPM directives like :
ServerLimit 256
StartServers 25
MaxClients 250
MinSpareServers 25
MaxSpareServers 50
MaxRequestsPerChild 5000
I used the script and it shows :
Apache Memory Usage (MB): 8552.29
Average Proccess Size (MB): 158.376
Let me know,
1. The above MPM directory values are OK or it further needs fine tuning?
2. My server uses “PHP 5.4.45 suphp with suexec”. Does this server needs MPM worker, if yes, then let me know, installing worker process will create any issues with site accessing
Your help is much appreciaated!..
3. Please correct me if I’m wrong, Your article above provides suggestions for tuning MPM worker not MPM prefork?
Thanks for this information.