
OPcache is the part of PHP that keeps the compiled form of your scripts in shared memory, so PHP does not have to read and compile every file again on every request. It should be switched on for every production PHP site. To confirm it is working, open a phpinfo() page and look for the "Zend OPcache" section, where "Opcode Caching" should read "Up and Running". The settings that matter most are the memory size, the number of files it can hold and how often it checks files for changes.
This guide explains each of those in turn, with the checks and commands you need.
What OPcache Does
PHP is not run directly from the text in your .php files. For each request PHP has to read the file from disk, parse it and compile it into low-level instructions called opcodes. Only then does it execute them.
A single WordPress page view loads a large number of PHP files from core, the theme and every active plugin. Without OPcache, all of that reading and compiling is repeated for every visitor, even though the files have not changed.
With OPcache, PHP compiles each file once and stores the opcodes in memory that all PHP worker processes share. The next request skips straight to execution. The PHP manual describes it as removing the need to load and parse scripts on each request.
What OPcache Is Not
The word cache covers several different things on a web server, and they are not interchangeable.
| Cache | What it stores | What it saves |
|---|---|---|
| OPcache | Compiled PHP code | Compiling the same files again |
| Object cache (Redis, Memcached) | Results of database queries | Repeating the same queries |
| Page cache | Finished HTML pages | Running PHP at all for that page |
| Browser and CDN cache | Images, CSS, JavaScript and sometimes pages | Requests to your server |
OPcache never changes what a visitor sees. It only makes the PHP that still has to run start faster, which helps most on pages that cannot be page-cached: carts, checkouts, logged-in dashboards and the WordPress admin. Our guide to WordPress cache types covers the other layers.
How to Check OPcache Is On
Check from the website, not from the command line. The setting opcache.enable_cli is off by default, and command-line PHP keeps a separate cache from the web server anyway. A command-line test tells you little about the live site.
Method 1: phpinfo
Create a temporary file, for example info-check.php, in the site's web root:
<?php phpinfo();
Open it in a browser and search the page for "Zend OPcache". You want to see:
- Opcode Caching: Up and Running
- Cache hits far higher than Cache misses
- Free memory well above zero
- Cached keys comfortably below Max keys
Delete the file as soon as you are done. A phpinfo() page reveals details about the server that should not be public.
Method 2: opcache_get_status
For exact numbers, use a temporary script that calls the status function:
<?php
$status = opcache_get_status(false);
var_dump($status['opcache_enabled']);
var_dump($status['cache_full']);
print_r($status['memory_usage']);
print_r($status['opcache_statistics']);
The function returns false if OPcache did not start or if access is restricted. Otherwise, read these values:
| Value | Healthy | If it is not |
|---|---|---|
opcache_enabled | true | OPcache is switched off in php.ini |
cache_full | false | Raise the memory or the file limit |
free_memory | Clearly above zero | Raise opcache.memory_consumption |
oom_restarts | 0 | The cache ran out of memory and was wiped. Raise the memory |
hash_restarts | 0 | The cache ran out of file slots. Raise opcache.max_accelerated_files |
opcache_hit_rate | High and stable on a busy site | Check for restarts or a full cache |
Some hosting control panels show OPcache status on their PHP settings screen, which saves you from uploading a file.
The Settings That Matter
OPcache has dozens of directives. These are the ones worth your attention. The defaults come from the OPcache configuration reference.
| Directive | Default | What it controls |
|---|---|---|
opcache.enable | 1 | Turns the cache on for web requests |
opcache.memory_consumption | 128 | Shared memory for compiled code, in megabytes |
opcache.interned_strings_buffer | 8 | Memory for repeated strings such as class and function names, in megabytes |
opcache.max_accelerated_files | 10000 | How many scripts the cache can index |
opcache.validate_timestamps | 1 | Whether PHP checks files for changes |
opcache.revalidate_freq | 2 | How many seconds to wait between those checks |
opcache.jit | disable | The just-in-time compiler, a separate feature |
Memory
The default of 128 MB is enough for one ordinary site. A server that hosts many sites under one PHP service, or a large application, can fill it. When the cache is full, new files are simply not cached, and performance drops without any error message. Check free_memory and oom_restarts before you change anything, then raise the value in steps.
Number of files
Count the PHP files the server has to cache:
find /var/www -type f -name "*.php" | wc -l
Set opcache.max_accelerated_files above that number. PHP rounds your value up to the next prime in a fixed list, so the default of 10000 really gives 16229 slots. The allowed range is 200 to 1000000.
Checking files for changes
With opcache.validate_timestamps=1, PHP looks at each file's modified time at most once every opcache.revalidate_freq seconds and recompiles the file if it has changed. The default interval is 2 seconds. The PHP manual's recommended settings use 60.
With opcache.validate_timestamps=0, PHP never checks. That is slightly faster, but changed files are ignored until the cache is reset. Use it only when code changes arrive through a deployment process that resets the cache every time.
For WordPress sites where people update plugins from the dashboard, keep validation on. Since version 5.5, WordPress also asks OPcache to drop the files it replaces during an update.
JIT and preloading
The JIT compiler is configured through OPcache but is a different feature. It has been disabled by default since PHP 8.4. Leave it off unless you have measured a benefit for your own application.
Preloading, set with opcache.preload, loads chosen files into memory when PHP starts. It applies to the whole PHP process and needs a restart to change, so it suits a server dedicated to one application, not shared hosting.
A Starting Configuration
For a busy WordPress or WooCommerce site on its own server, this is a reasonable place to start. Treat the numbers as a first guess, then adjust them using the status values above.
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.validate_timestamps=1
opcache.revalidate_freq=60
Put the lines in php.ini or in the OPcache file inside your PHP configuration directory. The phpinfo() page shows which files are loaded. Then restart PHP so the new values take effect. On a Debian or Ubuntu server running PHP-FPM that looks like this, with the version number changed to match yours:
sudo systemctl restart php8.4-fpm
On shared hosting you usually cannot edit these values yourself. Ask the host what they are set to.
How to Clear OPcache
If you have edited a file and the site still runs the old code, the cache is holding the previous version. You have three ways to clear it:
- Restart PHP-FPM or the web server. This always works.
- Call
opcache_reset()from a web request. Every script is recompiled the next time it is used. Running the function from the command line does not clear the web server's cache. - Use your host's tools. Some hosts provide a button in the control panel that resets OPcache for you.
This is separate from clearing a page cache. If a content change is not showing, the page cache or CDN is the more likely cause. See how to clear the WordPress cache.
What Changed in PHP 8.5
Before PHP 8.5, OPcache was a separate extension that had to be loaded with a zend_extension=opcache line. As of PHP 8.5.0 it is always built into PHP and always loaded.
Two practical consequences follow:
- When you move a server to 8.5 or later, remove the old
zend_extension=opcache.soline. It now produces a warning. opcache.enablestill decides whether the cache is active, so the checks in this guide still apply.
Notes for Hosting Companies
- Size the memory for each PHP service, not for each site. Every site served by the same PHP-FPM service shares one cache.
- Watch
oom_restartsandhash_restartsin your monitoring. A rising count means customers are losing their cache again and again. - Consider
opcache.restrict_api. It limits the OPcache functions to scripts under a path you choose, so one customer's script cannot reset the cache for everyone. - Leave
opcache.save_commentsat 1. Some frameworks and tools read annotations from code comments and break without them.
OPcache is one of the cheapest performance gains available on a PHP server, and it is often left at defaults that no longer match the workload. Ten minutes with the status output will tell you whether yours is helping. For more on the PHP side of hosting, see our PHP articles and the guide to updating your PHP version.
