Magento 2 Opcache and PHP Configuration Tuning

Magento 2 Opcache and PHP Configuration Tuning

November 27, 2025 · By Magento Company
Magento 2 Opcache and PHP Configuration Tuning

PHP’s default settings assume small applications. Magento - tens of thousands of PHP files, a megabytes-deep framework stack - is not a small application. A tuned php.ini is free performance: no code changes, no infrastructure spend, just configuration that matches reality. Here are the settings that matter.

OPcache: The Big One

OPcache stores compiled PHP bytecode in memory, skipping parse-and-compile on every request. It is not optional for Magento production - it is the difference between the framework bootstrapping in milliseconds versus seconds:

opcache.enable=1
opcache.memory_consumption=1024
opcache.interned_strings_buffer=64
opcache.max_accelerated_files=100000
opcache.validate_timestamps=0
opcache.save_comments=1

The non-obvious ones:

  • max_accelerated_files=100000: Magento plus vendor can exceed the default 10k file cap easily - when the cache fills, files silently fall out of cache and performance craters. Check opcache_get_status() and count your files
  • validate_timestamps=0: production setting - PHP never checks if files changed, so deploys must reset OPcache (opcache_reset() via a script or an FPM reload). Forget this and deploys serve old code
  • save_comments=1: Magento reads docblock annotations (service contracts, API metadata) - turning comments off breaks things in exotic ways
  • memory_consumption: monitor actual usage; 512MB-1GB is the realistic range for Magento

Memory Limits

memory_limit=2G          ; CLI: imports, setup, compile
memory_limit=768M        ; FPM: web workers

Differentiate CLI and FPM: cron jobs, imports and setup:di:compile legitimately need more than a web request ever should. A web request needing 2GB is a bug wearing a configuration fix.

Realpath Cache

PHP caches file path resolution - valuable when Magento resolves thousands of paths per request:

realpath_cache_size=4M
realpath_cache_ttl=600

FPM Interaction

OPcache is per-FPM-master: one shared cache across workers, which is what you want. But remember the deployment contract: validate_timestamps=0 + no cache reset on deploy = stale code served confidently. Automate the reset in your deployment pipeline, and verify it in the pipeline’s smoke tests (a version string or build hash on the page is a cheap probe).

Verify, Don’t Assume

After tuning: warm the store, then check opcache_get_status() for hit rates (>99 percent after warmup), memory usage headroom, and cached-file count versus your actual file count. OPcache done right is invisible; OPcache done wrong is a store that gets slower the longer it runs - worth five minutes of verification.

Performance Infrastructure Magento 2