What is OPcache and Why You Need It
PHP compiles scripts to opcodes on each request (unless cached). OPcache stores compiled opcodes in shared memory, bypassing recompilation. Results: 2-5x faster PHP execution, lower CPU, higher request capacity. WordPress with OPcache runs 3x faster than without. This guide covers enabling, tuning, and monitoring OPcache on Hostxpeed VPS.
How OPcache Works
When PHP script requested: normally PHP reads file, compiles to opcodes, executes. With OPcache: after first compilation, opcodes stored in memory. Subsequent requests reuse opcodes, skipping compilation. Memory cost: ~1MB per 1000 lines of code (negligible). Validation: checks script modification time (timestamp) or revalidate based on settings. Best for production with infrequent code changes.
Step 1: Enable OPcache
PHP 8.0+ includes OPcache but may be disabled. Check: php -m | grep opcache. Enable: edit php.ini (cli and fpm locations). For Ubuntu/Debian, /etc/php/8.2/cli/php.ini and /etc/php/8.2/fpm/php.ini. Add: zend_extension=opcache.so, opcache.enable=1, opcache.enable_cli=0 (CLI typically not needed). For RHEL: /etc/php.ini. Restart PHP-FPM: sudo systemctl restart php8.2-fpm.
Step 2: Basic Configuration
Recommended settings for VPS (2-8GB RAM): opcache.memory_consumption=256 (MB, increase to 512 for large apps). opcache.interned_strings_buffer=16. opcache.max_accelerated_files=10000 (increase for big projects). opcache.revalidate_freq=60 (seconds, check for file changes). opcache.fast_shutdown=1. opcache.validate_timestamps=1 (0 in production for performance, but 1 recommended for development). For production with no code changes often, set validate_timestamps=0, revalidate_freq=0.
Step 3: Optimize for WordPress
WordPress specific: memory_consumption=256, max_accelerated_files=20000 (WP has many files). Also enable: opcache.save_comments=1 (some plugins need comments). opcache.load_comments=1. Clear OPcache after plugin updates: use plugin (WP OPcache) or call opcache_reset() via admin. For high traffic, use opcache.revalidate_freq=180 (check every 3 minutes).
Step 4: Monitoring OPcache
Use opcache_get_status() function. Create info script: and search OPcache. Or use opcache-gui (GitHub project). Metrics: hits, misses, hit rate (target >90%). Memory usage: used_memory, free_memory, wasted_memory (if wasted >20%, increase memory_consumption). Restarts: if too many, increase max_accelerated_files.
Step 5: Managing Cache Clearing
After deploying new code, OPcache must revalidate. Options:1) opcache_reset() via web (admin endpoint). 2) restart PHP-FPM (sudo systemctl restart phpX.X-fpm). 3) Use opcache_invalidate(filename) selectively. 4) Set opcache.revalidate_freq=0 and touch files (filesystem mtime check). For CI/CD, call opcache_reset() after deployment (recommended). For WordPress, use WP CLI: wp opcache reset.
Step 6: Advanced Tuning
opcache.optimization_level=0xFFFF (all optimizations). opcache.enable_file_override=1 (serve from cache even if file exists but not cached? careful). opcache.max_wasted_percentage=5 (percentage of memory wasted before restart). opcache.use_cwd=1 (include current directory in cache key). opcache.validate_permission=0 (performance, less security). opcache.file_update_protection=2 (prevent caching conflicts during updates). opcache.preload_user=www-data (if using preloading).
Preloading (PHP 7.4+ Advanced)
Preloads selected files into OPcache when PHP-FPM starts (always in memory, never revalidated). Use for framework libraries (Laravel, Symfony). List files in opcache.preload (php.ini), e.g., opcache.preload=/path/to/preload.php. Preload script:
Troubleshooting Common Issues
Cache not working: check opcache.enable=1, restart PHP-FPM, no errors in logs. Memory full: increase memory_consumption. Low hit rate: revalidate_freq too low, or files changing too often (development). Wasted memory: restart PHP-FPM occasionally, or increase memory. Preloading fails: check file paths, permissions. OPcache conflicts with opcache.validate_timestamps=0: changes not reflected. Use .user.ini to override per directory.
Benchmark Results (WordPress Home Page)
No OPcache: 120ms TTFB, 35 req/sec. OPcache default: 45ms TTFB, 95 req/sec. OPcache tuned: 38ms TTFB, 110 req/sec. Memory usage increase: 80MB. CPU reduction: 65%. Conclusions: essential for production PHP. Implementation time: 5 minutes.
Conclusion
Enable OPcache now on your Hostxpeed VPS for immediate PHP performance boost. Default settings work for many; tune memory and max files for large apps. Monitor using opcache GUI. For critical apps, use preloading. No code changes required.