If you’re using the latest version of PHP, your site will automatically have OPCache enabled. This is great news for us because we no longer have to install it manually. In this tutorial, I’ll show you how to manage your OP cache and check if it’s working correctly.
What is OPCache?
PHP is an interpreted language. This means that each line of the script is executed one at a time whenever a PHP file is executed, in contrast to compiled languages, where the code is converted into machine language during compilation. This makes PHP slow. OPCache improves the situation by caching the PHP “bytecode” – a sort of intermediate code between source code and machine code. The latter is specific to a given machine, whereas bytecode works everywhere, but still requires one level of interpretation before it becomes machine-readable. However, it’s much faster to execute bytecode rather than the source code itself.
OPCache caches PHP bytecode the first time any PHP page is executed. This means that subsequent requests for the same PHP file will be much faster.
The compiled bytecode is stored in RAM, and that means you have to be careful how much memory you allot to it. By default, OPCache gets 128 MB of memory. But if you have multiple WordPress sites as I do, you should increase that memory so that there’s enough space for all the bytecode scripts. Here’s how.
Download and Load the OPCache Monitor PHP File
Here’s a great single-file PHP script to monitor OPCache and manage it. Installation is dead simple – merely save the code in a PHP file, upload it to your WordPress directory, point your browser at the file and run it. You’ll see something like this:
On the right-hand side, you’ll see the percentage of memory used. If it’s well below 100%, you should be fine – particularly if the “Start time” or “Last reset” fields were some time ago. However, if the memory usage is at or near 100%, then you should increase the OPCache memory so that it has sufficient space to work. Here’s how.
Find and Modify the Right .ini File
In the screenshot above, you’ll see the directory that the OPCache .ini file is using by looking at the “blacklist filename” directive. Open an SSH session and navigate to this folder. Here, you’ll see a .ini file containing the name “opcache”. To edit this file, you’ll need sudo permissions. Once you have the right permissions, open it up and uncomment the following line by removing the hash (#) in front of it:
In the above example, I’ve set the OPcache memory to 256 MB, which is double what it was initially. This is because I have three sites running on my VPS, including WP-Tweaks.com, and each of them uses around 25% of my memory.
Save your changes and exit the file.
Depending on your setup, you should now restart your server and/or PHP. I do both just to be sure. It should take a few seconds and when it’s done, reload the test opcache script you uploaded in the first step. Scroll down and take a look at the “memory consumption” line – it should now reflect the new memory limit you just set.
OPCache Restarts Automatically
OPCache will restart or flush the cache automatically when it has to. Some people think it happens whenever the wasted memory percentage crossed the “max wasted percentage” variable, but this isn’t true. The restart happens whenever the system feels there’s not enough memory. I’m not sure how the wasted percentage factors into the calculation, though.
In short, make sure your OPCache has enough free memory to cache the bytecode of all your PHP files. This will speed up your PHP installation considerably!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!