Apache consuming all memory on a small VPS

Scenario: We have a user with VPS (Centos 6) server. The server will occasionally consume all its memory and lock up completely. Only a “reset” of the Virtual Machine will bring it back as the VMware Tools becomes unresponsive as well. The server only runs a small WordPress site (with MySQL server). It started with 500Mb RAM, then moved to 1GB and finally 2GB when testing but the memory consumption would always eventually cause the VM to lock up.

 

The first thing to do was check the memory utilisation.

Type top  then press SHITF-F then ‘n‘ for memory usage

 

From the above screenshot we can see that the Apache process is using around 50Mb per process.

 

MaxClients and KeepAlives are always worth tweaking in the Apache config. When checking the config I could see that there were values for Worker and Prefork. Which one was I using?

Use this command httpd -V | grep MPM

Note: Prefork is the most commonly used  and works by spawning  individual processes. Worker is a threaded process and apparently more desirable (although I’ve never tested it in anger!) these days but the majority of installations use prefork.

 

Now, onto editing the httpd.conf. You can see in the below screenshot that i’ve tweaked the MaxClients and KeepAliveTimeout settings.

I started at MaxClients=128 but that was gradually knocked down. There is an algorithm you can use to work out the best configuration variables for Apache but trial and error was used in this case.

I also recommended using QuickCache (WordPress Plugin) or equivalent to the end user and upgrade the kernel : (yum update kernel )

This seems have to done the job and the RAM has been around 600KB for the past 2 weeks. Im sure this could be decreased even further by streamlining Apache (removing any unwanted modules) and tweaking the settings even further.

 

Notes:

MaxClients
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.
ServerLimit
For the prefork MPM, this directive sets the maximum configured value for MaxClients for the lifetime of the Apache process. Any attempts to change this directive during a restart will be ignored, but MaxClients can be modified during a restart.

Special care must be taken when using this directive. If ServerLimit is set to a value much higher than necessary, extra, unused shared memory will be allocated. If both ServerLimit and MaxClients are set to values higher than the system can handle, Apache may not start or the system may become unstable.

With the prefork MPM, use this directive only 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.

 

KeepAliveTimeout
The number of seconds Apache will wait for a subsequent request before closing the connection. Once a request has been received, the timeout value specified by the Timeout directive applies.

Setting KeepAliveTimeout to a high value may cause performance problems in heavily loaded se