PHP Reference ยท Web and Security
PHP OPcache
Reference for PHP OPcache: how bytecode caching speeds up PHP, the key opcache.* settings, checking status, resetting after deploys, JIT and preloading.
Normally PHP reads, parses and compiles each source file into bytecode on every request. OPcache is a built-in extension that keeps the compiled bytecode in shared memory, so later requests skip parsing and compiling. For typical applications it cuts response times noticeably and costs only memory, which is why it should be on in production.
OPcache is configured in php.ini. It benefits web servers (PHP-FPM, Apache) most. The command line normally starts a new process for each run, so there is nothing to reuse, and opcache.enable_cli is off by default.
opcache.enable=1 opcache.memory_consumption=256 opcache.validate_timestamps=0
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Read-only: asking a web server's OPcache for its state.
$status = opcache_get_status(false); echo $status['opcache_enabled'] ? 'on' : 'off'; echo $status['memory_usage']['used_memory']; echo $status['opcache_statistics']['opcache_hit_rate'];
Key settings
| Directive | Meaning |
|---|---|
opcache.enable |
Master switch (default 1). |
opcache.enable_cli |
Also cache for the CLI (default 0); useful for long-running workers and tests. |
opcache.memory_consumption |
Shared memory in MB for bytecode (default 128). |
opcache.max_accelerated_files |
Maximum number of cached files; set above your file count. |
opcache.interned_strings_buffer |
MB for shared identical strings. |
opcache.validate_timestamps |
1 checks files for changes; 0 never re-checks (fastest, but needs a reset on deploy). |
opcache.revalidate_freq |
Seconds between checks when timestamps are validated. |
opcache.jit, opcache.jit_buffer_size |
The JIT compiler (PHP 8.0+); helps CPU-heavy code more than typical web requests. |
opcache.preload |
A script run at startup that loads classes permanently (PHP 7.4+). |
Usage notes
- After a deploy with
validate_timestamps=0, reload PHP-FPM or callopcache_reset()through the web SAPI (not the CLI, which has a separate cache). Otherwise old code keeps running. - Zero-downtime deploys switch a symlink to a new release directory; also set
opcache.revalidate_pathor reload FPM so the new path is picked up. - Watch the hit rate and free memory (
opcache_get_status()). A full cache stops caching new files. Raisememory_consumptionormax_accelerated_files. - Do not expose status pages publicly. They list file paths.
- JIT rarely changes the speed of database-bound web apps. Benchmark before enabling it.
- Preloading keeps selected classes in memory for every request, but changing a preloaded file requires restarting PHP. Frameworks may provide a ready-made preload file.
- Development: leave
validate_timestamps=1so edits show immediately. - Tools:
opcache_invalidate($file, true)drops one file;opcache_compile_file()warms it.
Related
- php.ini,
ini_set()andini_get(), Composer - Lessons: Performance, Deployment