OPcache how it works and how to use it with WP Rocket

You can speed up your WordPress site so it’s around three times faster or more with the OPcache PHP OPcode caching system.

OPcache is a type of caching system that saves precompiled script bytecode in a server’s memory called a cache, so each time a user visits a web page, it loads faster.

Here’s more detail on OPcache and how to install it for your WordPress site to speed it up.

What is Caching?

Caching is a system you can put in place to speed up your site. It works by saving content to your server’s memory the first time it’s loaded on a web page. Each subsequent page load has the stored content retrieved from memory and served on the page.

This process means cached content is displayed a lot faster than if it’s loaded directly from the server.

It’s like memorizing your multiplication tables. Once you have memorized it, it’s so much faster to recite the answer to a multiplication problem from memory rather than trying to calculate the answer all over again.

A cache works in a similar way. Content is stored in a server’s memory so it can be loaded from there quickly instead of going all the way to the server to load the content which takes more time.

The result being a faster loading WordPress website.

There are also different types of caching such as browser, site, object, and OPcode caching. It’s recommended that you  implement more than one kind to increase your site’s performance.

For details, you can also check out these resources:

What is OPcache or PHP Opcode Caching?

OPcache is a type of OPcode caching. This kind of caching compiles human-readable PHP code to code your server understands which is called opcode. This occurs when the PHP file loads on a web page for the first time. Then, it’s saved to the server’s memory for faster loading at each subsequent page visit.

Bytecode cache engines such as OPcache, APC, and Xcache all complete this process the first time the PHP file is executed without having to do it a second, or third time.

How PHP Opcode Caching Works

When a PHP script executes, your server’s cache memory is checked to see if the script has already been cached. If it hasn’t, it’s parsed, which means the code is analyzed.

Then, the script is compiled into opcode making the file readable by the server. Once that’s done, the opcode is saved to the server’s memory.

In other words, it’s stored in your server’s cache. The next time a visitor loads the page with the PHP script, the cached code is executed and loaded much faster.

On the other hand, if the script is loaded on the page and the cache is checked for opcode and finds it, then it’s loaded lickety-split.

How OPcache works
When PHP scripts aren’t in the cache, they’re cached for subsequent page loads.

The Differences Between OPcache and APC Caches

OPcache, APC as well as Xcache are all opcode caching systems. OPcache used to be owned by Zend and Alternative PHP Cache (APC) was a free, open source extension for PHP. Xcache was also an alternative option.

APC was widely used, but it didn’t have the backing that OPcache had so it could be well maintained and stable with each new PHP release.

Fortunately, Zend made OPcache open source and available as an extension since PHP version 5.5. In earlier versions, you have the choice to use APC or OPcache, but if you would like to use the latter, you need to manually install it.

Xcache, on the other hand, is a good alternative to OPcache as a PHP accelerator.

Will OPcache Speed up My WordPress Site?

All three options are suitable for WordPress, but the recommended option for PHP versions 5.5 and above is OPcache. On average, it speeds up WordPress three fold, at the very least for medium to large sites.

If you have a smaller site without many additional PHP scripts or plugins installed, you likely won’t notice much of a difference.

However, you can still install OPcache on even small WordPress sites with no negative effects other than a slight increase in memory usage. But, it won’t be enough to cause any issues. This is similar for medium, large, or enterprise sites.

How to Install OPcache on Your Server

If you have PHP version 5.5 and above, OPcache PHP opcode caching is installed and enabled by default. You don’t have to do anything else. There’s also no additional requirements or configuration needed to run it.

That’s also why you won’t find any options if you were to look for them.

For details, check out Why You Need to Upgrade to PHP 7+ ASAP. (and How to Do It Right Now)

According to the PHP manual:

“OPcache can only be compiled as a shared extension. If you have disabled the building of default extensions with –disable-all, you must compile PHP with the –enable-opcache option for OPcache to be available.
Once compiled, you can use the zend_extension configuration directive to load the OPcache PHP opcode caching  extension into PHP. This can be done with zend_extension=/full/path/to/opcache.so on non-Windows platforms, and zend_extension=C:\path\to\php_opcache.dll on Windows.”

When you have done that, restart PHP using SSH.

On Apache, enter the command below to restart PHP.

For Nginx, enter the following:

How to Install OPcache on Earlier Versions

If your server is running on PHP versions 5.2, 5.3, or 5.4 you can manually install OPcache using the PECL command below:

Next, go to you php.ini file:

You’ll need to update your php.ini file with the following recommended settings:

You can often find your php.ini file among your site’s file folder system. If you’re not sure how to  find it, contact your hosting provider.

Clearing Opcache with WP Rocket

Once you have OPcache or PHP opcode caching installed, you may need to clear the cache. You can do this in one click using WP Rocket.

Once the plugin is installed on your WordPress site, go to Settings > WP Rocket and click the Purge OPcache button. You can find it on the right side of the page.

Purge OPcache with WP Rocket
You can purge OPcache in one click with WP Rocket.

You can also check out What is OPCache?

Wrapping Up

For many WordPress site owners, their server may already have the latest version of PHP installed. This means they already have OPcache automatically enabled to drastically speed up page load times for their site.

For those who have PHP version 5.2 to 5.4, you can manually install OPcache with the steps outlined above.

Do you have the latest PHP version installed? Do you have any questions about OPcache/PHP opcode caching? Let us know in the comments below.

Author's avatar

A copywriter, copy editor, web developer, consultant, course instructor and founder of WP Pros(e), Jenni McKinnon has spent the past 15 years developing websites and almost as long for WordPress. A self-described WordPress nerd, she enjoys watching The Simpsons and names her test sites after references from the show.


Why would I need to activate OPcache if I already use WP Rocket? And if I didn't, why would I need WP Rocket (or any other WP cache plugin) to cache my site when I can just use OPcache?

    Hi Asger :) OPcache and WP Rocket works on two different caching levels, which are compatible. OPCache takes care of object caching, while WP Rocket takes care of page (or static) caching. Using both of them will be beneficial for your site's performance.

How often (or when) should OPCache be cleared?

Hello Anton,
OPCache will clear itself automatically when needed.
The config line "opcache.revalidate_freq=60" specifies that OPCache will check your php files every 60 seconds, (actually it will just check the timestamps so it's a relatively lighweight operation). If it detects that a file has been touched, it will invalidate itself.
As for the maximum memory and files, I'd recommend you to do the following to fine tune your settings :
Run this command in a subfolder of your website, or directly place the file somewhere you can access it

curl https://raw.githubusercontent.com/amnuts/opcache-gui/master/index.php

Go see that index.php file at http(s)://yourwebsite.com/your-subfolder/index.php, you will end up on a nice GUI that will recap for you the current opcache status, memory utilisation.. allowing you to fine tune your settings properly depending on the amount of plugins you have.

Don't forget to remove that file once you're done.

I am a Plus-user with rocket CDN, using the latest WP-Version with PHP 7.4. According to my hoster, OPcache is running on my server (SSD hard drives, Ubuntu 18). Nevertheless, I cannot clear the cache from within the plugin. It keeps telling my "OPcache purge failed."
Do you have an idea what might be the problem here?
Loving WP Rocket and appreciate your reply.

    Hi Stef :) For this kind of questions, please get in touch with our support team. Thank you!

If OPCache is already enabled on the katest PHP versions, why eould one use WP Rocket Opcache again?

    Hi Manny! WP Rocket give you the possibility to reset the OPCache content easily right from your WordPress admin bar, so it's a useful integration :)

Add a comment
Your email address will not be published. All fields are required. Comment policy: We love comments and appreciate the time that readers spend to shader ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

Get a Faster Website in a Few Clicks

Setup Takes 3 Minutes Flat

Get WP Rocket Now What are you waiting for?