How To Install The Latest APC 3.1.9 On Ubuntu 10.04 LTS & W3 Total Cache Optimization

Yah another geeky post – woohoo! Anyway, when you are running high traffic WordPress sites – it’s very common to use W3 Total Cache (known as w3tc usually).

And with this you need some kind of in memory cache to store objects/db queries etc – the choice for a single server setup is generally APC as it’s part of the PHP core now (used to be eAccelerator/Xcache were contenders too).

Anyway never use APC for page caching – use disk enhanced and make sure your rewrites are working properly – your site will fly. For Object Cache and Database Cache – you should use APC (if you have a multi-machine cluster then use memcached).

The problem you might face (especially on Ubuntu 10.04) is that it uses a fairly old version of PHP and thus APC (3.1.3). I was facing horrible fragmentation with this setup and it will effect server performance over time. I could see from my monitoring that it was reaching over 20% fragmentation, this is not super bad – but over 10% is enough to worry for me as I’m a performance freak.

Munin php-apc graphing

I tried various different tweaks (ttl 0, ttl 7200, ttl 3200) but it wouldn’t go down. I eventually figured out I might try updating apc, rather than sticking with the one installed using:

aptitude install php-apc.

Assuming you already have php-apc installed and configured, this single command will remove it, and install the latest version of APC.

sudo aptitude remove php-apc; sudo aptitude install php-pear libpcre3 libpcre3-dev; sudo pecl install apc

Then all you need to do is:

sudo /etc/init.d/apache2 restart

Or for nginx/php-fpm (my preference now):

sudo /etc/init.d/nginx stop; sudo /etc/init.d/php5-fpm stop; sudo /etc/init.d/nginx start; sudo /etc/init.d/php5-fpm start

And that’s it, now you’ll have the latest version of APC running and you should see a LOT less fragmentation (mine is hovering about 4% now instead of 20%).

php-apc fragmentation

I’ll keep an eye on it, perhaps change the apc.ini settings a little more (usually found in /etc/php5/conf.d/apc.ini) – for reference if you want those, here they are 🙂

apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 128M
apc.optimization = 0
apc.num_files_hint = 2048
apc.user_entries_hint = 2048
apc.ttl = 7200
apc.user_ttl = 3600
apc.gc_ttl = 600
apc.cache_by_default = 1
apc.filters = "-/home/username/domains/\.php$"
apc.mmap_file_mask = "/tmp/apc.XXXXXX"
apc.slam_defense = 0
apc.file_update_protection = 2
apc.user_request_time = 1
apc.enable_cli = 0
apc.max_file_size = 2M
apc.stat = 1
apc.write_lock = 1
apc.report_autofilter = 0
apc.include_once_override = 0
apc.rfc1867 = 0
apc.rfc1867_prefix = "upload_"
apc.rfc1867_name = "APC_UPLOAD_PROGRESS"
apc.rfc1867_freq = 0
apc.localcache = 1
apc.localcache.size = 1024
apc.coredump_unmap = 0
apc.stat_ctime = 0





You can subscribe via e-mail to get my posts in your Inbox, or stalk me on numerous other platforms.

Follow my Dayre!

, , , , , , , , , , , , , ,

2 Responses to How To Install The Latest APC 3.1.9 On Ubuntu 10.04 LTS & W3 Total Cache Optimization

  1. Paul Tan March 29, 2012 at 6:32 pm #

    I’m using WP Super Cache, but the supercache files I put them on a /dev/shm volume so they’re in RAM. Got plenty of RAM on my PHP box… 64GB! is WP Total Cache better?

  2. ShaolinTiger March 29, 2012 at 6:35 pm #

    Paul Tan: Yah miles better, Super Cache was ok a few years but W3 Total Cache is WAY more advanced (support for APC/Memcached/automatic handling of CDN/Varnish purging etc). Combine it with nginx + php-fpm and it flies.