How to Limit APC Caching to Specific Virtual Sites

in Linux

When you host a good number of virtual sites on your VPS it’s not a good idea to use the default settings of PHP APC because unless you have a large amount of ram to spare, then chances are APC will gobble all of it. And when PHP APC runs out of memory to use, it could lead to other problems like load spikes and cache slams.

Here’s the quick hack I did to enable PHP APC on only one virtual site.

nano /etc/php.d/apc.ini

Inside the ini file locate the apc.filter variable and set it to the following:

apc.filter = ".*,+/home/user/public_html/.*"

Save apc.ini and restart php-fpm

service php-fpm restart

What the setting did was it enabled the filter which skips all php files (.*) that apc is supposed to cache. Then by defining (+/home/user/public_html/.*) apc will cache all php files under the directory.

By doing it this way you can enable PHP APC to a select sites that you need cached. As far as my experience goes, each WordPress site uses about ~20 MB of APC cache memory (will likely vary depending on your WordPress site, plugins installed etc.) so if you use the default apc.shm_size which is 32 MB, you can safely cache at least one WordPress site.

The reason I don’t recommend going for two sites based on the figures above is because you need to have a good amount of spare cache memory to prevent fragmentation. I usually go for 10% to 20% of free memory, the more, the better.

Some things you should know.

web-server: nginx 1.2.7
php: php-fpm 5.4.7
apc: php apc 3.1.13
db: mysql 5.5.28


Leave a Comment

Previous post:

Next post: