opcache.validate_timestamps=0 will ruin your next deploy
Disabling timestamp validation in OPcache is standard production advice. It's also a silent footgun that will serve stale code after a deploy if you don't have a plan.
Every PHP performance guide tells you to set opcache.validate_timestamps=0 in production. The advice is correct. OPcache checking file mtimes on every request is wasteful when your files don't change between deployments. What those guides skip is the part where your next deploy silently serves cached bytecode from the old version and you spend two hours wondering why your bug fix isn't working.
I've hit this. More than once. Most recently on a Laravel app for a healthcare client where a critical calculation change I deployed was just... not running. The old logic was executing. The new files were on disk. OPcache had no idea.
What OPcache Actually Does
PHP is an interpreted language but it doesn't re-parse source files on every request. OPcache compiles your .php files to bytecode and stores that bytecode in shared memory. Subsequent requests skip the parse/compile step entirely and run the cached bytecode directly.
When opcache.validate_timestamps=1 (the default), PHP checks the file's mtime on each request (subject to opcache.revalidate_freq, default 2 seconds) and recompiles if the file changed. Acceptable for development. In production with thousands of requests per second, those stat() calls add up.
When opcache.validate_timestamps=0, PHP never checks. It compiles a file once and serves that bytecode forever - or until the cache is manually cleared, or PHP-FPM restarts.
That "or until" is the whole problem.
The Stale Deploy Scenario
Here's the exact failure mode. You have PHP-FPM running with OPcache warmed up on your current codebase. You deploy new code. The files on disk change. OPcache doesn't know and doesn't care. PHP-FPM keeps serving the old bytecode. Your users get the old behavior. You get paged.
Some deploy setups avoid this accidentally. If you're doing full server restarts, or if your deploy process kills and respawns PHP-FPM workers, the cache gets wiped. But if you're deploying to a live server without restarting PHP-FPM - symlink swaps, rsync, git pull, whatever - you're leaving a loaded gun on the table.
The symlink pattern is especially deceptive. You swap the symlink, the new files are there, ls -la shows the right code, tail on a source file shows your changes. OPcache is still running bytecode from the previous release directory. The symlink swap didn't touch PHP-FPM.
The Fix: opcache_reset() on Deploy
The standard answer is to call opcache_reset() as part of your deploy process. This clears the entire cache. PHP-FPM recompiles everything fresh on the next requests.
Here's the catch: opcache_reset() only affects the process that calls it. If you run it from the CLI (artisan, a deploy script), you're resetting the CLI process's cache, not the PHP-FPM workers' shared memory segment. They are separate processes with separate cache pools.
You have to trigger the reset from within a PHP-FPM context. The cleanest way is a small script you hit via HTTP during the deploy:
<?php
// opcache-reset.php - deploy step hits this, then deletes it
// Protect this. Seriously.
$token = $_SERVER['HTTP_X_DEPLOY_TOKEN'] ?? '';
$expected = getenv('DEPLOY_SECRET');
if (!$expected || !hash_equals($expected, $token)) {
http_response_code(403);
exit('Forbidden');
}
if (!function_exists('opcache_reset')) {
http_response_code(500);
exit('OPcache not available');
}
$result = opcache_reset();
http_response_code($result ? 200 : 500);
echo json_encode(['reset' => $result, 'ts' => time()]);
And in your deploy script (this is Envoy-flavored but the logic is portable):
# After files are in place, before traffic hits the new code
curl -sf \
-H "X-Deploy-Token: ${DEPLOY_SECRET}" \
https://yourapp.com/opcache-reset.php
# Check the exit code
if [ $? -ne 0 ]; then
echo "OPcache reset failed - aborting deploy"
exit 1
fi
In Laravel specifically I wire this into an Artisan command that runs post-deploy and hits the internal endpoint:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
class OpcacheReset extends Command
{
protected $signature = 'opcache:reset';
protected $description = 'Clear OPcache on the FPM workers via HTTP';
public function handle(): int
{
$url = config('app.url') . '/opcache-reset.php';
$secret = config('services.deploy.secret');
$response = Http::withHeaders([
'X-Deploy-Token' => $secret,
])->get($url);
if ($response->successful() && $response->json('reset') === true) {
$this->info('OPcache cleared.');
return self::SUCCESS;
}
$this->error('OPcache reset failed: ' . $response->body());
return self::FAILURE;
}
}
Delete or move opcache-reset.php immediately after the deploy step. Don't leave it sitting there.
Other Approaches Worth Knowing
PHP-FPM reload. systemctl reload php8.3-fpm does a graceful reload - replaces workers without dropping connections. Existing workers finish their current requests and exit, new workers start fresh with empty caches. This is the simplest reliable approach if you have the access and the reload is fast enough for your uptime requirements. For most apps it is.
opcache.file_cache. You can configure a filesystem-based secondary cache with opcache.file_cache=/tmp/opcache. When in-memory cache is cold (after FPM restart), PHP falls back to disk. Combined with FPM reload this gives you fast warmup. Worth setting up if your app has a lot of files and cold-start latency matters.
Per-file invalidation with opcache_invalidate(). If you know exactly which files changed you can invalidate just those:
opcache_invalidate('/var/www/app/app/Services/BillingService.php', true);
The second argument forces recompile even with validate_timestamps=0. In practice, tracking changed files reliably during a deploy is more bookkeeping than it's worth. I just reset everything.
The Config That Actually Makes Sense
Here's what I run in production php.ini or pool conf:
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.validate_timestamps=0
opcache.save_comments=1
opcache.fast_shutdown=1
And validate_timestamps=1 in the development pool. That's it. The difference between dev and prod is one line, but it means dev gets live file changes and prod gets maximum cache efficiency - with the explicit understanding that deploys must reset the cache.
The max_accelerated_files number matters. If your app has more PHP files than this limit, OPcache silently stops caching the overflow. Laravel plus vendor is typically 5,000-8,000 files. Run opcache_get_status() and check num_cached_scripts vs your limit. Bump the limit if you're close.
When This Bites Hardest
Blue-green and rolling deploys. If you have multiple servers and you reset OPcache on them one at a time, you'll have a window where some servers are running old bytecode and some are running new. For most web apps this is fine - your deploy should be backward compatible anyway. For anything with a migration or a changed API contract between services, this window is a real problem. FPM reload sequenced with your load balancer drain is the right answer there.
Docker is different. Containers are immutable. Each new container image gets a fresh OPcache. The problem doesn't exist in the same way, which is one of the legitimate reasons to prefer containerized PHP. If you're on bare metal or long-lived VMs with rsync-style deploys, you need to manage this explicitly.
When I'd Reach For validate_timestamps=1
Never in production. If you're in a situation where you can't reliably reset OPcache on deploy - maybe shared hosting, maybe a weird constraint - then you're better off with validate_timestamps=1 and revalidate_freq=0 (check on every request). The performance hit is real but predictable. Stale code in production is neither.
In staging, sure, depending on how often files change and whether the environment mirrors production closely enough to matter.
The Takeaway
The opcache.validate_timestamps=0 advice is right. The part everyone skips is making the deploy process responsible for clearing the cache explicitly. Build the reset into your deploy pipeline, verify it succeeds, and test it - actually test it by checking that a known change is live immediately after deploy. If you don't have a cache reset step, you're not running validate_timestamps=0 safely, you're just getting lucky on restarts.
Need help shipping something like this? Get in touch.