Configure and Verify Zend OPcache Bytecode Caching for PHP Applications
Enable OPcache, tune memory and file limits, verify status, and clear stale bytecode after deployment.
25 Oct 2026, 07:47 UTC

Desired Outcome
Enable Zend OPcache so PHP scripts are compiled once and served from shared memory. This reduces parsing and compilation overhead. The configuration must also ensure that deployed code changes are visible without serving stale bytecode.
Prerequisites
- PHP 5.5.0 or newer with the Zend Engine. Most modern distributions include OPcache by default.
- Access to the global
php.inior a per-directory.user.inifile. Root or sudo access is required to edit global settings and restart services. - Enough RAM for the cache. A typical medium application needs 64 MiB to 256 MiB.
- A web server such as Apache or Nginx with PHP-FPM, or a CLI environment you can restart after changes.
Procedure
- Confirm OPcache is available. Run this on the server where PHP executes:
Look forphp -i | grep -i opcacheopcache.enable => Onand the active directive values. - Edit
php.ini(or.user.ini) and set production directives:opcache.enable=1 opcache.memory_consumption=128 opcache.max_accelerated_files=10000 opcache.revalidate_freq=60 opcache.validate_timestamps=0opcache.memory_consumptionsets shared memory in megabytes.opcache.max_accelerated_filessets the maximum number of cached script files.opcache.revalidate_freqsets the timestamp check interval in seconds when timestamp validation is enabled.opcache.validate_timestamps=0disables timestamp checks. Use1in development.
- Apply the configuration. After editing
php.ini, restart PHP-FPM:
Run as root or with sudo. This causes a brief service interruption. If you usedsystemctl restart php-fpm.user.ini, no restart is required. - Create a verification script named
opcache_status.phpin a web-accessible directory:
Access it with a browser or<?php $status = opcache_get_status(); var_dump($status['opcache_enabled']); var_dump($status['cache_full']); var_dump($status['num_cached_keys']); ?>curl. Expectbool(true)for enabled,bool(false)for cache_full, and an integer greater than zero for cached keys. - Verify update behavior. Edit
opcache_status.php, such as adding a comment, then reload the page. Withopcache.validate_timestamps=1, the change appears immediately. With0, the change appears only afteropcache_reset()or a PHP-FPM restart.
Expected Checks
- Run
php -i | grep -i opcacheafter changes to confirm directives match your intent. - Use the verification script to check
opcache_enabled,cache_full, andnum_cached_keysbefore and after traffic. An increase in cached keys means scripts are being cached. - Under load, monitor
opcache_get_status()['cache_full']. If it becomes true, increasememory_consumptionormax_accelerated_files.
Recovery Options
If stale code persists after deployment:
- Run
opcache_reset();from a temporary script or the command line to flush the bytecode cache. - Restart the PHP-FPM pool so workers rebuild the cache from current files.
- Temporarily set
opcache.validate_timestamps=1in development to force immediate reflection of file changes.
These actions affect only the in-memory cache, not source files.
Limitations and Practical Verification
OPcache uses shared memory per SAPI. CLI and PHP-FPM maintain separate caches, so a CLI change does not invalidate a running FPM worker. Check opcache_get_status() in both contexts to confirm.
Memory exhaustion or hitting max_accelerated_files causes cache misses and falls back to normal compilation. Requests do not fail, but latency may rise. Monitor cache_full under realistic traffic.
OPcache does not cache user data. Use APCu or Redis for application-level caching. Bytecode inspectors or debuggers that expect fresh compilation may behave unexpectedly with validate_timestamps=0.
Summary
Enable OPcache, tune memory_consumption and max_accelerated_files, and set validate_timestamps based on environment. Verify with php -i, opcache_get_status(), and a test script. Use opcache_reset() or a service restart when stale code appears.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.