What is OPcache and How Do You Use It?

Table of Contents

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

When PHP scripts aren’t in the cache, they’re cached for subsequent page loads.
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 version 5.5 and above is OPcache. On average, it speeds up WordPress threefold, 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 are also no additional requirements or configurations 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.

<script src="https://gist.github.com/jennimckinnon/a0a6d996d5d553aff58087cffc6c2f2b.js"></script>

For Nginx, enter the following:

<script src="https://gist.github.com/jennimckinnon/ee58de4f502c3540e3f5cb81f60db64d.js"></script>

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:

<script src="https://gist.github.com/jennimckinnon/f80d2fe73c1c1e0be5712e9bb8fe3fd1.js"></script>

Next, go to you php.ini file:

<script src="https://gist.github.com/jennimckinnon/b4cbe5d0f924293fa29240c8ddf52517.js"></script>

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

<script src="https://gist.github.com/jennimckinnon/a08541df21fa0a275b1306ec67b6f31c.js"></script>

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

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.

Subscribe to Our Newsletter

Stay in the loop with the latest WordPress and web performance updates.
Straight to your inbox every two weeks.

Get a Faster Website
in a Few Clicks

Setup Takes 3 Minutes Flat
Get WP Rocket Now