PHP OPcache Configuration
PHP OPcache is a built-in caching engine that dramatically improves PHP performance by storing precompiled script bytecode in shared memory. This eliminates the need for PHP to parse and compile scripts on every request, resulting in significantly faster page load times.
What Is OPcache?
Without OPcache, every PHP request follows this process:
- Read the PHP source file from disk
- Parse the PHP code into tokens
- Compile tokens into bytecode (opcodes)
- Execute the bytecode
With OPcache enabled, steps 1-3 are skipped for cached scripts — the cached bytecode is executed directly from memory. This can improve PHP application performance by 2-5x or more.
Checking OPcache Status
To check if OPcache is currently enabled:
- Create a file named
phpinfo.phpin your document root:
```
<?php phpinfo(); ?>
```
- Access it via your browser:
https://yourdomain.com/phpinfo.php - Search for "OPcache" on the page
- Look for
opcache.enable— it should say On
Tip: Delete the
phpinfo.phpfile after checking, as it exposes sensitive server information.
Enabling OPcache
Via cPanel (MultiPHP INI Editor)
- Log in to cPanel
- Navigate to Software > MultiPHP INI Editor
- Select the Basic Mode tab
- Choose your domain from the dropdown
- Find opcache.enable and set it to Enabled
- Click Apply
Via DirectAdmin
- Log in to DirectAdmin
- Navigate to Account Manager > PHP Settings (or CustomBuild for admin)
- Enable OPcache for the desired PHP version
- Save and apply changes
Via php.ini or .user.ini
If you have access to PHP configuration files, add or modify:
[opcache]
opcache.enable=1
opcache.enable_cli=0
opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.validate_timestamps=1
opcache.save_comments=1
opcache.fast_shutdown=1For shared hosting, create a .user.ini file in your document root with the settings your hosting plan allows you to override.
Recommended Configuration Settings
For Small Sites (1-2 WordPress sites)
opcache.memory_consumption=64
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60For Medium Sites (Multiple sites or WooCommerce)
opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60For Large Sites (High traffic, many plugins)
opcache.memory_consumption=256
opcache.interned_strings_buffer=32
opcache.max_accelerated_files=20000
opcache.revalidate_freq=120Understanding Key Settings
| Setting | Description | Recommended |
|---|---|---|
| memory_consumption | Memory allocated for cached scripts (MB) | 128 |
| interned_strings_buffer | Memory for interned strings (MB) | 16 |
| max_accelerated_files | Maximum number of files to cache | 10000 |
| revalidate_freq | Seconds between checking for file changes | 60 |
| validate_timestamps | Check if files have changed (set 0 in production only) | 1 |
| save_comments | Cache PHP docblock comments (required by many frameworks) | 1 |
Tip: The max_accelerated_files value is internally rounded up to the next prime number. Common values: 3907, 7963, 10007, 16229.
OPcache and WordPress
WordPress benefits significantly from OPcache. After enabling:
- Clear any existing page caches (WP Super Cache, W3 Total Cache, etc.)
- Load a few pages to warm up the OPcache
- Use a plugin like OPcache Dashboard to monitor cache hit rates
- Target a hit rate above 95% for optimal performance
If you see changes not reflecting on your site after updating themes or plugins, the OPcache may be serving old cached versions. Either wait for the revalidate_freq interval or clear the OPcache.
Clearing OPcache
Via PHP Script
Create a temporary file:
<?php opcache_reset(); echo "OPcache cleared"; ?>Via Command Line (VPS)
php -r "opcache_reset();"Note: CLI and web OPcache are separate. The CLI command only clears the CLI cache. For web cache, use a PHP script accessed via the browser or restart PHP-FPM:
systemctl restart php8.2-fpmTroubleshooting
OPcache Not Showing in phpinfo()
- The OPcache extension may not be installed — contact support or install via your control panel
- Check if the correct PHP version is selected for your domain
High Memory Usage
- Reduce memory_consumption if your server is memory-constrained
- Reduce max_accelerated_files to limit cached scripts
Changes Not Reflecting on Site
- Set validate_timestamps=1 and reduce revalidate_freq to 2-5 seconds during development
- Clear OPcache after deploying changes
Related Articles
Need help configuring OPcache? Contact our support team at {{SUPPORT_EMAIL}} or open a ticket at {{SUPPORT_URL}}.