Good Apache best mpm For Php

apache best mpm for php

Good Apache MPM Settings for Low End VPS / Servers – 1GB Memory / 1GB Swap

Apache uses a set of values called the Prefork MPM to determine how many servers it will utilize and how many threads each server can process. Out of the box all Apache installations use the same values regardless of whether your server has 512Mb of RAM or 8Gb of RAM.  It is important that as the server administrator you configure these values to work with your server load.

The Apache Prefork MPM can be found in the Apache configuration file; usually /etc/httpd/conf/httpd.conf.  The default values are…

<IfModule prefork.c>
StartServers       2
MinSpareServers    3
MaxSpareServers    3
ServerLimit       75
MaxClients        75
MaxRequestsPerChild  1000

Each Directive taken from “” is detailed below.

– – – – – – – – – – – –

The StartServers directive sets the number of child server processes created on startup.  As the number of processes is dynamically controlled depending on the load there is usually little reason to adjust this parameter.

– – – – – – – – – – – –

The MinSpareServers directive sets the desired minimum number of idle child server processes. An idle process is one which is not handling a request. If there are fewer than MinSpareServers idle then the parent process creates new children until satisfies the MinSpareServers setting.

– – – – – – – – – – – –

The MaxSpareServers directive sets the desired maximum number of idle child server processes. An idle process is one which is not handling a request. If there are more than MaxSpareServers idle, then the parent process will kill off the excess processes.

– – – – – – – – – – – –

The ServerLimit directive is only used if you need to set MaxClients higher than 256 (default). Do not set the value of this directive any higher than what you might want to set MaxClients to.

– – – – – – – – – – – –

The MaxClients directive sets the limit on the number of simultaneous requests that will be served. Any connection attempts over the MaxClients limit will normally be queued, up to a number based on the ListenBacklog directive. Once a child process is freed at the end of a different request, the connection will then be serviced.

For non-threaded servers (i.e., prefork), MaxClients translates into the maximum number of child processes that will be launched to serve requests. The default value is 256; to increase it, you must also raise ServerLimit.

– – – – – – – – – – – –

The MaxConnectionsPerChild directive sets the limit on the number of connections that an individual child server process will handle. After MaxConnectionsPerChild connections, the child process will die. If MaxConnectionsPerChild is 0, then the process will never expire.

Setting MaxConnectionsPerChild to a non-zero value limits the amount of memory that process can consume by (accidental) memory leakage.

– – – – – – – – – – – –

The single most important directive is MaxClients as this determines the amount of Apache child processes that will be launched to server requests.  A simple calculation for MaxClients would be:

(Total Memory – Critical Services Memory) / Size Per Apache process

I define Critical Services as services such as mySQL, Plesk, Cpanel; any service that is required for proper operation of your server.

I’ve used the following commands via shell to determine values for Total Memory, OS Memory, MySQL Memory, and Apache Process Size

[[email protected] httpd]# free -m
total       used       free     shared    buffers     cached
Mem:          1002        599        402          0         28        337
-/+ buffers/cache:        233        769
Swap:         2047        124       1922

[[email protected] httpd]# ps aux | grep ‘mysql’ | awk ‘{print $6}’

[[email protected] httpd]# ps aux | grep ‘httpd’ | awk ‘{print $6}’

In this case the server has 1002Mb of memory allocated, xx used by the OS itself, 21Mb used by mySQL, and each Apache thread averages about 30Mb.

MaxClients = (1002 – 21) / 30 therefore MaxClients = 32.7

The next important aspect is MaxConnectionsPerChild as this is the amount of threads that will be processed before the child is recycled.

A good calculation for MaxConnectionsPerChild would be:

(total amount of daily requests  / total number of daily processes)

Determining these values is a bit more complex as it requires some type of statistics package or thorough knowledge of interpreting Apache access logs.

As this does not adversely effect memory usage, only cpu time to cycle the process if you are unable to determine this information the standard 1000 should be used.

Thus a good configuration for this server would be

<IfModule prefork.c>
StartServers       2
MinSpareServers    3
MaxSpareServers    3
ServerLimit       30
MaxClients        30
MaxRequestsPerChild  1000

Be sure once you’ve saved the file to perform a configuration test before restarting Apache.

[[email protected] httpd]# service httpd configtest
Syntax OK

[[email protected] httpd]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:


On another note, on my Xen VPS running Directadmin / Ubuntu 10.04 64bit LTS with 1GB allocated memory, 2GB swap, i can comfortably run this config set on my /etc/httpd/conf/extra/httpd-mpm.conf



*** Update 10 October 2013 ***

I have made further tweaks to my Xen virtual server which has 2 cpu cores, 1GB ram allocated / 2GB swap with 40G hard disk space. It appears that the tweaks i made helps to improve the stability of the server running Directadmin hosting my wordpress websites and able to handle more than 100 visitors at any given point of time without freezing like the previous settings i have. Have a look at the Apache and Mysql settings respectively to help improve your settings. See the respective link and configurations below.

Tuning Your Mysql Settings / Conf

Tuning Your Apache Settings / Conf





Leave a Reply