Apache Low Memory Settings + PHP + APC

Apr 03
2010

In addition to moving my servers to save costs, I ran into a two part issue that I lumped into: “I need to tune memory usage a bit”.

Part 1: Apache

Since I moved my Apache servers to lower memory instances, I was running into swap space usage that I could easily avoid, ie:

free -m
                      total       used       free     shared    buffers     cached
Mem:               268        245         22          0         71         53
-/+ buffers/cache:        120        147
Swap:               511         29        482

Some of the reasoning behind this was that, by default, Apache expects a bit more memory to be available than what I provided to it in the move. The fix was to introduce a few settings to lower child processes and limit concurrent connections to something more reasonable to the type of traffic my site really gets – which is near nothing most days.

The settings I dropped into apache were:

httpd.conf:

    #Low Memory Settings
    StartServers 1
    MinSpareServers 4
    MaxSpareServers 2
    ServerLimit 6
    MaxClients 6
    MaxRequestsPerChild 3000

I made the adjustments, cleared out the swap space with:

swapoff -a
swapon -a

Then restarted apache:

/etc/init.d/apache2 stop
/etc/init.d/apache2 start

And all was well in the world.

free -m
                       total       used       free     shared    buffers     cached
Mem:                268        207         60          0         31         79
-/+ buffers/cache:           97        170
Swap:                 511          0        511

Part 2-1: PHP

A bit simpler, my blog site was running into max memory allocation limits. I had left the default php.ini in place in the upgrade, so I needed to do a once over of configs and change memory_limit from 16M to something more reasonable for my site.

php.ini

memory_limit = 64M      ; Maximum amount of memory a script may consume (16MB)

Part 2-2: APC

Having Apache settings set for lower memory usage also allowed me more room to increase my APC cache limit a bit higher to keep more pages faster. From 30 MB to 50 MB.

apc.ini

extension=apc.so
apc.enabled=1
apc.shm_size=50

Other obvious solutions in consideration, switch to Rackspace to invert my memory/cpu requirement/cost ratios. Any other tips are welcome :-)

3 Responses to “Apache Low Memory Settings + PHP + APC”

  1. patcoll says:

    The best tip may be to use nginx. You can experience reduced memory consumption by using it as a simple reverse proxy to just serve static files. You don't need Apache to load PHP in memory if you're just serving a CSS or image file!

  2. Jailbirt says:

    you can set the same using squid as reverse proxy, apache is a very good well known scalable and stable server, but obviously you can't get it tunned just following a quick howto.

    anyway, thanks for your post.

  3. Jailbirt says:

    you can set the same using squid as reverse proxy, apache is a very good well known scalable and stable server, but obviously you can't get it tunned just following a quick howto.

    anyway, thanks for your post.

Leave a Reply

Visit Other Sites!

Find me on other sites...

Archives

All entries, chronologically...

Pages List

General info about this site...