Skip to main content

OPcache Tuning

OPcache stores compiled PHP bytecode in shared memory so PHP does not have to recompile WordPress files on every request. When OPcache is enabled and sized correctly, dynamic requests consume less CPU and your response time is more consistent under load.

Quick Summary
  • Enable OPcache and keep opcache.save_comments=1 for WordPress/plugin compatibility.
  • Size opcache.memory_consumption and opcache.max_accelerated_files so OPcache does not thrash.
  • Keep validate_timestamps=1 unless you have a deployment process that reliably restarts PHP.

Start with a safe baseline and adjust based on what OPcache reports.

opcache.ini
opcache.enable=1
opcache.enable_cli=0

; Starting point: adjust based on cache usage
opcache.memory_consumption=192
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000

; WordPress/plugin compatibility
opcache.save_comments=1

; Code change detection (see tabs below)
opcache.validate_timestamps=1
opcache.revalidate_freq=60

Validation Strategy (Code Updates vs Performance)

These settings control how OPcache notices changed PHP files.

If WordPress/plugins/themes can update files directly on the server, keep timestamp validation enabled.

opcache-validate-timestamps.ini
opcache.validate_timestamps=1
opcache.revalidate_freq=60

This is usually the least surprising option for WordPress sites.

Sizing OPcache

Two common causes of poor OPcache performance are:

  • Not enough shared memory (opcache.memory_consumption)
  • Not enough slots for cached scripts (opcache.max_accelerated_files)

You can estimate the upper bound by counting PHP files in your WordPress install directory:

count-php-files.sh
# Run this inside your WordPress docroot
find . -type f -name '*.php' | wc -l

Then set opcache.max_accelerated_files comfortably above that count.

Monitoring OPcache Health

If you can safely expose it to admins only, you can inspect OPcache status:

opcache-status.php
<?php
header('Content-Type: application/json');
echo json_encode(opcache_get_status(false), JSON_PRETTY_PRINT);
caution

Do not expose OPcache status publicly. It can leak file paths and environment details.

Internal Health Monitoring Parameters

When reviewing OPcache status, focus on:

  • Hit rate: consistently low hit rates suggest churn or frequent cache invalidation.
  • Wasted memory: high fragmentation can indicate memory pressure.
  • Cache full flags: indicates max_accelerated_files is too low.

Common Mistakes & Troubleshooting

Configuration FailureOperational SymptomRemediation Protocol
opcache.save_comments=0Random plugin breakage, missing annotations, odd behavior in builders/ecommerce.Set opcache.save_comments=1.
validate_timestamps=0 without restartsUpdates/deploys don't take effect.Re-enable validation or restart PHP-FPM/LSWS on changes.
OPcache thrashingHit rate drops, TTFB varies for similar requests.Increase opcache.memory_consumption and/or opcache.max_accelerated_files.

What's Next