OPcache stores precompiled PHP scripts in shared memory, so PHP-FPM skips the parsing and compilation step on every request. This can cut PHP response times by 20–50% on CPU-bound workloads. You’ll edit the PHP configuration file that your FPM pool uses, enable the extension, tune a few directives, and then verify it’s active.
What You’ll Need
- Root or sudo access to your server.
- An installed and running PHP-FPM service (e.g.,
php8.2-fpm). - Nginx configured to proxy PHP requests to FPM (usually via
fastcgi_pass). - Command-line access to run
php -iandsystemctl.
Step-by-Step
- Check if OPcache is already installed.
Runphp -m | grep opcachefrom the terminal. If you seeZend OPcache, it’s installed but may be disabled. If nothing shows, install it with your package manager. On Ubuntu/Debian:sudo apt install php-opcache. On CentOS/RHEL:sudo yum install php-opcache(ordnf). - Find the PHP configuration file.
Runphp --inito see the loaded configuration files. The line that saysLoaded Configuration Filepoints to the mainphp.ini. There’s also a separateopcache.iniin theconf.ddirectory (e.g.,/etc/php/8.2/cli/conf.d/10-opcache.ini). - Enable OPcache in the CLI and FPM configuration.
Open theopcache.inifile with a text editor (e.g.,sudo nano /etc/php/8.2/cli/conf.d/10-opcache.ini). Make sure it containszend_extension=opcache. If it’s commented out, uncomment it. Note that the CLI and FPM may have separateconf.ddirectories; check both if you want OPcache active for command-line scripts as well. - Set the key OPcache directives.
In the sameopcache.inifile, add or update these lines:opcache.enable=1opcache.memory_consumption=128opcache.interned_strings_buffer=8opcache.max_accelerated_files=10000opcache.revalidate_freq=2opcache.fast_shutdown=1
These are sane starting values. Therevalidate_freqcontrols how often (in seconds) OPcache checks for file changes; set it to0during development to always see changes immediately. - Restart PHP-FPM.
Runsudo systemctl restart php8.2-fpm(adjust the version to match your setup). If you changed the CLI configuration, there’s no need to restart anything for CLI scripts—they’ll pick it up on the next run. - Verify OPcache is active.
Runphp -i | grep opcache.enableto confirm it’sOn. For the web server, create a temporary PHP file in your web root with<?php phpinfo(); ?>, open it in your browser, and search for “OPcache”. The “Zend OPcache” section should showopcache.enableasOn. Delete that file immediately after checking. - Monitor OPcache usage.
Usephp -r "opcache_get_status();"(or theopcache-statusscript) to see hit rate and memory usage. A healthy hit rate is above 95%.
Common Mistakes
- Editing the wrong php.ini – The CLI and FPM often have separate configuration files. Ensure you’re editing the one used by FPM, usually under
/etc/php/<version>/fpm/. - Forgetting to restart FPM – OPcache settings are read at FPM startup. A simple reload may not pick up changes; use
restartto be safe. - Setting
opcache.revalidate_freqtoo high – If you’re actively deploying code, a high value (like 60) can serve stale files for a minute. Use2or0in development. - Ignoring
opcache.max_accelerated_files– If your project has more PHP files than this limit, the extra files won’t be cached. Check the “Cached scripts” count in phpinfo and adjust accordingly. - Not enabling
opcache.fast_shutdown– This improves cleanup speed; leave it on unless you hit a specific bug.
Tips
- For production, set
opcache.revalidate_freq=60or higher to reduce file checks. When you deploy, runsudo systemctl reload php-fpm(or useopcache_reset()in a script) to clear the cache. - Use
opcache.validate_timestamps=1during development for convenience. In production, you can set it to0to completely disable timestamp checks, but then you must manually clear the cache on every deploy. - Monitor memory usage with
opcache_get_status(). If “memory_used” is near “memory_consumption”, increase the value. - If you use a Composer-based framework, consider using
opcache.preloadto preload common classes. This is advanced but can yield significant performance gains.
Troubleshooting
OPcache doesn’t show up in phpinfo()
Check that the extension is actually loaded. Run php -m | grep opcache. If it’s not there, ensure the zend_extension line is uncommented and points to the correct path (usually opcache.so). Also verify that the FPM configuration file is being read—run php-fpm -i | grep opcache as root to see the FPM-specific settings.
Changes to PHP files aren’t reflected
Increase opcache.revalidate_freq to 0 and restart FPM. If that fixes it, your previous setting was too high. Alternatively, call opcache_reset() via a script or run sudo systemctl reload php-fpm.
OPcache is enabled but hit rate is low
Low hit rate often means the cache is being flushed frequently (e.g., due to low memory or low max_accelerated_files). Check the “OOM” and “num_misses” counters in opcache_get_status(). Increase memory or file limits accordingly.
FPM fails to start after enabling OPcache
Check the FPM error log (/var/log/php-fpm.log or journalctl -u php-fpm). A common cause is a missing extension file or a syntax error in the configuration. Run php-fpm -t to test the configuration.
OPcache works in CLI but not in FPM
This almost always means the FPM configuration file is different. Locate the FPM-specific opcache.ini (e.g., /etc/php/8.2/fpm/conf.d/10-opcache.ini) and edit that one.
Frequently Asked Questions
Q: Does OPcache work with PHP 5.6?
Yes, OPcache is bundled with PHP 5.5 and later, but it’s not enabled by default. You can install it via php-opcache package. However, PHP 5.6 is end-of-life; upgrade to a supported version.
Q: Can I use OPcache with Apache instead of Nginx?
Absolutely. OPcache is independent of the web server. The steps are the same; just restart Apache instead of FPM.
Q: How do I clear the OPcache without restarting FPM?
You can call opcache_reset() in a PHP script or use the opcache_reset function from the CLI: php -r "opcache_reset();". If you have the Zend OPcache extension loaded in CLI, this will clear the cache for the CLI SAPI, not the web server. To clear the web server cache, you need a script accessible via HTTP or restart/reload FPM.
Q: What is a good opcache.revalidate_freq for production?
For a stable production environment, set it to 60 or 300 seconds. This reduces file checks. When you deploy, reload FPM to clear the cache.
Q: Does OPcache help with WordPress?
Yes, WordPress benefits significantly. Many managed hosts enable OPcache by default. It reduces the overhead of parsing the large number of PHP files WordPress loads on each request.
Q: Is OPcache compatible with PHP 8.0+?
Yes, OPcache is included and enabled by default in PHP 8.0 and later, but you still need to set opcache.enable=1 in production. The configuration steps are the same as for PHP 7.x.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Skip Ryobi and Milwaukee? Harbor Freight Tools to Buy
- Funny Subreddits That Make You Laugh
- Why Retro Gaming Is Still Worth Playing Today
- Neptune Facts That Will Blow Your Mind
- Survival Crafting Game Fatigue: Why & How to Fix It
- Sell Your EV Now Before Prices Plummet?