OPCache is a crucial extension in PHP that improves performance by caching bytecode in memory. It was introduced in PHP 5.5, and since then, it has been enhanced in every subsequent version. PHP 8.x is no exception, and it comes with improved OPCache performance that you can take advantage of to boost the speed of your PHP applications.
In this article, we will discuss how you can optimize OPCache in PHP 8 to improve the performance of your WordPress webistes.
1. Update your PHP version
The first step to optimizing OPCache is to ensure that you are using the latest version of PHP. The newer versions come with improved features and better performance. So, if you’re still running an older version of PHP, consider upgrading to the latest version.
As of the writing of this article, the latest versions of PHP are the following:
- PHP 8.0.28 – February 14th 2023
- PHP 8.1.18 – April 13th 2023
- PHP 8.2.5 – April 13th 2023
You can check which PHP version you are running on your website by simply creating a PHP file with any name with the phpinfo(); function. As an example, let’s assume your website is called example.com, let’s create a file called info.php into your website public folder with the following content:
<?php phpinfo(); ?>
Then save the file and from your browser visit https://example.com/info.php.
You should get a screen like the one below, which will tell you which PHP version is currently being used on your website and all the PHP modules and extensions that are enabled along with their values.
If you are unable to generate such file, try contacting your hosting provider for assistance or drop on the comments below.
2. Increase the OPCache memory limit
By default, OPCache uses 64MB of memory to cache bytecode. However, this might not be enough for large-scale applications. You can increase the memory limit to improve the performance of your application. To do this, you need to update the opcache.memory_consumption value in your PHP configuration file (php.ini).
For example, to increase the memory limit to 512MB, you can add the following line to your php.ini file:
opcache.memory_consumption=512
It is worth to share that not all web hosting providers allow customers to modify the php.ini values. Most of cPanel based hosts will allow customers set custom PHP values via .htaccess or .user.ini files.
Below an example on how to add this setting on .htaccess
php_value opcache.memory_consumption 512
However if you are on an nginx based provider such as Kinsta or simply you do not have root access to the server, you will need to contact their support team for further assistance.
3. Enable OPCache validation
OPCache validation ensures that the cached bytecode is still valid and has not been modified since it was cached. Enabling OPCache validation can improve performance by reducing the number of times the bytecode needs to be recompiled.
To enable OPCache validation, you need to set the opcache.validate_timestamps value to 1 in your php.ini file.
opcache.validate_timestamps=1
Below an example on how to add this setting on .htaccess
php_value opcache.validate_timestamps 1
4. Adjust the OPCache revalidate frequency
The OPCache revalidate frequency determines how often OPCache checks if the cached bytecode is still valid. By default, OPCache revalidates every 2 seconds. However, you can adjust this value to reduce the frequency of revalidation and improve performance.
To adjust the OPCache revalidate frequency, you need to update the opcache.revalidate_freq value in your php.ini file.
opcache.revalidate_freq=60
In the above example, we have set the revalidate frequency to 60 seconds, which means that OPCache will check if the cached bytecode is still valid every 60 seconds.
Below is how to do it via .htaccess
php_value opcache.revalidate_freq 60
This customization is very useful for membership, eCommerce and LMS based sites that use plugins such as Paid Membership Pro, WooCommerce & LearnDash.
You can increase the value higher than 60 seconds, in our experience you could increase this value up to 3600 seconds which is 60 minutes. This will help reduce CPU usage and increase performance.
⚠️ Please test on a development or staging environment before changing this setting on a production environment ⚠️
5. Disable OPCache for development
OPCache can sometimes cache old code and cause issues during development. To avoid this, you can disable OPCache for development environments.
To disable OPCache for development environments, you can add the following code to your PHP configuration file:
if (substr_count($_SERVER['HTTP_HOST'], '.dev') > 0) { ini_set('opcache.enable', 0); }
The above code disables OPCache if the domain name contains “.dev”.
In conclusion, optimizing OPCache is an essential step in improving the performance of your WordPress website. With PHP 8, you can take advantage of improved OPCache performance to boost the speed of your web applications.
By increasing the memory limit, enabling OPCache validation, adjusting the revalidate frequency, and disabling OPCache for development, you can optimize OPCache to get the most out of your WordPress website.