Skip to content

Configuring PHP opcache for production

Size opcache.memory_consumption to the codebase, disable timestamp checks, and pick the validation strategy that fits your deployment model.

PHP without opcache compiles every script on every request — a 30% performance tax for nothing. Opcache caches the compiled bytecode in shared memory, and on a steady-state production host the compilation cost is paid once and then never again. This article configures opcache for a real production install, including the deployment-aware choice between revalidating on the fly and resetting on deploy.

How to verify

Inspect the current opcache state:

php -i | grep -E "opcache\.(enable|memory_consumption|max_accel|validate|preload)"
php -r 'print_r(opcache_get_status(false));' 2>/dev/null | head -30
curl -s 'http://127.0.0.1/opcache-status.php' 2>/dev/null | head
sudo grep -E '^opcache' /etc/php/8.3/fpm/php.ini /etc/php/8.3/fpm/conf.d/10-opcache.ini

If opcache_enabled is false or memory_consumption is the default 128 MiB on a large codebase, opcache is missing for half the files — opcache_get_status() will show cache_full = true.

What’s happening

Opcache lives in shared memory. Every FPM worker, after compiling a script, stores the opcode array in that shared segment. The next worker that needs the same script reads it directly — no disk read, no parser, no compiler. The opcache hash table maps “file path + mtime” to opcodes.

Three numbers matter. opcache.memory_consumption is how big the shared segment is in MiB. Too small and opcache evicts files (the dreaded oom restart). opcache.max_accelerated_files is the maximum file count slot count — Composer-heavy projects easily hit 10 000 files, so the default 10 000 is a tight fit. opcache.interned_strings_buffer is a separate MiB pool for deduplicating strings; bigger string buffer = less duplication = effectively more cache room.

The deployment dimension is opcache.validate_timestamps. With it enabled, opcache stats the file on every request (or every N seconds with revalidate_freq) and recompiles if mtime changed — convenient in development. In production with an atomic deploy, it is wasted syscalls — set validate_timestamps=0 and call opcache_reset() explicitly after the deploy.

The fix

  1. Measure how many PHP files the application actually has:

    find /var/www/app -name '*.php' | wc -l

    Round up to the nearest power of 2 over 10 000 — 16 229 or 65 407 are commonly recommended primes for opcache.max_accelerated_files.

  2. Drop a dedicated opcache config at /etc/php/8.3/fpm/conf.d/20-opcache.ini:

    opcache.enable=1
    opcache.enable_cli=0
    opcache.memory_consumption=256
    opcache.interned_strings_buffer=32
    opcache.max_accelerated_files=16229
    opcache.validate_timestamps=0
    opcache.save_comments=1
    opcache.fast_shutdown=1
    opcache.huge_code_pages=0
    opcache.jit_buffer_size=0

    save_comments=1 is mandatory for any framework that reads docblocks for routing or DI metadata (Symfony, Doctrine annotations) — turning it off saves a tiny amount of memory and breaks the framework.

  3. Add a small status script for diagnostics (protect it with Nginx auth or allow 127.0.0.1; deny all; in the location):

    <?php
    // /var/www/app/public/opcache-status.php
    header('Content-Type: application/json');
    echo json_encode(opcache_get_status(false), JSON_PRETTY_PRINT);
  4. Reload FPM and verify the cache fills up:

    sudo systemctl reload php8.3-fpm
    # warm it up
    for i in $(seq 1 50); do curl -s http://app.example.com/ > /dev/null; done
    curl -s 'http://127.0.0.1/opcache-status.php' | jq '.opcache_statistics'

    cached_scripts should climb until it matches the active file count, and hits should dominate misses within a minute.

  5. Add opcache_reset() to the deploy script. With validate_timestamps=0, the only way new code becomes visible is an explicit reset:

    # post-deploy hook
    curl -s -X POST 'http://127.0.0.1/opcache-reset.php?secret=...'
    # or, equivalently, restart FPM:
    sudo systemctl reload php8.3-fpm

    For a Laravel/Symfony deploy this is part of the standard hook list, alongside php artisan config:cache or symfony console cache:clear.

Operational notes

  • The opcache JIT is opt-in and most production sites should leave jit_buffer_size=0. JIT helps math-heavy or long-running scripts; the typical web request is dominated by I/O and JIT does not move the needle.
  • opcache.preload (PHP 7.4+) ahead-of-time loads files into a permanent slot — great for very large frameworks, but the preload file runs as root and any error keeps the FPM from starting. Treat it as advanced tuning, not a default.
  • opcache.memory_consumption is per-pool. On a host running multiple FPM pools (one per app), each one gets its own segment — total RAM cost is memory * num_pools.
  • opcache.huge_code_pages=1 requires kernel huge page support and is worth testing only on hosts with hundreds of MiB of opcache memory; otherwise it adds complexity without benefit.
  • If you deploy via symlink swap (Capistrano-style current -> releases/timestamp), validate_timestamps=0 will miss the change because the file paths are different — the cache is keyed on resolved path. Either reset opcache on deploy or set opcache.revalidate_path=1.

For the surrounding FPM tuning, see PHP-FPM pool sizing. Stack Harbor’s managed operations practice ships opcache config in configuration management and the reset call in the deploy pipeline — the host is in one of two states (cache cold, cache warm) and the transition is observable.