Reduce number of Apache processes

I'm running Apache 2.2. I have 10 apache processes running, and it's taxing my limited resources.

I have searched for the config option that controls how many processes are spawned, but I could use some help. Is it MaxRequestWorkers? Is it ThreadsPerChild? Something else? (Neither appears to be set in my config files.)

Here's an example of one of my apache processes:

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY        TIME CMD
5 S    33 26099 25513  0  80   0 - 56951 poll_s ?          1:42 /usr/sbin/apache2 -k start

Edit:

I'm sorry. I am running apache 2.2, not 2.4.

list of mods-enabled:

lrwxrwxrwx 1 root root 28 Sep  5  2014 alias.conf -> ../mods-available/alias.conf
lrwxrwxrwx 1 root root 28 Sep  5  2014 alias.load -> ../mods-available/alias.load
lrwxrwxrwx 1 root root 33 Sep  5  2014 auth_basic.load -> ../mods-available/auth_basic.load
lrwxrwxrwx 1 root root 33 Sep  5  2014 authn_file.load -> ../mods-available/authn_file.load
lrwxrwxrwx 1 root root 36 Sep  5  2014 authz_default.load -> ../mods-available/authz_default.load
lrwxrwxrwx 1 root root 38 Sep  5  2014 authz_groupfile.load -> ../mods-available/authz_groupfile.load
lrwxrwxrwx 1 root root 33 Sep  5  2014 authz_host.load -> ../mods-available/authz_host.load
lrwxrwxrwx 1 root root 33 Sep  5  2014 authz_user.load -> ../mods-available/authz_user.load
lrwxrwxrwx 1 root root 32 Sep  5  2014 autoindex.conf -> ../mods-available/autoindex.conf
lrwxrwxrwx 1 root root 32 Sep  5  2014 autoindex.load -> ../mods-available/autoindex.load
lrwxrwxrwx 1 root root 26 Sep  5  2014 cgi.load -> ../mods-available/cgi.load
lrwxrwxrwx 1 root root 30 Sep  5  2014 deflate.conf -> ../mods-available/deflate.conf
lrwxrwxrwx 1 root root 30 Sep  5  2014 deflate.load -> ../mods-available/deflate.load
lrwxrwxrwx 1 root root 26 Sep  5  2014 dir.conf -> ../mods-available/dir.conf
lrwxrwxrwx 1 root root 26 Sep  5  2014 dir.load -> ../mods-available/dir.load
lrwxrwxrwx 1 root root 26 Sep  5  2014 env.load -> ../mods-available/env.load
lrwxrwxrwx 1 root root 27 Sep  5  2014 mime.conf -> ../mods-available/mime.conf
lrwxrwxrwx 1 root root 27 Sep  5  2014 mime.load -> ../mods-available/mime.load
lrwxrwxrwx 1 root root 34 Sep  5  2014 negotiation.conf -> ../mods-available/negotiation.conf
lrwxrwxrwx 1 root root 34 Sep  5  2014 negotiation.load -> ../mods-available/negotiation.load
lrwxrwxrwx 1 root root 32 Sep 12  2014 passenger.conf -> ../mods-available/passenger.conf
lrwxrwxrwx 1 root root 32 Sep 12  2014 passenger.load -> ../mods-available/passenger.load
lrwxrwxrwx 1 root root 27 Sep  5  2014 php5.conf -> ../mods-available/php5.conf
lrwxrwxrwx 1 root root 27 Sep  5  2014 php5.load -> ../mods-available/php5.load
lrwxrwxrwx 1 root root 33 Sep  5  2014 reqtimeout.conf -> ../mods-available/reqtimeout.conf
lrwxrwxrwx 1 root root 33 Sep  5  2014 reqtimeout.load -> ../mods-available/reqtimeout.load
lrwxrwxrwx 1 root root 40 Sep 14  2014 rewrite.load -> /etc/apache2/mods-available/rewrite.load
lrwxrwxrwx 1 root root 31 Sep  5  2014 setenvif.conf -> ../mods-available/setenvif.conf
lrwxrwxrwx 1 root root 31 Sep  5  2014 setenvif.load -> ../mods-available/setenvif.load
lrwxrwxrwx 1 root root 36 Jun  7  2015 ssl.conf -> /etc/apache2/mods-available/ssl.conf
lrwxrwxrwx 1 root root 36 Jun  7  2015 ssl.load -> /etc/apache2/mods-available/ssl.load
lrwxrwxrwx 1 root root 29 Sep  5  2014 status.conf -> ../mods-available/status.conf
lrwxrwxrwx 1 root root 29 Sep  5  2014 status.load -> ../mods-available/status.load

Solution 1:

It depends which mpm module (mpm_worker/mpm_prefork/mpm_event) your apache is using. If you are unsure, then post the output of cat /etc/apache2/mods-enabled/mpm*.conf which reveals the number of StartServers/MinSpareServers/MaxSpareServers, MaxRequestWorkers and maybe ThreadsPerChild and ThreadLimit. If the filenames should be different in your linux distro, then post an output of your enabled modules ls -l /etc/apache2/mods-enabled.

It should look like something like this and explains itself (Debian, Apache2.4):

root@debian:/# cat /etc/apache2/mods-enabled/mpm*.conf
# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxRequestWorkers: maximum number of server processes allowed to start
# MaxConnectionsPerChild: maximum number of requests a server process serves

<IfModule mpm_prefork_module>
    StartServers             5
    MinSpareServers       5
    MaxSpareServers      10

    # same as MaxClients in Apache 2.2
    MaxRequestWorkers     150
    MaxConnectionsPerChild   0
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

See Reducing Apache Memory usage and Average Process Size Value

The documentation says (as described in the link above):

You can, and should, control the MaxClients setting so that your server does not spawn so many children that it starts swapping. The procedure for doing this is simple: determine the size of your average Apache process, by looking at your process list via a tool such as top, and divide this into your total available memory, leaving some room for other processes. https://httpd.apache.org/docs/2.2/misc/perf-tuning.html

Example:

Tasks: 207 total,   1 running, 206 sleeping,   0 stopped,   0 zombie
%Cpu(s):  2.4 us,  0.8 sy,  0.0 ni, 96.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16307588 total, 14360744 free,  1188636 used,   758208 buff/cache
KiB Swap:        0 total,        0 free,        0 used. 14686936 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                 
 6567 www-data  20   0  296028  15288   5616 S   0.0  0.1   0:00.07 apache2                                                                 
 6569 www-data  20   0  296040  15360   5676 S   0.0  0.1   0:00.08 apache2                                                                 
 6571 www-data  20   0  295996  15200   5676 S   0.0  0.1   0:00.07 apache2                                                                 
 6572 www-data  20   0  296028  15348   5676 S   0.0  0.1   0:00.08 apache2                                                                 
 6573 www-data  20   0  296040  15356   5676 S   0.0  0.1   0:00.07 apache2

Running the cool script from the linked page above gives me:

root@debian:~# ps -ylC apache2 | awk '{x += $8;y += 1} END {print "Apache Memory Usage (MB): "x/1024; print "Average Process Size (MB): "x/((y-1)*1024)}'
Apache Memory Usage (MB): 100.711
Average Process Size (MB): 16.7852

Note: "Average Process Size" is the "RES" value when you run top.

To determine MaxClients (aka MaxRequestWorkers), I need to calculate:

Maxclients=X/Y where
   X=Max. Available Memory Reserved for Apache
   Y=Average Process Size