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.
- Enable OPcache and keep
opcache.save_comments=1for WordPress/plugin compatibility. - Size
opcache.memory_consumptionandopcache.max_accelerated_filesso OPcache does not thrash. - Keep
validate_timestamps=1unless you have a deployment process that reliably restarts PHP.
Recommended Baseline (WordPress-Friendly)
Start with a safe baseline and adjust based on what OPcache reports.
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.
- WP Updates (Default)
- Deploy Pipeline (Restart PHP)
If WordPress/plugins/themes can update files directly on the server, keep timestamp validation enabled.
opcache.validate_timestamps=1
opcache.revalidate_freq=60
This is usually the least surprising option for WordPress sites.
If you deploy code via CI/CD and reliably restart PHP-FPM/LSWS on every deploy, you can disable timestamp checks.
opcache.validate_timestamps=0
With validate_timestamps=0, code changes will not show up until PHP is restarted.
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:
# 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:
<?php
header('Content-Type: application/json');
echo json_encode(opcache_get_status(false), JSON_PRETTY_PRINT);
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_filesis too low.
Common Mistakes & Troubleshooting
| Configuration Failure | Operational Symptom | Remediation Protocol |
|---|---|---|
opcache.save_comments=0 | Random plugin breakage, missing annotations, odd behavior in builders/ecommerce. | Set opcache.save_comments=1. |
validate_timestamps=0 without restarts | Updates/deploys don't take effect. | Re-enable validation or restart PHP-FPM/LSWS on changes. |
| OPcache thrashing | Hit rate drops, TTFB varies for similar requests. | Increase opcache.memory_consumption and/or opcache.max_accelerated_files. |