{"id":2107,"date":"2013-06-26T11:20:46","date_gmt":"2013-06-26T03:20:46","guid":{"rendered":"http:\/\/rmohan.com\/?p=2107"},"modified":"2013-06-27T10:09:55","modified_gmt":"2013-06-27T02:09:55","slug":"tuning-the-apache-prefork-mpm","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=2107","title":{"rendered":"Tuning the Apache Prefork MPM"},"content":{"rendered":"<p>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.\u00a0 It is important that as the server administrator you configure these values to work with your server load.<\/p>\n<p>The Apache Prefork MPM can be found in the Apache configuration file; usually \/etc\/httpd\/conf\/httpd.conf.\u00a0 The default values are&#8230;<\/p>\n<p>&lt;IfModule prefork.c&gt;<br \/>\nStartServers\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2<br \/>\nMinSpareServers\u00a0\u00a0\u00a0 3<br \/>\nMaxSpareServers\u00a0\u00a0\u00a0 3<br \/>\nServerLimit\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 75<br \/>\nMaxClients\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 75<br \/>\nMaxRequestsPerChild\u00a0 1000<br \/>\n&lt;\/IfModule&gt;<\/p>\n<p>Each Directive taken from &#8220;http:\/\/httpd.apache.org\/docs\/trunk\/mod\/mpm_common.html&#8221; is detailed below.<\/p>\n<p>&#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211;<\/p>\n<p>The StartServers directive sets the number of child server processes created on startup.\u00a0 As the number of processes is dynamically controlled depending on the load there is usually little reason to adjust this parameter.<\/p>\n<p>&#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211;<\/p>\n<p>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.<\/p>\n<p>&#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211;<\/p>\n<p>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.<\/p>\n<p>&#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211;<\/p>\n<p>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.<\/p>\n<p>&#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211;<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>&#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211;<\/p>\n<p>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.<\/p>\n<p>Setting MaxConnectionsPerChild to a non-zero value limits the amount of memory that process can consume by (accidental) memory leakage.<\/p>\n<p>&#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211; &#8211;<\/p>\n<p>The single most important directive is MaxClients as this determines the amount of Apache child processes that will be launched to server requests.\u00a0 A simple calculation for MaxClients would be:<\/p>\n<p>(Total Memory &#8211; Critical Services Memory) \/ Size Per Apache process<\/p>\n<p>I define Critical Services as services such as mySQL, Plesk, Cpanel; any service that is required for proper operation of your server.<\/p>\n<p>I&#8217;ve used the following commands via shell to determine values for Total Memory, OS Memory, MySQL Memory, and Apache Process Size<\/p>\n<p>TOTAL MEMORY<br \/>\n[root@vps httpd]# free -m<br \/>\ntotal\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 used\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 free\u00a0\u00a0\u00a0\u00a0 shared\u00a0\u00a0\u00a0 buffers\u00a0\u00a0\u00a0\u00a0 cached<br \/>\nMem:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1002\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 599\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 402\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 28\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 337<br \/>\n-\/+ buffers\/cache:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 233\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 769<br \/>\nSwap:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2047\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 124\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1922<\/p>\n<p>MYSQL MEMORY<br \/>\n[root@vps httpd]# ps aux | grep &#8216;mysql&#8217; | awk &#8216;{print $6}&#8217;<br \/>\n408<br \/>\n21440<br \/>\n704<\/p>\n<p>APACHE PROCESS SIZE<br \/>\n[root@vps httpd]# ps aux | grep &#8216;httpd&#8217; | awk &#8216;{print $6}&#8217;<br \/>\n22468<br \/>\n11552<br \/>\n41492<br \/>\n40868<br \/>\n41120<br \/>\n41696<br \/>\n39488<br \/>\n41704<br \/>\n15552<br \/>\n16076<br \/>\n16084<br \/>\n728<\/p>\n<p>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.<\/p>\n<p>MaxClients = (1002 &#8211; 21) \/ 30 therefore MaxClients = 32.7<\/p>\n<p>The next important aspect is MaxConnectionsPerChild as this is the amount of threads that will be processed before the child is recycled.<\/p>\n<p>A good calculation for MaxConnectionsPerChild would be:<\/p>\n<p>(total amount of daily requests\u00a0 \/ total number of daily processes)<\/p>\n<p>Determining these values is a bit more complex as it requires some type of statistics package or thorough knowledge of interpreting Apache access logs.<\/p>\n<p>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.<\/p>\n<p>Thus a good configuration for this server would be<\/p>\n<p>&lt;IfModule prefork.c&gt;<br \/>\nStartServers\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2<br \/>\nMinSpareServers\u00a0\u00a0\u00a0 3<br \/>\nMaxSpareServers\u00a0\u00a0\u00a0 3<br \/>\nServerLimit\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 30<br \/>\nMaxClients\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 30<br \/>\nMaxRequestsPerChild\u00a0 1000<br \/>\n&lt;\/IfModule&gt;<\/p>\n<p>Be sure once you&#8217;ve saved the file to perform a configuration test before restarting Apache.<\/p>\n<p>[root@vps httpd]# service httpd configtest<br \/>\nSyntax OK<\/p>\n<p>[root@vps httpd]# service httpd restart<br \/>\nStopping httpd:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [\u00a0 OK\u00a0 ]<br \/>\nStarting httpd:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [\u00a0 OK\u00a0 ]<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>TCP TUNNING\u00a0 ON OS LEVEL<\/p>\n<p>&nbsp;<\/p>\n<p>TCP TUNNING ON OS LEVEL<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>net.ipv4.tcp_timestamps=0<\/p>\n<p>net.ipv4.tcp_tw_reuse=1<\/p>\n<p>net.ipv4.tcp_tw_recycle=1<\/p>\n<p>&nbsp;<\/p>\n<p>net.ipv4.tcp_fin_timeout=30<\/p>\n<p>net.ipv4.tcp_keepalive_time=1800<\/p>\n<p>net.core.rmem_max=8388608<\/p>\n<p>net.core.wmem_max=8388608<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>net.core.rmem_default=65536<\/p>\n<p>net.core.wmem_default=65536<\/p>\n<p>net.ipv4.tcp_rmem=&#8217;4096 87380 8388608&#8242;<\/p>\n<p>net.ipv4.tcp_wmem=&#8217;4096 65536 8388608&#8242;<\/p>\n<p>net.ipv4.tcp_mem=&#8217;8388608 8388608 8388608&#8242;<\/p>\n<p>net.ipv4.route.flush=1<\/p>\n<p>net.ipv4.tcp_max_syn_backlog=4096<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2107"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2107"}],"version-history":[{"count":6,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2107\/revisions"}],"predecessor-version":[{"id":2109,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2107\/revisions\/2109"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}