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

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).

4 comments on “Enabling and disabling PHP’s APC cache on a per Virtual Host level in Apache
  1. Web Hosting says:

    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?

  2. Domagoj says:

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

  3. Stéphan Schamp says:

    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 *



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Why ads?

I'm glad you made it to this blogpost. I hope it helps solve your problem. So why then do I show ads on the site? Writing content, testing it and making sure the layout isn't totally b0rked takes time. A lot of time. The ads are a way to pay back a small portion of that time.

And as you know running a site costs (a bit of) money: the domain name, webhosting, time spent writing and updating content, ... So if you like the content of this blog, consider disabling your AdBlocker for this domain. Thanks!

Looking for help?

Tired of fixing all these tech-problems yourself? We've got an excellent team at Nucleus, a top-class Belgian hosting provider, that can help you.

Discover our Managed Hosting, where skilled engineers manage your servers and keep them up-to-date, so you can focus on your core business. We use a variety of Configuration Management Systems such as Puppet to make sure every config is reviewed, unit-tested and guaranteed to be working.

Want to get in touch? Find me as @mattiasgeniar on Twitter or via the contact-page on this blog.