OPcache and JIT
OPcache works out of the box with OxPHP. All PHP worker threads share a single OPcache memory segment. Scripts are compiled once at first execution and served from the cache by every worker thereafter. No special setup is required to enable this sharing.
How OPcache works with OxPHP
OxPHP registers itself as a named SAPI, and OPcache treats it identically to other server SAPIs. In practice:
- All PHP worker threads use the same compiled opcode cache. When one worker compiles a file, every worker uses the result.
- Scripts are not compiled per request. After the first request for each script, subsequent requests skip the parse and compile step entirely.
opcache.enable_clidoes not affect OxPHP. That setting applies only to SAPIs namedcliandphpdbg. OxPHP registers with the SAPI namecli-server, so OPcache is controlled solely viaopcache.enable. Theopcache.enable_clisetting is useful if you run PHP CLI in the same container (e.g., for migrations or Artisan commands). The official OxPHP image ships PHP CLI alongside the server binary, so you can setopcache.enable_cli=1if your CLI scripts benefit from caching.
To enable OPcache, at minimum:
[opcache]
opcache.enable=1OPcache is already loaded in the official OxPHP Docker image. On PHP 8.5 and 8.6 it is compiled into the PHP binary, and on PHP 8.4 it is a shared extension that the PHP base image loads from conf.d/docker-php-ext-opcache.ini. Do NOT add zend_extension=opcache to your INI file: on 8.4 PHP reports that OPcache is already loaded, and on 8.5 and 8.6 there is no opcache.so to load, so either way the line costs a message on every PHP startup. Only the [opcache] configuration section is needed.
Recommended production settings
These settings are optimized for production container deployments where PHP files do not change at runtime. Disable timestamp validation for maximum throughput. To also compile files at startup, add the two settings from Preloading.
[opcache]
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.validate_timestamps=0
opcache.revalidate_freq=0
opcache.file_update_protection=0
opcache.jit_buffer_size=64M
opcache.jit=tracing| Setting | Recommended Value | Description |
|---|---|---|
memory_consumption |
128 |
Shared memory in MB for compiled scripts. Increase if opcache_get_status() shows low free memory. |
interned_strings_buffer |
16 |
Memory in MB for interned strings shared across all workers. |
max_accelerated_files |
10000 |
Maximum number of cached scripts. Set this higher than your total .php file count. |
validate_timestamps |
0 |
When 0, OPcache never checks the filesystem for changes. Restart the container to pick up code changes, or call opcache_reset() at the cost described in OPcache restarts. |
revalidate_freq |
0 |
Seconds between filesystem checks. Has no effect when validate_timestamps=0. |
file_update_protection |
0 |
Seconds after file modification before the file is eligible for caching. Set to 0 to cache immediately at startup. |
Development settings
For development, enable timestamp validation so code changes take effect without restarting the container. Disable JIT to get clearer stack traces during debugging.
[opcache]
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.validate_timestamps=1
opcache.revalidate_freq=2
opcache.jit_buffer_size=0
opcache.jit=disableWith validate_timestamps=1, OPcache checks file modification times every revalidate_freq seconds. This adds a small per-request overhead but lets you edit PHP files and see changes on the first request after that interval.
This is the recommended development-mode reload strategy for OxPHP. OPcache checks an included file's timestamp at most once per revalidate_freq seconds, so code edits are picked up once that window has passed, without a container restart or an external file-watcher daemon. Use revalidate_freq=0 for immediate stat-per-include (highest accuracy, slightly more I/O), or revalidate_freq=2 to amortize the stat cost. The default shown above is a good balance, especially if your DOCUMENT_ROOT is on a slow bind-mount (Docker on macOS/Windows).
What is NOT reloaded by validate_timestamps
A few classes of changes still require a container restart (or worker recycle) even with validate_timestamps=1:
- Preloaded files (
opcache.preload) are linked into the server at startup and are never revalidated. After editing a preloaded file, restart the container. - In Worker Mode, bootstrap state (the autoloader, DI container, and any objects built in the outer scope) lives in worker memory. OPcache recompiles changed class files, but the worker does not re-run its bootstrap. For dev worker loops, call
Worker::scheduleExit()at the end of each request (e.g. behind anOXPHP_DEVenv flag) to recycle the worker, which re-executes the outer scope and picks up all changes. - Framework-level caches (the compiled Symfony container, Laravel route/config/view cache, Composer optimized classmap) are
.phpfiles OPcache revalidates, but the values inside them reference stale class paths or container IDs. Run the framework'scache:clearcommand; OPcache alone is not enough. - Non-PHP files include
.env,composer.json, YAML/JSON config, and template files compiled outside OPcache. OPcache tracks only files it compiled; everything else needs a restart.
JIT compilation
OPcache's JIT compiler translates PHP opcodes into native machine code at runtime. Use tracing mode for the best optimization:
opcache.jit=tracing
opcache.jit_buffer_size=64MJIT provides the most benefit for CPU-bound PHP code: math-heavy loops, string processing, image manipulation, and template rendering. For I/O-bound applications that spend most of their time waiting on database queries or external API calls, the improvement is minimal.
To disable JIT:
opcache.jit=disable
opcache.jit_buffer_size=0Preloading
OPcache preloading compiles and caches PHP files at server startup, before any requests are handled. This eliminates first-request compilation cost entirely and makes classes and functions available globally without any require or autoload overhead.
Configure preloading in your INI file:
opcache.preload=/var/www/html/preload.php
opcache.preload_user=www-dataCreate a preload.php script that loads your most frequently used files:
<?php
// preload.php — runs once at server startup
require __DIR__ . '/vendor/autoload.php';
// Preload framework core files
$files = glob(__DIR__ . '/vendor/symfony/http-kernel/**.php');
foreach ($files as $file) {
opcache_compile_file($file);
}
// Preload hot application paths
opcache_compile_file(__DIR__ . '/src/Controller/ApiController.php');
opcache_compile_file(__DIR__ . '/src/Service/UserService.php');Preloaded classes and functions are permanently available to all requests. They cannot be changed without restarting the server.
If you use Worker Mode, your application is already initialized once: the autoloader, configuration, and database connections persist between requests. OPcache preloading complements this by eliminating opcode compilation overhead, but it does not replace application initialization. The two mechanisms work independently and can be used together.
Applying the PHP configuration
OxPHP reads PHP configuration from the standard conf.d directory. Use a Docker volume or a COPY instruction to supply your custom INI file.
docker run -p 80:80 \
-v ./custom.ini:/usr/local/etc/php/conf.d/custom.ini:ro \
ghcr.io/oxphp/oxphp:0.16.0FROM ghcr.io/oxphp/oxphp:0.16.0
COPY custom.ini /usr/local/etc/php/conf.d/custom.ini
COPY --chown=www-data:www-data . /var/www/htmlservices:
app:
image: ghcr.io/oxphp/oxphp:0.16.0
ports:
- "80:80"
volumes:
- ./custom.ini:/usr/local/etc/php/conf.d/custom.ini:ro
- ./src:/var/www/htmlMonitoring cache status
Inspect the live OPcache status from PHP to verify it is working:
<?php
$status = opcache_get_status();
echo "Cached scripts: " . $status['opcache_statistics']['num_cached_scripts'] . "\n";
echo "Cache hits: " . $status['opcache_statistics']['hits'] . "\n";
echo "Cache misses: " . $status['opcache_statistics']['misses'] . "\n";
echo "Free memory: " . $status['memory_usage']['free_memory'] . " bytes\n";If free_memory is consistently low, increase opcache.memory_consumption.
OPcache restarts
OPcache restarts its shared memory, dropping every cached script, when opcache_reset() is called. It also restarts when its memory (opcache.memory_consumption) or its script table (opcache.max_accelerated_files) is full while at least opcache.max_wasted_percentage of the memory (5% by default) holds discarded scripts, such as files that changed after they were cached. A restart cannot run while code from that memory is still running, so on Linux OxPHP holds it until nothing that uses the cache is left:
- Requests that start while a restart waits run without the cache, as under PHP-FPM: they compile what they include, or load it from
opcache.file_cachewhen that is set, and do not wait. Such a request runs without the cache to its end, and holds the next restart. - The restart runs at the start of the first request once nothing holds it. Each of these holds it until it ends: a request that started with the cache, an
oxphp_async()task whose closure came from the cache (even after the request that dispatched it has ended), a pool thread that has run such a task, a worker or pool thread that called the factory or destroy callback of aShared\Poolanother thread created, and a request that started while an earlier restart waited. - In Worker Mode, a worker that booted with the cache holds the restart and is retired at its next moment with no request in flight. Its replacement runs the worker script's bootstrap again. A restart costs up to two rounds of worker replacement, plus one each time a worker that booted while the restart waited calls such a
Shared\Poolcallback before the restart has run. Workers are not retired during a graceful shutdown. - The threads of the
oxphp_async()pool are replaced the same way, at a moment they run no task, and once more for a thread that started while the restart waited and then ran a task whose closure came from the cache. - A worker or pool thread that a steady load leaves no such moment stops taking new requests or tasks for at most 5 seconds to reach one, in turn with the other threads of its kind, which carry the load meanwhile. With
PHP_WORKERS=1orASYNC_WORKERS=1nothing else takes the work, up to twice per restart: requests wait and are refused onceQUEUE_WAIT_TIMEOUT_MShas passed, and tasks stay on the queue, where a task dispatched while it is full is refused as usual.
A holder that never ends keeps the restart waiting for as long as it runs: a Server-Sent Events stream or a long poll that started with the cache, or a task in an endless loop whose closure came from the cache. So does a request or task that outlasts those 5 seconds on a thread a steady load never frees, and with ASYNC_WORKERS=1 a task waiting for another task. A steady stream of tasks whose closures opcache.preload loaded keeps it waiting too, since each one holds it from its dispatch. OxPHP stops none of them, and requests run without the cache until the restart runs.
opcache.force_restart_timeout (180 seconds by default), after which PHP-FPM kills the processes that still hold a restart, only makes OxPHP log a warning once a restart has waited that long, with the number of requests and tasks holding it (holders). 0 turns the warning off.
The log at info level records each restart and each thread it replaces:
| Message | When |
|---|---|
OPcache restart scheduled |
The request that scheduled the restart has ended. reason is opcache_reset(), out of memory or hash overflow. |
Worker retiring: …, Async worker thread retiring: … |
A worker or pool thread is replaced: an OPcache restart is waiting for it, or it opened while an OPcache restart was pending. |
Worker taking no new requests …, Async worker thread taking no new tasks … |
A worker or pool thread has stopped taking new work to reach a moment with none in flight. |
Worker taking requests again: …, Async worker thread taking tasks again: … |
It takes work again, because what it runs outlasted the wait or going without work no longer helps the restart. |
OPcache restart done |
The restart has run. waited_ms is how long it waited. |
OPcache restart still waiting, … (warn) |
The restart has waited longer than opcache.force_restart_timeout. |
To deploy new code, prefer replacing the container. opcache_reset() runs every request without the cache until the restart and, in Worker Mode, boots every worker again.
Prompts for your agent
These prompts assume an agent with the OxPHP documentation MCP server connected. The agent reads the pages it needs itself.
Tune OPcache for this repository's actual code size and how its image gets built. Read the `php/opcache` page through the OxPHP documentation MCP server.
Count the `.php` files this project ships to size `max_accelerated_files` and `memory_consumption`, and pick `validate_timestamps` based on whether my Dockerfile bakes the code in or a bind mount overlays it at runtime.See also
- Docker Guide: container setup and mounting configuration files
- Configuration Reference: environment variables for OxPHP
- Worker Mode: persistent PHP workers that benefit most from OPcache