Enabling and disabling PHP’s APC cache on a per Virtual Host level in Apache

Mattias Geniar, Sunday, December 11, 2011 - last modified: Monday, February 20, 2012

On some systems you want APC to work for a few Virtual Hosts, but not all -- as it may break things like phpMyAdmin.

You can do that by disabling the APC "cache_by_default" option globally. In your apc-config file (usually /etc/php.d/apc.ini or somewhere along your general /etc/php.ini file) you disable the default caching.

apc.cache_by_default = 0 

And you can then enable it per Virtual Host you like, either in the general Virtual Host apache config or in the .htaccess file if you allow Overrides in the Apache config.

php_flag apc.cache_by_default On

Notice the difference in the apc.ini config where the value is set to '0' (zero). When passing "flags" to PHP via the Virtual Host or .htaccess config, it's either On (=1) or Off (=0).

Hi! My name is Mattias Geniar. I'm a Support Manager at Nucleus Hosting in Belgium, a general web geek, public speaker and podcaster. Currently working on DNS Spy. Follow me on Twitter as @mattiasgeniar.

I respect your privacy and you won't get spam. Ever.
Just a weekly newsletter about Linux and open source.

SysCast podcast

In the SysCast podcast I talk about Linux & open source projects, interview sysadmins or developers and discuss web-related technologies. A show by and for geeks!

cron.weekly newsletter

A weekly newsletter - delivered every Sunday - for Linux sysadmins and open source users. It helps keeps you informed about open source projects, Linux guides & tutorials and the latest news.

Share this post

Did you like this post? Will you help me share it on social media? Thanks!


Web Hosting Friday, February 10, 2012 at 16:36

It would me so much more convenient if it was possible to disable APC on a virtual host basis. Does anyone know if that’s possible?


    Binoj D Tuesday, December 11, 2012 at 12:32

    I think you could do the opposite of this post.
    apc.cache_by_default = 1

    php_flag apc.cache_by_default Off


Domagoj Sunday, February 19, 2012 at 19:35

Thank you for your post.
I was looking for that..


Stéphan Schamp Thursday, March 7, 2013 at 18:43

Thanks, I used this for a client where some sites don’t play nice with APC.
However, disabling apc via .htaccess doesn’t seem to work with FastCGI.

APC and Zend Optimizer don’t play nice.

If you install Zend Optimizer after APC has been installed,
you might get errors for certain sites.

You can fix this by using the following guide:


This way, you can globally enable APC and disable APC for sites that require Zend Optimzer.


Leave a Reply

Your email address will not be published. Required fields are marked *