Setting Apache2.2 prefork settings correctly

Apache's memory consumption is regulated by the spawned server processes and can easily consume all of your servers memory. If the prefork-module is used following paramters needs to be configured in your httpd.conf:

StartServers          18
MinSpareServers       3
MaxSpareServers       6
ServerLimit           60
MaxClients            60
MaxRequestsPerChild   4000

Now to get this parameters right use the following script to determine the actual memory consumption of one apache child process:

​pgrep httpd | xargs -n1 -I{} cat /proc/{}/smaps | awk '{if ($0 ~ /stack/) {pids+=1} else if ($0 ~/^Shared_/) {shared+=$2} else if ($0 ~ /^Pss:/) {priv+=$2}} END {printf "%.2f MB\n",(priv+shared/(pids*pids))/1024}'

Now to get MaxClients simply take the amount you want to give to apache and divide it with the consumption of one process, depending on the actual load the results may vary. Typically one process took between 30MB and 50MB. The above mentioned configuration is set for 3GB RAM (3*1024MB). Here a thumbrule:

StartServers          = 30% of MaxClients
MinSpareServers       = 5% of MaxClients
MaxSpareServers       = 10% of MaxClients
ServerLimit           = MaxClients
MaxClients            = total memory / one process memory (round down to be safe)
MaxRequestsPerChild   4000