Custom PHP

PHP OPcache Configuration That Actually Speeds Up Production

PHP OPcache configuration is the set of php.ini directives that decide how many compiled scripts PHP keeps in shared memory, how often it checks them for changes, and whether it preloads code at startup. I tune memory_consumption, max_accelerated_files and validate_timestamps against the real file count of each build, then use preloading and deploy-time resets to remove per-request compilation without serving stale code.

By Saif Al Jahanwebdev-saif.com

What OPcache actually caches, and why the settings matter

OPcache stores the compiled opcode of each PHP script in shared memory, so PHP skips parsing and compiling the same file on every request. The extension ships with PHP but stays off until opcache.enable=1 is set in the loaded php.ini. Configuration matters because that memory is finite: undersize it and scripts are evicted and recompiled under load; leave validation on and PHP stats files on every request. I treat OPcache as part of the runtime, not an afterthought, and read opcache_get_status() on the live server before changing a single directive. The gain is biggest on template-heavy builds where the same dozens of files run each request.

  • opcache.enable=1 enables web requests; opcache.enable_cli=1 is a separate switch and usually unnecessary
  • Compiled scripts are keyed by their real path on disk and live in shared memory
  • opcache_get_status() and opcache_get_configuration() expose hit rate, memory use and loaded directives
  • On shared hosting OPcache may be on while the directives are locked down

Sizing memory_consumption and max_accelerated_files

I size from the build, not from a copied snippet. Count the files with find /path/to/app -name '*.php' | wc -l, then set opcache.max_accelerated_files to a prime number comfortably above that count — the directive rounds up to a prime internally, and a full cache causes evictions. opcache.memory_consumption is in megabytes; for a mid-sized WordPress or custom PHP application 128M is a reasonable starting point, and I raise it when status shows memory_usage approaching the limit or the hit rate sagging under traffic. opcache.interned_strings_buffer deduplicates repeated strings across workers and matters more in framework-heavy code. I change one directive at a time and watch opcache_get_status() rather than guessing.

  • Set max_accelerated_files above the real file count, using a prime number
  • Start memory_consumption at 128M for a mid-sized app, then adjust from status data
  • Raise interned_strings_buffer above its 8M default when code repeats many strings
  • Watch memory_usage and opcache_hit_rate after every single change

Validation: validate_timestamps and revalidate_freq

These two directives decide how often PHP asks whether a cached script changed on disk. With opcache.validate_timestamps=1 and opcache.revalidate_freq=60, PHP checks a file's timestamp at most once per minute — cheap enough for development, but still extra stat calls under load. In production I set validate_timestamps=0 so cached scripts are never revalidated, which removes that overhead, and I make deployment responsible for clearing the cache. The risk is obvious: deploy code without a reset and users keep running the old version until a worker restarts. That trade-off is only safe when the release pipeline includes an explicit opcache_reset() call or a PHP-FPM reload. I never ship it without that step documented.

  • Development: validate_timestamps=1 with revalidate_freq=2 so edits appear quickly
  • Production: validate_timestamps=0 plus a cache clear during every deploy
  • revalidate_freq=0 means check on every request — use only in development
  • Clear the cache with opcache_reset() or by reloading php-fpm after a release

Preloading with opcache.preload

Preloading compiles a chosen set of scripts into shared memory when PHP-FPM starts, before any request arrives. I point opcache.preload at a PHP file that calls opcache_compile_file() over the hot path: bootstrap code, the autoloader, core classes and frequently used helpers. That script runs at startup as the user starting the pool, so setting opcache.preload_user avoids PHP refusing to preload when the pool starts as root. Preloading is not free — it lengthens worker startup and consumes memory — so I keep a small deliberate list instead of every file in the project. I verify with opcache_get_status()['preload_statistics'] where available and confirm the app still boots after a restart.

  • Keep the preload script in application code and under version control
  • Call opcache_compile_file() on a specific list, not a recursive directory scan
  • Set opcache.preload_user when PHP-FPM starts as root
  • Restart PHP-FPM after editing the preload script — it runs once at startup

JIT and how it interacts with OPcache memory

