OpCache and PHP 8

by May 2, 2023OPCache, Performance, PHP0 comments

Kinsta - Unlock 4 Months OFF Annual WordPress Plans

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:

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.

Kinsta - Unlock 4 Months OFF Annual WordPress Plans
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
TTFB and how it impacts your website

TTFB and how it impacts your website

In the world of website performance, TTFB or Time to First Byte is an essential metric that can impact the overall performance of your WordPress website. In this article, we will discuss: what TTFB is, why it is crucial for website performance, how to check your current TTFB value, and how WP Mechanics can help you improve this metric.

read more
PageSpeed Scores and why they matter

PageSpeed Scores and why they matter

In today's digital age, website speed has become a crucial factor in user experience and search engine optimization. Slow-loading websites can lead to frustrated users and lower search engine rankings. In this article, we will discuss the importance of having a good...

read more
Next Gen Images webp & avif, what are they?

Next Gen Images webp & avif, what are they?

Images are an essential part of any website, but they can also slow down page load times, negatively impacting user experience and SEO. One solution to this problem is to use next-gen image formats, which are designed to be smaller and more efficient than traditional image formats like JPEG and PNG.

In this article, we will discuss next-gen image formats and how to easily implement them on your WordPress website.

read more
Improve your website speed using REDIS

Improve your website speed using REDIS

If you have a WordPress website that uses LearnDash, WooCommerce, or PaidMembershipsPro, you may have noticed that your website can be slow and unresponsive at times. This can be frustrating for your users and can even lead to lost sales or membership renewals. The good news is that you can improve the performance of your WordPress website by using Redis.

read more