Content · Web Development · PHP

Configuring PHP OPcache for Production

Production OPcache stores compiled opcodes in shared memory and avoids repeatedly parsing scripts.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Admin

Production OPcache stores compiled opcodes in shared memory and avoids repeatedly parsing scripts.

What It Does and When to Use It


Example FPM INI directives; apply changes only in the runtime that serves requests. OPcache caches compiled opcodes, JIT may optimize CPU-intensive PHP, while APCu caches application-owned values.

Requirements, Inputs and Expected Behavior


Confirm the runtime, extension, environment variables, and permissions before running the examples in production. Tune memory and invalidation to deployment strategy; settings must be loaded by the intended SAPI.

Example 1: Basic Operation


Example FPM INI directives; apply changes only in the runtime that serves requests.

opcache.enable=1
opcache.memory_consumption=192
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.validate_timestamps=0

Example 2: Behavior and Validation


Read active OPcache configuration instead of assuming the edited INI file was loaded.

<?php
$config = opcache_get_configuration();
foreach (['opcache.enable','opcache.memory_consumption','opcache.validate_timestamps'] as $name) { echo $name, ': ',var_export($config['directives'][$name] ?? null,true),PHP_EOL; }

Practical Example: Application Use


Check cache statistics in the target FPM worker; CLI counters are not representative.

<?php
$status = opcache_get_status(false);
if (!$status) { echo 'OPcache disabled for this SAPI'; exit; }
$stats=$status['opcache_statistics'];
echo 'Cached: ', $stats['num_cached_scripts']??0, ' Hits: ', $stats['hits']??0;

Important Behavior and Edge Cases


The second example highlights: Read active OPcache configuration instead of assuming the edited INI file was loaded. The practical example adds: Check cache statistics in the target FPM worker; CLI counters are not representative.

Comparison with Related Approaches


ApproachWhat changes
OPcache, JIT and APCuOPcache caches compiled opcodes, JIT may optimize CPU-intensive PHP, while APCu caches application-owned values.
Choosing safelyProfile application response times and memory under representative traffic before tuning.

Common Beginner Mistakes


MistakeCorrect approach
Assuming a cache is shared across multiple serversConfirm process- and host-level cache scope; use a distributed store when needed.
Never invalidating cached dataSet a TTL or versioned cache key and test release-time invalidation.
Skipping operational constraintsTune memory and invalidation to deployment strategy; settings must be loaded by the intended SAPI.

Key Takeaways


  • Production OPcache stores compiled opcodes in shared memory and avoids repeatedly parsing scripts.
  • Check cache statistics in the target FPM worker; CLI counters are not representative.
  • Profile application response times and memory under representative traffic before tuning.
Share this resourceSend this page to someone who may find it useful.