Skip to content

Apache Httpd Optimization on Processes and Requests

First, check what the type of MPM we are running with:

[root@web ~]# apachectl -l
Compiled in modules:
  core.c
  prefork.c
  http_core.c
  mod_so.c

Since we are running "prefork" rather than "worker", we should search the section of <IfModule prefork.c> in httpd.conf, where the directives are our targets for tuning. Let's see the original values for this module.

[root@web ~]# cat /etc/httpd/conf/httpd.conf
...
<IfModule prefork.c>
StartServers       8
MinSpareServers    5
MaxSpareServers   20
ServerLimit      256
MaxClients       256
MaxRequestsPerChild  4000
</IfModule>
...

Their respective features are excerpted from the official document and listed below. You can also refer to Apache HTTP Server Version 2.2 Documentation for more information.

Directive Description Summary
StartServers Number of child server processes created at startup 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.
...
MinSpareServers Minimum number of idle child server processes 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 at a maximum rate of 1 per second .
...
MaxSpareServers Maximum number of idle child server processes 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 .
...
ServerLimit Upper limit on configurable number of processes ...

Special care must be taken when using this directive. If ServerLimit is set to a value much higher than necessary, extra, unused shared memory will be allocated. If both ServerLimit and MaxClients are set to values higher than the system can handle, Apache may not start or the system may become unstable.

With the prefork MPM, use this directive only 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.

...
MaxClients Maximum number of connections that will be processed simultaneously

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.

...
MaxRequestsPerChild Limit on the number of requests that an individual child server will handle during its life

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

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

Note
For KeepAlive requests, only the first request is counted towards this limit. In effect, it changes the behavior to limit the number of connections per child.

...

The set of original (default) values in httpd.conf seems meaningful and can be a good staring point for web servers with 1GB memory, so it could be easier to determined the values of directives by taking the value of server memory Ram (in unit GB) as a factor of the values.

  • StartServers = 8 * Ram
  • MinSpareServers = 5 * Ram
  • MaxSpareServers = 20 * Ram
  • ServerLimit = 256 * Ram
  • MaxClients = ServerLimit
  • MaxRequestsPerChild = 4000 * Ram
For a server with 0.5GB (512MB) memory, Ram = 0.5, which is pretty tight for a production machine, the directives can be:
  • StartServers = 8 * 0.5 = 4
  • MinSpareServers = 5 * 0.5 ≃ 2
  • MaxSpareServers = 20 * 0.5 = 10
  • ServerLimit = 256 * 0.5 = 128
  • MaxClients = ServerLimit = 128
  • MaxRequestsPerChild = 4000 * 0.5 = 2000

Now, we have a starting point for our web server with 512MB memory, let's reflect the values to httpd.conf:

[root@web ~]# vi /etc/httpd/conf/httpd.conf
...
<IfModule prefork.c>
StartServers       4
MinSpareServers    2
MaxSpareServers   10
ServerLimit      128
MaxClients       128
MaxRequestsPerChild  2000
</IfModule>
...

Don't forget to check the syntax of httpd.conf for safety

[root@web ~]# apachectl -t
Syntax OK

And the last step, restart httpd service.

[root@web ~]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

Please note that, MaxClients and ServerLimit should be equal to each other. Let's say we have MaxClients 512 and ServerLimit 256, they are not equal to each other, then you will see the warning when syntax checking:

[root@web ~]# apachectl -t
WARNING: MaxClients of 512 exceeds ServerLimit value of 256 servers,
lowering MaxClients to 256. To increase, please see the ServerLimit
directive.
Syntax OK

Leave a Reply

Your email address will not be published. Required fields are marked *