OPcache also hosts PHP's JIT compiler. opcache.jit=tracing, or the numeric equivalent 1255, compiles hot loops into machine code at runtime, which can help CPU-bound work such as data processing; it rarely moves the needle on database-bound WordPress pages. JIT needs its own allocation through opcache.jit_buffer_size, commonly 64M or 128M, and that buffer comes out of the same process memory budget. If it is too small, JIT quietly does less than expected. I enable it only after hit rate and memory are healthy, measure before and after with a realistic workload, and switch it off again when there is no measurable gain. Adding directives is not a performance strategy.

Common production mistakes I see in PHP builds

Most OPcache problems are operational, not exotic. A deploy that forgets to reset leaves old code live and looks like a broken release. A memory_consumption value copied from a blog undersizes a larger application and causes evictions under load. Turning validate_timestamps off on a server where OPcache is shared across multiple sites can serve stale code between vhosts. Enabling opcache.enable_cli to fix a CLI script wastes memory in every command-line process. I have also seen JIT enabled with no buffer, and preload scripts that scan an entire vendor directory so restarts take minutes. Each is fixable with a status check and a deliberate config.

  • Deploying without opcache_reset() or a PHP-FPM reload
  • Undersized memory_consumption and max_accelerated_files causing evictions
  • validate_timestamps=0 with no documented cache-clear step in the release
  • Preload scripts that compile thousands of files and slow every restart
  • JIT enabled without opcache.jit_buffer_size, or enabled on I/O-bound pages

How I verify an OPcache setup

I verify with data, not assumptions. First I confirm the directives actually loaded, using php -i | grep opcache and opcache_get_configuration(), because a setting in the wrong php.ini file is a common reason a change appears to do nothing. Then I read opcache_get_status() for num_cached_scripts, memory_usage and opcache_hit_rate before and after a realistic traffic sample; a hit rate that drops under load points at memory or max_accelerated_files. After a deploy I confirm the new code is running rather than trusting the pipeline. I am Saif Al Jahan, a WordPress and PHP developer in Mymensingh, Bangladesh, and this is the same check I run on client builds before calling a performance change done.

Frequently asked questions

What is OPcache and why does configuration matter?

OPcache is a PHP extension that stores compiled script bytecode in shared memory so PHP does not recompile the same files on every request. Configuration matters because that memory is finite and validation has a per-request cost: get the sizing and reset strategy wrong and you either waste memory or serve stale code. It is turned on with opcache.enable=1.

Should I set opcache.validate_timestamps to 0 in production?

Yes, if your deployment process clears the cache. Setting validate_timestamps=0 stops PHP checking files for changes, removing stat calls from every request. Without a matching opcache_reset() or PHP-FPM reload in the release, users keep running the old code until a worker restarts. Leave it enabled in development where edits should appear without a reset.

How much memory should opcache.memory_consumption be?

Start around 128M for a mid-sized WordPress or custom PHP app, then let data decide. Check opcache_get_status() under load: if memory_usage approaches the configured limit or the hit rate falls as traffic rises, raise it. There is no universal number. Count your PHP files and set max_accelerated_files above that count as well.

What does opcache.preload actually do?

It compiles a chosen set of PHP scripts into shared memory when PHP-FPM starts, so the first request does not pay to compile them. You point opcache.preload at a script that calls opcache_compile_file() on a deliberate list of hot files. Keep the list small: preloading thousands of files makes restarts slow and consumes memory.

How do I clear OPcache after a deploy?

Call opcache_reset() from a controlled deploy script, or reload PHP-FPM, which clears the cache as workers restart. A web request cannot always reset the cache used by other pool workers, so I prefer a reload inside the release pipeline. Do not leave a public, unauthenticated reset endpoint on a production server.

Is OPcache JIT worth enabling for WordPress?

Usually not for database-driven WordPress pages, where time goes to SQL and I/O rather than CPU. JIT helps CPU-heavy loops and can be enabled with opcache.jit=tracing plus an opcache.jit_buffer_size of 64M or more. I measure before and after on a realistic workload and disable it when the numbers do not move.

Written by Saif Al Jahan, webdev-saif.com · Mymensingh, Bangladesh. Last updated .