What is OPCache? Using the PHP Op Code Cache to speed up your website

race car intense man with glasses

Have you ever wished your WordPress site could load up to three times faster? Well, it’s possible with the PHP OPcache system! Because WordPress is built with PHP, anything that improves PHP performance will speed up your WordPress website.

Caching systems play a crucial role in improving website performance by reducing the amount of time it takes to load a page. In this blog post, we’ll explain what PHP OPcache is and provide a step-by-step guide on how to install and use it for your WordPress site.

Want more tips on how to speed up your wordpress website? Check out our comprehensive guide, 25 Tips to Speed up WordPress. For more on PHP OPCache, keep reading!

TL;DR: What is PHP OPcache?

PHP OPcache is a caching system that stores precompiled PHP script bytecode in a server’s memory, speeding up web page loading times. Because PHP is normally interpreted at runtime, caching the compiled OPCodes can dramatically speed up PHP Processing.

The Basics of Caching

What is Caching and Why Does It Matter?

Caching is the process of temporarily storing copies of data or content so that it can be accessed more quickly the next time it’s needed. To understand how caching works, imagine having to do a multiplication problem like 6×7. If you’ve memorized your multiplication tables, you can quickly recall that the answer is 42. This is similar to caching—your brain has cached the answer to save time and effort.

There are several types of caching, including browser caching, site caching, object caching, and opcode caching. Each type of caching works differently, but the end goal is the same: to speed up your website by reducing the amount of time it takes to load content.

Opcode Caching: A Deeper Dive

Opcode caching is a specific type of caching that focuses on caching the compiled PHP code, also known as the opcode. As an interpreted language, when a PHP script is executed, it’s first compiled into opcode, which is then executed by the server.

Caching allows this compiling function to be performed once and reuse the resulting opcode, rather than interpret and compile the PHP script every time it is run.

PHP OPcache and Opcode Caching

What is PHP OPcache?

PHP OPcache is a caching system that stores precompiled script bytecode in the server’s memory. This means that when a PHP script is executed, the server can quickly access the cached opcode instead of having to compile the code from scratch again. The result is faster web page loading times and an overall better user experience.

How Does PHP Opcode Caching Work?

To understand how PHP opcode caching works, let’s take a closer look at the process of executing a PHP script. When a user requests a web page, the server first checks if the opcode for that page is already cached in memory. If it is, the server can quickly execute the cached opcode and deliver the web page to the user. If the opcode is not cached, the server must compile the PHP script into opcode and then execute it. This compilation step takes time and resources, which is why caching the opcode can significantly improve website performance.

When comparing PHP OPcache to other bytecode cache engines like APC and Xcache, it’s important to note that OPcache is now included and enabled by default in PHP versions 5.5 and above. This makes it the preferred choice for most users, as it’s both easy to use and highly efficient.

Installing and Using OPcache on Your Server

Installing and Enabling OPcache for PHP 5.5 and Above

If you’re using PHP 5.5 or a newer version, you’re in luck! OPcache is already included and enabled by default. To confirm that it’s enabled, you can check your phpinfo() output. Simply create a new PHP file with the following content:

<?php
phpinfo();
?>

Save the file and access it through your browser. Look for the “Zend OPcache” section in the output. If you see it, that means OPcache is enabled and working on your server.

Manually Installing OPcache for PHP 5.2 to 5.4

If you’re using an older version of PHP (5.2 to 5.4), you’ll need to manually install OPcache. First, check which PHP version you have installed by running the following command from your server’s command line:

php -v

Once you know your PHP version, you can follow these steps to install OPcache:

  1. Download the appropriate OPcache package for your PHP version from the official PHP website.
  2. Extract the downloaded package and follow the provided installation instructions.
  3. Update your php.ini file with the recommended settings for OPcache. These settings may vary depending on your server configuration and PHP version, but a general example is:
zend_extension=/path/to/your/opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

Make sure to replace /path/to/your/opcache.so with the actual path to the OPcache extension on your server.

  1. Restart your web server to apply the changes.

If you’ve followed these steps, OPcache should now be installed and enabled on your server. You can check your phpinfo() output again to confirm that it’s working.

Special Considerations

OPCache with cPanel

If you’re using cPanel to manage your server, the process of installing and enabling OPcache may be slightly different. In most cases, you can enable OPcache through the EasyApache 4 interface by following these steps:

  1. Log in to your cPanel account and navigate to the EasyApache 4 section.
  2. Look for the PHP version you’re using and click on the ‘Customize’ button next to it.
  3. Find the OPcache option in the list of PHP extensions, and toggle it to enable or disable it.
  4. Click on the ‘Review’ button, and then click on the ‘Provision’ button to apply the changes.

After completing these steps, OPcache should be enabled on your cPanel server. You can verify this by checking your phpinfo() output as described earlier in the blog post.

Using OPCache with WordPress

While PHP OPcache works great with WordPress in general, there are a few things to keep in mind:

  1. Make sure your WordPress site is running on PHP 5.5 or a newer version to take full advantage of the performance benefits provided by OPcache.
  2. Some WordPress plugins may not play well with OPcache. If you encounter issues with a specific plugin after enabling OPcache, you may need to disable OPcache for that particular plugin or look for an alternative plugin that’s compatible with OPcache.
  3. You might need to adjust your OPcache settings for optimal performance with WordPress. For example, you can increase the opcache.memory_consumption value to allocate more memory for caching, which can improve the performance of large WordPress sites with many plugins and themes.

OPCache compatibility with IonCube Encoded Scripts

IonCube is a popular PHP encoder that protects PHP scripts by compiling them into bytecode, which can then be executed by the IonCube Loader extension. If you’re using IonCube encoded scripts on your server, you may wonder if there are any compatibility issues with PHP OPcache.

The good news is that IonCube and OPcache can generally coexist without any issues. When you install the IonCube Loader extension, it takes precedence over OPcache for executing IonCube encoded scripts. This means that OPcache will only cache and execute non-encoded PHP scripts, while the IonCube Loader will handle the encoded scripts.

However, it’s essential to ensure that the IonCube Loader extension is loaded before the OPcache extension in your php.ini file. To do this, simply make sure that the zend_extension line for the IonCube Loader appears before the zend_extension line for OPcache in your php.ini file.

Conclusion

Using PHP OPcache for your WordPress site can significantly improve its performance by caching the opcode and reducing the time it takes to load web pages. With the information provided in this blog post, you’re now equipped to install and use OPcache on your server, whether you’re using PHP 5.5 and above or an older version. So why wait? Give OPcache a try and see the difference it can make for your WordPress site’s performance!

By following the guidelines and recommendations provided in this blog post, you’ll be able to successfully install and use PHP OPcache on your server, regardless of whether you’re using cPanel, WordPress, IonCube encoded scripts, or any combination of these technologies. Give OPcache a try, and enjoy the performance boost it can provide for your website!

In conclusion, PHP OPcache is a powerful and efficient caching system that can significantly improve the performance of your WordPress site by caching the opcode and reducing the time it takes to load web pages. With the information provided in this blog post, you’re now equipped to install and use OPcache on your server, whether you’re using PHP 5.5 and above or an older version.