Screenshot showing OPcache settings in WordPress dashboard

OPcache Explained: How to Properly Enable and Tune It for WordPress

, , , ,

Series: WordPress Performance on DirectAdmin (Rocky Linux 9)

Phase 2: PHP & Runtime — Part 7 of 30

OPcache Explained: How to Properly Enable and Tune It for WordPress

Opcode caching, specifically OPcache, is essential for WordPress performance on DirectAdmin-managed Rocky Linux 9 servers. Correctly enabling and tuning OPcache reduces PHP execution time by caching precompiled script bytecode in memory, minimizing file system hits and PHP parsing. This article provides a practical, engineering-focused approach to deploying and optimizing OPcache for WordPress, with DirectAdmin and Rocky Linux 9 specifics.

Understanding OPcache: Why It Matters for WordPress

  • Reduces PHP load: OPcache stores compiled PHP scripts in memory, reducing CPU usage and disk I/O.
  • Improves response times: Faster execution benefits dynamic WordPress workloads.
  • Essential for scaling: Especially impactful on high-traffic or multi-site WordPress servers.

DirectAdmin on Rocky Linux 9 typically uses PHP-FPM pools (per-domain or per-user) with Apache or NGINX as the web server. OPcache is implemented at the PHP-FPM worker/process level; tuning may be global or per-PHP version, and certain settings can be overridden at the pool or user level.

Checklist: Enabling and Verifying OPcache

  1. Install OPcache (if not present).
  2. Enable OPcache in the correct php.ini for your PHP version.
  3. Reload/restart PHP-FPM via DirectAdmin or systemd.
  4. Verify OPcache is loaded and functioning.

1. Install OPcache with Rocky Linux 9 DNF

Most DirectAdmin PHP builds include OPcache. To confirm, check for opcache.so in your PHP extensions directory:

sudo find /usr/local/php* -name opcache.so

If missing, install via DNF for system PHP, or rebuild via CustomBuild for DirectAdmin-managed PHP versions:

sudo dnf install php-opcache

For DirectAdmin CustomBuild PHPs:

cd /usr/local/directadmin/custombuild
sudo ./build set opcache yes
sudo ./build php n

2. Enable OPcache in the Correct php.ini

Determine the PHP version and handler in use. For per-domain PHP selector, check DirectAdmin user settings or .htaccess (for Apache) or domain.com.conf (for NGINX).

  • Global (system) PHP: /etc/php.ini or /etc/php.d/10-opcache.ini
  • DirectAdmin CustomBuild PHPs: /usr/local/phpXX/lib/php.ini (replace XX with version, e.g. 81 for PHP 8.1)

Ensure these lines are present and not commented out:

[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.validate_timestamps=1
opcache.save_comments=1

Note: For DirectAdmin with multiple PHP versions, repeat for each version/configuration as required per domain.

3. Restart PHP-FPM Pools

Restarting PHP-FPM is required for config changes. This can cause brief downtime (a few seconds). To minimize impact, restart during maintenance windows if possible.

sudo systemctl restart php-fpm

For CustomBuild PHPs (replace XX with PHP version):

sudo systemctl restart php-fpmXX

Or, for per-user pools:

sudo systemctl reload php-fpmXX

Use reload for a graceful reload if available.

4. Verify OPcache Is Working

  1. Check for OPcache in phpinfo() output.
  2. Use WP-CLI to confirm OPcache status for the running PHP:
wp eval 'var_dump(function_exists("opcache_get_status") && opcache_get_status());'

Alternatively, run:

php -i | grep -i opcache

Look for opcache.enable => On and no errors related to OPcache in the output.

Recommended OPcache Settings for WordPress

Use these proven starting values for most small to mid-sized WordPress sites. Adjust based on memory and site scale:

opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.validate_timestamps=1
opcache.fast_shutdown=1
opcache.save_comments=1
opcache.enable_cli=0
  • opcache.memory_consumption: Increase to 256M+ on large, plugin-heavy sites.
  • opcache.max_accelerated_files: Raise to 20000 for WooCommerce/multisite.
  • opcache.revalidate_freq: 60s is typical; drop to 2–5s for rapid code changes (at cost of more checks).

For per-domain tuning in DirectAdmin, use .user.ini in the document root:

opcache.memory_consumption=256
opcache.max_accelerated_files=20000

DirectAdmin’s NGINX/Apache templates do not require changes for OPcache, but ensure templates do not override php_admin_value or php_flag related to OPcache. If you customize templates, re-check them after DirectAdmin updates.

Checklist: Tuning and Monitoring OPcache

  1. Edit php.ini or .user.ini with new values.
  2. Reload/restart the appropriate PHP-FPM pool.
  3. Monitor OPcache status and WordPress performance.

Testing OPcache Effectiveness

  • WP-CLI: Benchmark command execution before and after tuning:
time wp plugin list
time wp option get siteurl
  • HTTP Benchmarks: Use curl or wrk:
curl -s -w '%{time_total}
' -o /dev/null https://example.com/
wrk -t2 -c20 -d20s https://example.com/
  • Monitor PHP-FPM and web server logs for errors:
sudo tail -f /var/log/php-fpm/error.log
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/httpd/error_log
  • OPcache Status: Optionally use a secure OPcache dashboard (like opcache-gui), but never expose this to the public internet.

Key OPcache Parameters: What to Watch

  • opcache.memory_consumption: Check used_memory vs free_memory in status output. If nearly full, increase.
  • opcache.max_accelerated_files: Review num_cached_scripts and max_cached_keys. If at limit, increase.
  • opcache.hit_rate: Should be >99% in normal operation.

Advanced: Per-Domain and Per-PHP Pool Tuning in DirectAdmin

With DirectAdmin, you may have multiple PHP versions in use. Tune OPcache for each pool by:

  1. Editing php.ini for each PHP version in /usr/local/phpXX/lib/php.ini
  2. For per-domain overrides, place a .user.ini in the site’s public_html or document root.

After per-domain changes, reload the affected PHP-FPM pool only, minimizing disruption:

sudo systemctl reload php-fpm81

Always test after changes as above.

Common OPcache Pitfalls on DirectAdmin

  • Not restarting the correct PHP-FPM pool: Changes won’t take effect.
  • Running out of cache memory: Leads to cache purges and poor performance. Monitor and adjust.
  • Disabling OPcache for CLI: opcache.enable_cli=0 is recommended to avoid cache confusion during CLI scripts (e.g., WP-CLI, cron jobs).
  • Forgetting per-domain overrides: .user.ini can silently override global settings.

OPcache and Security

  • Never expose OPcache status pages to the public internet.
  • Restrict access to monitoring scripts via IP or HTTP authentication.

Summary: Steps for Reliable OPcache Tuning with DirectAdmin

  1. Confirm OPcache is installed and enabled for all PHP versions in use.
  2. Set good defaults in each php.ini; use .user.ini for per-domain tuning.
  3. Restart/reload PHP-FPM pools as needed (be aware of brief downtime).
  4. Test WordPress and PHP performance with WP-CLI and HTTP benchmarking tools.
  5. Monitor OPcache usage and hit rate, adjust as site usage grows.

Note: This article offers general technical guidance. Validate all configurations in a safe environment before applying them to production.

Previous: PHP-FPM vs mod_php: Which Is Faster for WordPress?

Next: Why Your WordPress Site Is Slow Even With OPcache Enabled

Smart reads for curious minds

We don’t spam! Read more in our privacy policy