Httpd – Running out of swap space on web servers, what to do

apache-2.2httpdhttpd.confswap

I have 2 LAMP web servers that are routinely running out of swap space (see attached top screenshot).

Apache's settings are as follows:

<IfModule prefork.c>
StartServers       64
MinSpareServers    64
MaxSpareServers    128
ServerLimit        256
MaxClients         256
MaxRequestsPerChild  4096
</IfModule>

The resource limits on PHP are:

max_execution_time = 30
max_input_time = 30
memory_limit = 80M

I'm new to server admin stuff like this (I'm a developer); What can I adjust so that I can prevent swapping?

Here is a screenshot of top on one of the servers when crashing: screenshot

Best Answer

You're running out of swap because you're using all your RAM and then some. You have a serious problem that you need to rectify right now.

You have two choices: ignore cause and just add more RAM, or target the problem of what's actually munching on your memory.

Adding RAM is fairly cheap and fairly easy if it's your server but it's a temporary fix and if it's a VPS or a rented server, it's not so cheap. Let's have a go at fixing the root problem instead. What's sucking in that much memory? Here are a few tips:

  • Turn off InnoDB (unless you need it) in MySQL
  • Beat Apache (and MySQL) with the Stick of Configuration +5
  • Consider a smaller httpd like cherokee, lighttpd or nginx (they're really fast and eat almost no RAM). Main downside is you can't use .htaccess files but you can hard-code in their functionality.
  • Are you using an OP-code cache for PHP? Try turning it off or switching to another, more efficient one.

In terms of just getting swap items back into real RAM, you can do that by:

swapoff -a && swapon -a

But don't try then when you've got less free memory than you have things in swap. It'll crash your server.