keepcalculator

06 Numbers and Powers

Enable PHP OPcache with Nginx and PHP-FPM

Card No. 06·80740 · Updated · Standard formula, shown below

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 -i and systemctl.

Step-by-Step

  1. Check if OPcache is already installed.
    Run php -m | grep opcache from the terminal. If you see Zend 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 (or dnf).
  2. Find the PHP configuration file.
    Run php --ini to see the loaded configuration files. The line that says Loaded Configuration File points to the main php.ini. There’s also a separate opcache.ini in the conf.d directory (e.g., /etc/php/8.2/cli/conf.d/10-opcache.ini).
  3. Enable OPcache in the CLI and FPM configuration.
    Open the opcache.ini file with a text editor (e.g., sudo nano /etc/php/8.2/cli/conf.d/10-opcache.ini). Make sure it contains zend_extension=opcache. If it’s commented out, uncomment it. Note that the CLI and FPM may have separate conf.d directories; check both if you want OPcache active for command-line scripts as well.
  4. Set the key OPcache directives.
    In the same opcache.ini file, add or update these lines:
    opcache.enable=1
    opcache.memory_consumption=128
    opcache.interned_strings_buffer=8
    opcache.max_accelerated_files=10000
    opcache.revalidate_freq=2
    opcache.fast_shutdown=1
    These are sane starting values. The revalidate_freq controls how often (in seconds) OPcache checks for file changes; set it to 0 during development to always see changes immediately.
  5. Restart PHP-FPM.
    Run sudo 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.
  6. Verify OPcache is active.
    Run php -i | grep opcache.enable to confirm it’s On. 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 show opcache.enable as On. Delete that file immediately after checking.
  7. Monitor OPcache usage.
    Use php -r "opcache_get_status();" (or the opcache-status script) 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 restart to be safe.
  • Setting opcache.revalidate_freq too high – If you’re actively deploying code, a high value (like 60) can serve stale files for a minute. Use 2 or 0 in 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=60 or higher to reduce file checks. When you deploy, run sudo systemctl reload php-fpm (or use opcache_reset() in a script) to clear the cache.
  • Use opcache.validate_timestamps=1 during development for convenience. In production, you can set it to 0 to 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.preload to 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

The whole Numbers and Powers drawer →