I desperately need help in figuring out how to troubleshoot this problem I'm having. I run a fairly mission critical web server (Debian 7.5, 512MB RAM, 512MB swap, Apache, MySQL). It runs a couple WordPress sites on it. Today I found the websites responding quite slowly, and ssh'd in to find the load average was just above 10.0, and RAM use was at 100% and swap was close to the 512MB limit.
I have no idea how to figure out what's going on. Is Apache or MySQL not tuned properly? Maybe someone is attacking the server with repeated hits (how would I know?). I installed htop
but even if I saw that Apache or MySQL was eating up a ton of resources, how would I figure out why?
Any points in the right direction would be massively appreciated. I'm at a loss here and I have to keep this server stable.
Side note: The server was up for 30 days, so maybe this was some sort of slow leak. Now that I've rebooted, load average is at 0.45 1.10 0.88, RAM is 165/512MB and swap is 68/512MB.
UPDATE: Still having issues. I captured a screenshot of htop
below.
Congratulations, you've managed to use nearly all of your swap space.
The first obvious problem here is that you went very deep into swap. This is probably what's causing the system to thrash so hard (lots of time spent in system, I/O wait and software interrupts).
First thing to do is to reduce the number of Apache processes that are running. You don't need that many for a small site, and it's just going to throw you deep into swap and kill your performance...which is what already happened. I would recommend you start very small and increase when it becomes necessary. An example:
This limits you to only serving 5 simultaneous requests (everyone else has to wait in line). If at this point you get warnings from Apache about running out of servers, and you still have RAM to spare, then you can increase them, but you are eventually going to run into a point where your VPS simply hasn't got enough RAM to handle all your traffic. At that point you should upgrade the VPS.
Before anything, based on the screenshot you have posted with
htop
output, it seems you have 512MB of RAM on a site running WordPress? I have never seen WordPress happy on servers less than 1GB of RAM. Maybe if you are running a test or development site, 512MB is adequate, but for a production site you need 1GB of RAM. That is the root of your problem. That said, here are some ideas to help you squeeze out better performance from the setup you have:First, I would not panic about an attack happening. The reality is that your server is probably just taking on a high load of legitimate traffic, but the server itself is not configured/tuned for your usage. Of course bad configurations can bring your sit down during DDoS (Distributed Denial of Service) attack, but it can all make your life miserable when nice/normal traffic suddenly shows up in high numbers.
I posted a nice list of items you can review to improve your L.A.M.O. stack performance on another similar question, and will repost here for your reference:
KeepAlive
setting in Apache that works great! But out of the box, I believe it’s set to aMaxKeepAliveRequests
of 100 which is fairly nuts. I usually set this to about 30 connections with a smallKeepAliveTimeout
of 2 to 3 seconds. The key is to have theKeepAliveTimeout
to match speed it takes for an average page to download with a little bit of room for overhead/slowness. So if a page loads in 1 second, do aKeepAliveTimeout
of 2 seconds.memory_limit
in yourphp.ini
and make sure it’s not higher than necessary. The default is 64M, but in many cases that can be lowered to 32M.Regarding the MySQL tuning, that is something that can take a few weeks to nail down at the beginning. The reason being tuning scripts are based on real traffic MySQL sees. So you basically make your site live to the world, wait 2 days (at least), run the tuning scripts & then wait a few more days to tune some more. After a week or so you should be able to tune MySQL to work as well as it can with your setup.
First install these plugins: super cache (htaccess mode), wpbase, widget cache. Wordpress is known for having these issues. IF that doesn't help, it might be a memory leak on one of your themes (especially if mobile view is enabled).
Post your access and error logs.
I strongly recommend working with nginx instead of apache.