Mysql – Memory usage on the 512MB Debian Rackspace VPS

apache-2.2debianMySQL

Hi I'm running Apache2/PHP5/MYSQL on a 512MB Debian VPS to serve a WordPress site.
The site is pretty busy, maybe over 1000 visits a day. The blog is a news blog serving images, text and allowing comments.
Since moving to the VPS, 2 days ago, the site has been down twice due to memory issues.
I've run Top and got this:

Tasks:  93 total,   1 running,  92 sleeping,   0 stopped,   0 zombie
Cpu(s):  2.3%us,  0.1%sy,  0.0%ni, 97.2%id,  0.2%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    517980k total,   458876k used,    59104k free,    12900k buffers
Swap:  1044216k total,     9060k used,  1035156k free,    87592k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
14087 www-data  20   0  302m  42m 3880 S   11  8.3   0:03.67 apache2
14086 www-data  20   0  299m  38m 3896 S    5  7.7   0:03.56 apache2
13779 mysql     20   0 68240  11m 6572 S    1  2.3   0:03.93 mysqld
14455 john      20   0 19072 1316 1000 R    0  0.3   0:00.13 top
    1 root      20   0  8356  604  572 S    0  0.1   0:00.42 init

I've tried to optimise both Mysql and Apache, but Apache still seems to be taking an awful lot of memory – 302M virtual and 42M per process.

  • Is this normal?
  • Is there anything I can do to reduce Apache's load?
  • Will moving to worker-mpm make a big difference?

Thank you

UPDATE

After moving to worker mpm I saw a rise in memeory being used when the server was pretty much idle. But this is the readout from Top when I am posting a story to the site –

Tasks: 237 total,   2 running, 230 sleeping,   0 stopped,   5 zombie
Cpu(s):  3.0%us,  1.1%sy,  0.0%ni, 29.8%id, 66.0%wa,  0.0%hi,  0.0%si,  0.1%st
Mem:   1042268k total,   764492k used,   277776k free,     6304k buffers
Swap:  2096472k total,  1275928k used,   820544k free,    49472k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
13520 www-data  20   0  265m  50m 4852 R   10  5.0   0:00.60 php-fcgi-wrappe
13282 www-data  20   0  246m  28m 2576 D    5  2.8   0:00.96 php-fcgi-wrappe
13081 www-data  20   0  247m  29m 2620 D    4  2.9   0:00.45 php-fcgi-wrappe
  953 mysql     20   0  198m  17m 2832 S    3  1.8   4:34.19 mysqld
13517 www-data  20   0  254m  40m 4620 S    3  4.0   0:00.46 php-fcgi-wrappe
13560 www-data  20   0  250m  36m 3380 S    1  3.6   0:00.32 php-fcgi-wrappe
13290 www-data  20   0  253m  35m 3712 D    1  3.5   0:01.08 php-fcgi-wrappe
13557 www-data  20   0  249m  36m 3300 S    1  3.5   0:00.32 php-fcgi-wrappe
13221 www-data  20   0  250m 9932 2008 D    1  1.0   0:00.68 php-fcgi-wrappe
13594 john      20   0 19208 1468 1000 R    1  0.1   0:00.05 top
  655 root      20   0  9140  996  876 S    0  0.1   0:01.39 xe-daemon
13232 www-data  20   0  197m 2876 1008 D    0  0.3   0:00.59 php-fcgi-wrappe
13281 www-data  20   0  134m 1772  516 D    0  0.2   0:00.03 php-fcgi-wrappe
13294 www-data  20   0  196m 2748  924 D    0  0.3   0:00.44 php-fcgi-wrappe
13296 www-data  20   0  212m 2412  892 D    0  0.2   0:00.57 php-fcgi-wrappe
15262 www-data  20   0  482m 6128 1288 S    0  0.6   0:15.39 apache2
    1 root      20   0  8356  600  572 S    0  0.1   0:01.02 init

As you can see there are a lot of processes running, Apache is at a small 6MB, the PHP-FCGI-Wrapper is quite large and there are many of them. But the funny thing is my overall free memory is more than when the system is doing little or nothing.
Why is this?
Are there other things I can do to reduce the amount of memory being used?
I read that Nginx is another option, but I already run W3 Total Cache plugin, PHP APC, Mysql Query caching and Cloudflare. Will Nginx help with this setup?

Thanks

Best Answer

Yes, it's normal. I assume you're running the prefork mpm (mpm_prefork_module) in which case, each Apache process loads all of the libraries and modules. Your first step should be to reduce them to the absolute minimum. You then need to work out how many Apache processes you can afford to load and set your mpm limits appropriately.

You might want to consider using the worker mpm (mpm_worker_module) assuming you're not doing so already, but this requires extra work since PHP isn't thread-safe. That usually means you need to end up running PHP as CGI (using something like FastCGI).

I see you're using Debian. Debian comes with two FastCGI modules, those being libapache2-mod-fcgid and libapache2-mod-fastcgi. They are binary compatible, I’m lead to believe, but fcgid is newer and works better with suexec. So you should use libapache2-mod-fcgid unless you know you need libapache2-mod-fastcgi for some specific reason. If you read examples talking about libapache2-mod-fastcgi you can probably just use libapache2-mod-fcgid instead.

Don’t install them both at once – you can do, but there’s no point and it’ll only cause confusion. You only need one.

Basically, Apache + PHP + wordpress is a huge memory hog, and you'll need to actively implement a low memory setup to get it working reliably on 512MB. You should also reduce the memory footprint of mysql as low as possible if you have not done so.

Related Topic