Friday 16 December 2016

WHAT & HOW TO CONFIGURE APACHE MULTI-PROCESSING MODULES (MPMs) ~ AwsTechNix



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 behavior will change. Apache offers three MPMs to choose from; PreforkWorker, and Event. You might be wondering which MPM module you should choose. The 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


khimlal@devOps$ 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 directive 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 checks the documentation.

4. Time to customize the Apache Worker directives as we need
ServerLimit            10
StartServers             2
MaxClients          100
MinSpareThreads  25
MaxSpareThreads 75
ThreadsPerChild   20

Before customizing the directives you need to understand how the directives work. Let me explain in plain English. The server will start 2 child processes which are determined by StartServers directive. Each process will start 20 threads which are determined by ThreadsPerChild directive so this means 2 processes 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 increased 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.

ps -ylC apache2 | awk '{x += $8;y += 1} END {print "Apache Memory Usage (MB): "x/1024; print "Average Proccess Size (MB): "x/((y-1)*1024)}'


You can use above shell script to determine an average amount of memory consumed by one Apache process. In addition to that, it’ll show the 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 Process 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).

No comments :

Post a Comment