Home » Add batcache to your WordPress site (nginx + APC + php-fpm + batcache)

Add batcache to your WordPress site (nginx + APC + php-fpm + batcache)

This is a continuation of my other blog post(s) – Install nginx, php-fpm, APC, MariaDB, and phpMyAdmin on CentOS 6 and How to install a web server on CentOS 7 using nginx, php-fpm, MariaDB, and firewalld. This is just a simple post about installing Batcache (the best caching plugin for WordPress IMHO) and fixing it to output the comment block like it’s supposed to. This is specifically for the above setup, so I’m going to be using APC object cache instead of memcached or any other caching system. If you aren’t going to be using APC, then you can skip the first step (just be sure to install another caching system first like memcached).

First let’s install APC object cache:

CentOS 7:

CentOS 6:

Then we need to install the APC plugin. Go to Plugins > Add New and add the APC Object Cache Backend plugin. After you install it, you’ll notice the message that tells you it’s not an actual plugin, but rather just a drop-in for using APC for your caching system. Then you’ll have to copy /wp-content/plugins/apc/object-cache.php to /wp-content/object-cache.php. After this is done, you really don’t need this plugin anymore unless you want to keep an eye on updates that Mark Jaquith pushes, but they’re rare.

Now that we’ve got the APC object cache drop-in in place we can install Batcache. Go to Plugins > Add New and add the Batcache plugin. Once that’s installed, you’ll need to copy /wp-content/plugins/advanced-cache.php to /wp-content/advanced-cache.php.

Then all we have to do is turn on caching within WordPress. Edit your wp-config.php file and add the following line somewhere in there:

You are now technically running batcache through APC object cache. However, if you’re like me and go crazy over magical caching systems like this and want to confirm that it’s working, you’ll need to edit the batcache file to fix a bug they have that makes their “generated in 0.984 seconds” message not appear in the source. This is on line 288 of the /wp-content/advanced-cache.php file…

Replace this:

With this:

Once that’s replaced, you should be able to refresh your page a couple of times and see a message similar to this in your page source just before the </head> tag:

I generally load my site in an incognito/private window just to get the regular browsing experience your non-logged-in users will see. Batcache does not deliver its cached pages to logged in users, so if your site is largely member-based with mostly active user sessions, it may not be the best solution for you. If your site is 50% or more of traffic for non-logged-in users, it’s unarguably the best caching solution for WordPress.

If you have any questions, feel free to post a comment 🙂


11 comments on “Add batcache to your WordPress site (nginx + APC + php-fpm + batcache)

  1. Tang Rufus on

    I wrote about Batcache on my blog too! However, I am confused about how Batcache handles HTTPS requests. Would you share how to config Batcache over HTTPS?

    • Jeff Hays on

      Hey Tang,

      The general rule of Batcache is that it will cache the majority of things for users that aren’t logged in. When you’re needing a caching solution for sites that have open registration, a large active user base (like a site with a bulletin board system), or any site where the majority of your user base are logged in users, caching can be tricky.

      Generally speaking, Batcache doesn’t treat SSL pages any differently in terms of how it’s configured or what it caches. It will continue to cache content in the same way for HTTPS pages. It will not deliver cached pages to logged in users by default.

      If you use transients, wp_cache_set(), and wp_cache_get() functions correctly, you can take advantage of WordPress caching no matter what caching solution you use. Let me know if you have any other questions. Thanks for posting 🙂

  2. Tang Rufus on

    I dropped batcache and focus on server tuning (gzip, spdy, ngx_pagespeed, OCSP Stapling, etc)
    Moved to PHP’s built-in opCache. No WordPress caching plugin so far. Still looking for a lightweight one for opCache.

    Thanks for the explanation, Jeff

  3. Edward Liu on

    Hey, I’ve set up batcache properly (put object-cache.php and advanced-cache.php into wp-content), but somehow the generated in n seconds just won’t show up!

    • Jeff Hays on

      Hey Edward,

      Usually this happens because you’re logged in and batcache doesn’t actually cache anything for logged in sessions. Open up an incognito window in your browser and refresh the page twice and you should see the message. As long as you edited their plugin on line 288 with the bug fix I outlined above, then I’m sure this is probably your issue. Let me know if this doesn’t work for you.

  4. Edward Liu on

    Hey! thanks for your response. I think it took awhile for batcache to kick in, and it did. However, when it did kick in, the page loads became pretty strange (compared to w3tc and wp super cache). What is the way to optimize it? I tried both APC and memcached object-cache.php, but they end up destroyed my servers due to way too many php5-fpm processes.

  5. Danie VIn on

    Why did you use APC for backend caching not memcached ?
    APC is only good for frontend caching . Not for storing DB query cache.

    • Jeff Hays on

      Hi Danie,

      This post is simply a tutorial for setting up this particular stack and isn’t necessarily my recommendation for which solution to use in terms of caching. For my enterprise level sites that have a lot of traffic and require the cache to be distributed across several servers I have used memcached and varnish and such as well.

      There are different reasons to use different solutions and you’ll need to determine what works best for your site/server based on traffic, system resources, network overhead, etc. Despite the mention of Drupal in this article, this is still a good read in terms of actual use cases for various caching solutions: http://www.midwesternmac.com/blogs/jeff-geerling/apc-caching-dramatically


Leave a Reply

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