0

I have recently bought a VPS server with the below configuration

  • Centos 6 64bit
  • 20GB SSD
  • 512MB RAM
  • 512 VSwap

I have hosted around 10 websites. In that 4 of them is WordPress. Others are dynamic(PHP) websites. I have a very few visitors. But when I checked the RAM I found that Apache is most of the ram. When apache is running, ram usage is around 97%. While stopping apache, it becomes 30%.

How can I configure apache so that it uses less ram?

enter image description here

I heard that converting InnoDB to MyISAM can solve ram usage. But my MySQL is only using less ram

  • 1
    Your server is too small for your workload. – user9517 Mar 1 '16 at 16:31
  • but no visitors. Just php files and mysql. How can they consume this much of memory even without running – Gijo Varghese Mar 1 '16 at 16:33
  • 1
    Disable all the modules that are not used by php and apache. Apache modules consume memory. Also tune your Apache Prefork Module configurations. – Siddharth sharma Mar 8 '16 at 6:57
  • When i listed apache modules, I got a long list. I'm confused on which one to disable and which one to enable. – Gijo Varghese Mar 8 '16 at 14:35
  • For this you have to read Apache Doc, we actually don't know how you are using your apache server. All module description are available in docs. Only select what you need and disable rest. This will surely improve your performance. I suggest you to remove all the proxy,optional,dav and CGI modules. You can find all the Loaded modules in /etc/httpd/conf.modules.d/ directory. – Siddharth sharma Mar 12 '16 at 12:04
1

In my case, I had Apache with mod_php running on a Centos 64bit VM with 2GB RAM. When I initially installed Apache 2.4, everything worked well and the parent Apache process (owned by root) would consume around 10 MB of memory.

Later on, when I checked top, the parent and all child processes were consuming 28% of server memory (around 500 MB each). After fiddling around with many settings and mpm forks and max servers and what not, I finally did yum remove php and restarted Apache. Memory consumption went back to normal 10 MB. This lead me to believe it was a PHP setting causing the problem. So I copied over php.ini from another server and reinstalled PHP (using yum install php) and again everything worked fine. Comparing the two php.ini files, I found that I had earlier linked browscap in php.ini and it was a 60 MB file which would load in memory during startup.

So do check, you may have something similar.

0

The super rough formula for apache/php memory footprint is: (number of worker processes) x (max memory per worker process). It doesn't actually matter if they're apache child processes running mod_php or php-fpm processes.

For instance if you're running regular old apache pre-fork mpm with the default MaxClients setting (256) and your php.ini's memory_limit setting is 128MB then your overall apache memory footprint can get as high as 32GB!

Now if your server is basically idle with no traffic it won't get there, but it really only takes one request to one wordpress (or drupal or whatever) page for one worker process to allocate most of that 128MB. When you factor in that browsers make multiple requests in parallel, you could easily see a simple setup shooting up to 1GB on just a few pageviews.

In the real world its usually not quite that bad because even though each of your worker processes will try to allocate that memory, the kernel is smart enough to realize that 80% of what they're loading is the same stuff so they can just think they have their own copy of wordpress in memory when really they're sharing one.

Therefore, to answer your question of how to get apache to use less ram, simply lower one or both of those numbers until you're happy with the memory footprint (and of course your sites still work). I personally like to set my MaxClients to 4x the number of cpu cores available on the machine.

I do feel the need to point out though that you should consider the fact that your best option is likely to do nothing. Many people bring a falsely-economizing mentality to memory consumption thinking that if its 90% in use that represents some kind of waste or problem. Really its the opposite! If the purpose of this server is to respond to http requests with the results of dynamic php responses, then it should be using 90% of the memory to do that. To leave the memory empty/available would be to waste it. The trick is to tune things so that you use most of your memory, but leave just enough to not cause swapping.

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.