Apache Process Memory Usage and Worker Configuration
A bash script for checking Apache process memory usage. This is useful for figuring out server configuration.
Script:
#!/bin/bash
ps -ylC apache2 --sort:rss | \
awk '{sum+=$8; ++n} END \
{print "Tot="sum"("n")"; \
print "Avg="sum"/"n"="sum/n/1024"MB"}'
Example output:
Tot=322300(16) Avg=322300/16=19.6716MB
This means that every Apache process currently takes ~20 MB of RAM. What I do is take about 80% of total FREE memory and divide it on how many processes I can support. Then I know how many workers I should put in my web server configuration.
$ free -m total used free shared buffers cached Mem: 15792 15622 169 8 400 14323 -/+ buffers/cache: 897 14894 Swap: 8183 0 8183 (14894 * 0.8) / 20 = 595.76 If you take into account the 16 processes already used: 14894 + (16 * 19.7) = (15209 * 0.8) / 20 = 608.36 instead. # cat /etc/apache2/mods-enabled/mpm_prefork.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 servesStartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxRequestWorkers 600 MaxConnectionsPerChild 0
This is given that it's a dedicated web server, or else perhaps based yourself on even less than 80% memory. For monitoring worker usage I use Apache2's built-in status.conf module. This is usually enabled to default, it not you can enable it with
a2enmod status
.Lock /etc/apache2/mods-enabled/status.conf for local use first:
SetHandler server-status Require local
Use PHP to read it locally to any location instead using authorization:
if($_SESSION['pass'] == $password) {
echo '<input type="button" value="Refresh" onclick="location.reload()"><br>'.
file_get_contents("https://your-site.com/server-status");
}
Example (most important bit) output of /server-status
1.88 requests/sec - 5.2 kB/second - 2850 B/request 4 requests currently being processed, 8 idle workers _W.W____K_.W_._................................... Scoreboard Key: "_" Waiting for Connection, "S" Starting up, "R" Reading Request, "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup, "C" Closing connection, "L" Logging, "G" Gracefully finishing, "I" Idle cleanup of worker, "." Open slot with no current process
The example above is from a server with only 1 GB RAM and 50 workers (default) but should be about 25.