Linux – How To Tune Apache prefork mpm values on linux Server

apache-2.2linuxlinux-kernelmemoryvirtualhost

Currently I am on Apache/2.4.23 version and I am running multiple virtual host sites on my webserver behind an ELB. I have 4 instances under my ELB each with 8GB of total RAM. On these webservers I don't see any mpm directives set in my httpd.conf file where as I could see the default values of the mpm modules directives in httpd-mpm.conf file(at /usr/share/doc/httpd24-2.4.23). My webserver is using prefork mpm module

httpd -V | grep MPM
Server MPM:     prefork

Currently all 4 webservers are having only around 200MB of free space left from 8GB and I see there are always around 60 httpd process are running. Below are the metrics

[root@ip ~]# ps -ef | grep httpd | wc -l
58

[root@ip ~]# ps -ylC httpd | awk '{x += $8;y += 1} END {print "Apache Memory Usage (MB): "x/1024; print "Average Proccess Size (MB): "x/((y-1)*1024)}'
Apache Memory Usage (MB): 1640.18
Average Proccess Size (MB): 38.1438

[root@ip ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          7986       7755        231         51        114        233
-/+ buffers/cache:       7407        579
Swap:            0          0          0

Inorder to increase my server performance I am planning to update my httpd.conf file with below prefork mpm directives. The only thing I would like to change is MaxRequestWorkers value from 250 to 400.

<IfModule mpm_prefork_module>
    StartServers             5
    MinSpareServers          5
    MaxSpareServers         10
    MaxRequestWorkers      400
    MaxConnectionsPerChild   0
</IfModule>

Edit: Below are the default settings in httpd-mpm.conf file. But I dont see any prefork mpm modules settings in httpd.conf file thereby assuming that my webserver is using below default values

<IfModule mpm_prefork_module>
    StartServers             5
    MinSpareServers          5
    MaxSpareServers         10
    MaxRequestWorkers      250
    MaxConnectionsPerChild   0
</IfModule>

Below is the top output:

top - 13:26:31 up 21:08,  1 user,  load average: 0.27, 0.17, 0.12
Tasks: 201 total,   1 running, 199 sleeping,   0 stopped,   1 zombie
Cpu(s):  0.7%us,  0.3%sy,  0.0%ni, 98.3%id,  0.7%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   8178632k total,  7985364k used,   193268k free,   113336k buffers
Swap:        0k total,        0k used,        0k free,   189448k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
26918 apache    20   0  764m  53m  39m S  0.0  0.7   0:01.65 httpd
24963 apache    20   0  769m  50m  36m S  0.3  0.6   0:01.39 httpd
26026 apache    20   0  769m  49m  34m S  0.0  0.6   0:00.81 httpd
26116 apache    20   0  769m  47m  32m S  0.0  0.6   0:01.06 httpd
25766 apache    20   0  769m  47m  32m S  0.0  0.6   0:00.95 httpd
27620 apache    20   0  769m  47m  32m S  0.0  0.6   0:00.47 httpd
26548 apache    20   0  769m  47m  32m S  0.0  0.6   0:00.66 httpd
27128 apache    20   0  769m  46m  32m S  0.0  0.6   0:01.16 httpd
28247 apache    20   0  769m  46m  31m S  0.0  0.6   0:00.24 httpd
27670 apache    20   0  769m  46m  31m S  0.0  0.6   0:00.32 httpd
27424 apache    20   0  769m  46m  31m S  0.0  0.6   0:00.41 httpd
24378 apache    20   0  763m  46m  33m S  0.0  0.6   0:01.28 httpd
26800 apache    20   0  763m  45m  32m S  0.0  0.6   0:00.75 httpd
27672 apache    20   0  763m  45m  31m S  0.0  0.6   0:00.53 httpd
26614 apache    20   0  762m  45m  33m S  0.0  0.6   0:00.88 httpd
25098 apache    20   0  762m  44m  32m S  0.0  0.6   0:01.20 httpd
26671 apache    20   0  763m  44m  31m S  0.0  0.6   0:00.64 httpd
27635 apache    20   0  763m  44m  31m S  0.0  0.6   0:00.61 httpd
23499 apache    20   0  763m  43m  29m S  0.0  0.5   0:01.77 httpd
26285 apache    20   0  767m  43m  29m S  0.3  0.5   0:00.70 httpd
27868 apache    20   0  761m  42m  31m S  0.3  0.5   0:00.70 httpd
26444 apache    20   0  763m  42m  29m S  0.0  0.5   0:00.73 httpd
26081 apache    20   0  763m  42m  29m S  0.0  0.5   0:00.68 httpd
25467 apache    20   0  763m  42m  29m S  0.0  0.5   0:00.99 httpd
26412 apache    20   0  763m  42m  28m S  0.0  0.5   0:00.53 httpd
27412 apache    20   0  763m  42m  29m S  0.0  0.5   0:00.45 httpd
26720 apache    20   0  763m  42m  29m S  0.0  0.5   0:00.51 httpd
26179 apache    20   0  763m  42m  29m S  0.0  0.5   0:00.59 httpd
25943 apache    20   0  763m  42m  29m S  0.0  0.5   0:00.78 httpd
27570 apache    20   0  763m  42m  28m S  0.0  0.5   0:00.28 httpd
26721 apache    20   0  763m  42m  28m S  0.0  0.5   0:00.46 httpd
27252 apache    20   0  763m  42m  28m S  0.0  0.5   0:00.54 httpd
27408 apache    20   0  763m  42m  28m S  0.0  0.5   0:00.29 httpd
27612 apache    20   0  763m  42m  28m S  0.0  0.5   0:00.36 httpd
27576 apache    20   0  763m  42m  28m S  0.0  0.5   0:00.28 httpd
27668 apache    20   0  883m  41m  28m S  0.0  0.5   0:00.31 httpd
27626 apache    20   0  763m  41m  28m S  0.0  0.5   0:00.32 httpd

My Question is, will this change help my webserver to use less system memory and will it handle load efficiently. And as the free space is less on my webserver does changing value from 250 to 400 for MaxRequestWorkers will cause any further problem? Is there any better solution to optimize the memory consumption on my server?

Best Answer

I am not sure why you would opt for prefork over worker. Since you are already using Apache/2.4.23. Refer: This Answer right here will advocate use of worker, Also it is far memory and concurrency efficient.

Worker

mpm_worker uses threading - which is a big help for concurrency. Worker spins off some child processes, which in turn spin off child threads; similar to prefork, some spare threads are kept ready if possible, to service incoming connections. This approach is much kinder on RAM, since the thread count doesn't have a direct bearing on memory use like the server count does in prefork. It also handles concurrency much more easily, since the connections just need to wait for a free thread (which is usually available) instead of a spare server in prefork.

PS. Could not comment this because of insufficient points.