PHP 7: Maintaining and Upgrading Legacy Apps · Performance on PHP 7

OPcache and Realpath Cache on PHP 7

Production OPcache settings and common misconfigurations.

OPcache is bundled since PHP 5.5 and is the most important performance setting on PHP 7 too. Enable it with enough memory and file slots, disable timestamp validation in immutable deployments, and tune realpath_cache_size for apps with many includes. PHP 7.4 adds preloading for frameworks.

opcache.enable=1
opcache.memory_consumption=192
opcache.max_accelerated_files=20000
opcache.validate_timestamps=0
opcache.save_comments=1          ; required by annotation-based frameworks/Doctrine
realpath_cache_size=4096K
realpath_cache_ttl=600

Common pitfalls

  • opcache.save_comments=0 breaking annotation-based libraries.
  • Symlink-swap deploys without resetting OPcache/realpath cache.
  • OPcache disabled on the CLI for long-running workers where it could help.

Key points

  • Enable and size OPcache
  • Keep save_comments for annotations
  • Reset caches on deploy

Further reading: PHP manual: OPcache configuration

Practice questions

Why might Doctrine annotations stop working after OPcache tuning?

Answer

If opcache.save_comments=0, docblocks are stripped from cached code, so annotation readers can't see them.

Why do symlink-based deploys need cache resets?

Answer

OPcache and the realpath cache may keep resolving the old target path, serving old code until PHP-FPM is reloaded.

Open in the app to mark lessons complete, take notes and track your progress.