Adjusting PHP OPCache for WordPress

Content Error or Suggest an Edit

Notice a grammatical error or technical inaccuracy? Let us know; we will give you credit!

Draft Warning

You’ve reached a draft 🤷‍♂️ and unfortunately, it’s a work in progress.

Introduction

The following page will discuss adjusting the PHP OPCache for WordPress

WordPress Specific PHP OPCache Configuration

There isn’t a silver bullet configuration for WordPress, PHP OPCache configuration is unique to each workload. But the following will provide you with a general guide of what you should change.

opcache.validate_timestamps (Default 1)

If enabled, OPcache will check for updated scripts every opcache.revalidate_freq seconds. When this directive is disabled, you must reset OPcache manually via opcache_reset()opcache_invalidate() or by restarting the Web server for changes to the filesystem to take effect.

https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.validate-timestamps

The opcache has to be validated to ensure that the most recent code is within the opcache; by default, this is enabled, and a file’s timestamp is checked based on opcache.revalidate_freq.

onopcache.revalidate_freq (Default 2)

How often to check script timestamps for updates, in seconds. 0 will result in OPcache checking for updates on every request.

This configuration directive is ignored if opcache.validate_timestamps is disabled.

https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.revalidate-freq

Validating timestamps adds overhead, if you’re a developer revalidating frequently helps when you’re changing code frequently and need to see results instantly.

However, in production, if there aren’t many code changes, you can set this to 5 or 10. Understanding that any code changes will not show for up to 5-10 seconds if not more.

opcache.memory_consumption (Default 128)

The size of the shared memory storage used by OPcache, in megabytes. The minimum permissible value is "8", which is enforced if a smaller value is set.

https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.memory-consumption

Increasing the opcache.memory_consumption comes in handy to allow a more significant amount of opcode to be stored in the opcache, reducing cache misses and discards.

Depending on your system, you can increase this to 256 or 512 based on your hosting instance memory.

opcache.interned_strings_buffer (Default: 8)

The amount of memory used to store interned strings, in megabytes.

https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.interned-strings-buffer

Needs more explanation

opcache.max_accelerated_files (Default: 10000)

The maximum number of keys (and therefore scripts) in the OPcache hash table. The actual value used will be the first number in the set of prime numbers { 223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987, 262237, 524521, 1048793 } that is greater than or equal to the configured value. The minimum value is 200. The maximum value is 1000000. Values outside of this range are clamped to the permissible range.

https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.max-accelerated-files

The number of files that will be “accelerated” and added to the opcache. If you want to know what a good size for opcache.max_accelerated_files would be. You can simple count all the files in your WordPress instance public web directory. You can do this easily from a linux shell.

$ find . -iname "*.php" | wc -l

opcache.use_cwd (Default: 1)

If enabled, OPcache appends the current working directory to the script key, thereby eliminating possible collisions between files with the same base name. Disabling this directive improves performance, but may break existing applications.

https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.use-cwd

opcache.file_update_protection

More details.

opcache.fast_shutdown

More details.

opcache.save_comments

More details.

opcache.enable_cli

Enables the opcode cache for the CLI version of PHP.

https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.enable-cli

General Information

Finding the Right Settings for PHP OpCache

Tideways explains this best

  • If cache_full is true and a restart is neither pending nor in progress, that probably means that the waste is not high enough to exceed the max waste percentage.You can check by comparing current_wasted_percentage with the INI variable opcache.max_wasted_percentage. In this case also the cache hit rate opcache_hit_rate will drop below >=99%.Solution: Increase opcache.memory_consumption setting.
  • If cache_full is true and num_cached_keys equals max_cached_keys then you have too many files. When there is not enough waste, no restart will be triggered. As a result there are scripts that don’t get cached, even though there might be memory available.Solution: Increase opcache.max_accelerated_files setting.
  • If your cache is never full, but you are still seeing a lot of restarts, that can happen when you have too much waste or configured the max waste percentage too low.Solution: Increase opcache.max_waste_percentage setting.
  • To look for inefficient restart behavior you can evaluate the oom_restarts (related to opcache.memory_consumption setting) and hash_restarts (related to to opcache.max_accelerated_files). Make sure to check when the last restart happened with last_restart_time statistic.
  • To find a good value for opcache.max_accelerated_files you can use this Bash one-liner to get the number of PHP files in your project:
  • $ find project/ -iname *.php|wc -l

To look for inefficient restart behavior you can evaluate the oom_restarts (related to opcache.memory_consumption setting) and hash_restarts (related to to opcache.max_accelerated_files). Make sure to check when the last restart happened with last_restart_time statistic.

To find a good value for opcache.max_accelerated_files you can use this Bash one-liner to get the number of PHP files in your project:

$ find project/ -iname *.php|wc -l 9607

Make sure to account for code-generated and cache files or multiple applications running inside the same FPM worker pool.

https://tideways.com/profiler/blog/fine-tune-your-opcache-configuration-to-avoid-caching-suprises

PHP OpCache and PHP Workers and PHP-FPM Pools

Needs more detail.

PHP OPCache Statistics and Tools

The following are PHP OPCache statistics and tools that you can use to manage and review your PHP OPcache.

WordPress Plugins

These are WordPress specific plugins available right within your WordPress Dashboard.

  1. OPcache Manager
  2. WP OPcache

Standalone

These are standalone, meaning you have to install them into your web root.

  1. CacheTool
  2. OPcache GUI by amnuts
  3. PeeHaa’s OpCacheGUI
  4. OPcache Dashboard by Carlos Buenosvinos
  5. OCP by _CK_

Suggested PHP OpCache Settings for WordPress

Here is the suggested PHP OPCache that I use.

opcache.validate_timestamps=1
opcache.revalidate_freq=60
opcache.memory_consumption=256 # (Be careful and check memory usage)
opcache.interned_strings_buffer=64
opcache.max_accelerated_files=32500
opcache.save_comments=1 # (
opcache.fast_shutdown=1
opcache.enable_cli=1

Citations

Changelog

  • 12-14-2022 – Article Created
0 Shares:

You May Also Like