Enabling and Tuning PHP OPcache for Better Performance
Learn how to enable PHP OPcache with a production‑ready php.ini block, verify it is working, and avoid common pitfalls that erase its performance benefits.
08 Sept 2026, 09:42 UTC

Quick answer
To get immediate performance gains from PHP OPcache, enable it in php.ini with a production‑oriented set of directives, restart the PHP‑FPM (or Apache) process, and verify that the cache is being used.
How OPcache works
OPcache stores the compiled bytecode of PHP scripts in shared memory. When a request arrives, the engine can skip the parsing and compilation steps and execute the cached bytecode directly. This reduces CPU usage and can lower response times, especially for scripts that are executed frequently.
Worked configuration
Place the following block in your php.ini (or in a dedicated opcache.ini file that is included by PHP). Adjust the values to match your server’s memory and file count.
; Enable OPcache
opcache.enable=1
; Memory allocated for cached bytecode (in MB)
opcache.memory_consumption=128
; Memory for interned strings (in MB)
opcache.interned_strings_buffer=8
; Maximum number of PHP files that can be cached
opcache.max_accelerated_files=4000
; How often (in seconds) to check for updated files; 0 means only when the cache is reset
opcache.revalidate_freq=0
; Disable timestamp validation – relies on cache reset instead of stat() calls
opcache.validate_timestamps=0
; Save comments in the bytecode (useful for debugging, can be disabled in pure production)
opcache.save_comments=0
; Enable fast shutdown sequence (PHP‑FPM only)
opcache.fast_shutdown=1
After saving the file, restart the PHP service so the new settings take effect:
- For PHP‑FPM:
sudo systemctl restart php8.2-fpm(adjust the version as needed) - For Apache with mod_php:
sudo systemctl restart apache2
Verifying that OPcache is active
Create a small diagnostic script (e.g., opcache-status.php) and request it through your web server:
<?php
if (function_exists('opcache_get_status')) {
$status = opcache_get_status();
echo '';
print_r([
'enabled' => $status['opcache_enabled'],
'memory_used' => $status['memory_used'],
'memory_free' => $status['memory_free'],
'hit_rate' => $status['hit_rate'],
'cached_scripts'=> $status['num_cached_scripts'],
]);
echo '';
} else {
echo 'OPcache not available';
}
?>
Look for non‑zero values in memory_used and a hit_rate greater than 0%. If the script shows opcache_enabled as false or the values remain zero, double‑check that the php.ini file was read (use phpinfo() and locate the OPcache section) and that the PHP process was restarted.
Limits and considerations
Process‑specific cache
Each PHP‑FPM worker or Apache mod_php process maintains its own OPcache segment unless you explicitly configure shared memory across processes (which is the default for PHP‑FPM). Consequently, the total memory consumption is roughly opcache.memory_consumption × number_of_workers. If you have many workers, you may need to lower the per‑worker memory or increase the total available RAM.
Cache invalidation
With opcache.validate_timestamps=0 and opcache.revalidate_freq=0, OPcache will not check the filesystem for changes. You must manually clear the cache (e.g., opcache_reset() or restarting PHP‑FPM) after deploying new code. Forgetting to do so can serve stale bytecode.
Common mistakes
- Leaving
validate_timestamps=1on a busy site – every request triggers astat()call, negating much of the benefit. - Allocating too little memory – results in frequent cache misses and evictions, visible as a low
hit_rateinopcache_get_status(). - Not restarting PHP‑FPM after changing OPcache settings – the old configuration stays in effect, leading to confusion when
phpinfo()shows new values but the cache behaves as before.
Rolling back the change
If you need to disable OPcache (for example, to troubleshoot an issue), set opcache.enable=0 in the same php.ini file and restart the PHP service. The opcode cache will no longer be used, and memory previously reserved for it will be freed.
Practical checklist
- Add the OPcache block to
php.ini(or a dedicated include). - Restart PHP‑FPM or Apache.
- Request the diagnostic script and confirm
memory_used> 0 and a risinghit_rate. - Monitor
opcache_get_status()over time; adjustmemory_consumptionormax_accelerated_filesif you see frequent cache resets or low hit rates. - After each code deployment, clear the cache (
opcache_reset()) or restart PHP‑FPM when usingvalidate_timestamps=0.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.