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

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


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

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?


  • 1
    Not a programming question. Should be moved to serverfault, or maybe Wordpress to suggest caching plugins etc.
    – Konerak
    May 8 '12 at 8:56
  • Hmm, 302M does seem a lot. 1000 visits/day isn't a substantial load - that's less than one a minute. Maybe do a web search around 'apache small memory footprint' etc? Also, as you say, try a different MPM. Has it spawned a number of PHP tasks via FastCGI/fcgid?
    – halfer
    May 8 '12 at 9:10

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.

  • I expanded the server to 1GB and that leaves about 50% of the Mem free when in prefork. I changed to the worker mpm, Apache is now at less than 5M(Res) per process, but the php-fcgi-wrapper process is hitting 30M(Res). The server is now using over 90% of the Mem. Before reverting to prefork is there any other tweaking I can do? How can I reduce the size of each PHP process? I've read that using Nginx can help reduce the load from PHP. But I already use W3 Total Cache plugin in Wordpress, Cloudflare, PHP-APC and Database query caching. Will Nginx help with this setup?
    – John Behan
    May 9 '12 at 0:45
  • Technically, we shouldn't have a long conversation in the comments. mpm worker doesn't reduce the memory each PHP process uses, what it does, is allow you to have better control over how many apache processes you need, how many PHP processes you need, reduces the memory footprint of the pure Apache processes that don't need to handle any PHP stuff, and gets each of them to do more. I run a very busy site in 512MB without any issues, but it'll only handle a known number of concurrent connections. May 9 '12 at 8:48
  • Thanks, I'm giving up on this one, going to move the site to a managed server and let the client worry about it. I'll look at learning more about this for my own server. I appreciate all the feedback. I've been putting sites online for a few years now and never had this problem before, it's been an education :(!
    – John Behan
    May 9 '12 at 14:54

Linux generally caches its data much differently then windows, it will take every chance it gets to utilize memory for cache, so from what you top info shows, it looks like 87 mb were being used for cache in the first paste, and then 49 mb in the second, which is completely normal, and when the time comes that a program needs more memory, linux will simply remove some data from the cache and give the free memory to the program, this being allowed to smoothly occur because of the buffer, so unless it gets to the point where the cache is being cut down to a very small amount on a constant basis, I would not stress out too much over it, however from the look of those cgi processes, you may benefit from tweaking your php installation a little, although that is version specific so you would be best off googling for that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.