Performance & Maintenance

How to Enable PHP OPcache to Speed Up WordPress

Enable PHP OPcache in minutes to dramatically reduce WordPress load times by caching compiled bytecode in memory.

Overhead view of a laptop showing data visualizations and charts on its screen.

OPcache is the fastest, free way to speed up WordPress: it stores compiled PHP bytecode in memory so every request skips recompiling the same files repeatedly. Enabling it can cut your server response time (TTFB) by 30–70 % without touching a single line of your theme.

This guide walks you through enabling OPcache in PHP, the best configuration values for WordPress, and how to confirm it's running.

What Is OPcache and Why Does It Matter for WordPress?

PHP is an interpreted language. By default, every time a visitor loads your page, the server reads each PHP file, parses it, and compiles it to bytecode before execution. WordPress ships with hundreds of PHP files — that cycle repeats on every single request.

OPcache breaks that cycle. It compiles files once, stores the result in RAM, and reuses it for subsequent requests. The benefits are real:

  • Lower CPU usage per request.
  • Faster TTFB, which directly improves LCP scores and user experience.
  • Higher concurrent capacity without hardware upgrades.

OPcache ships with PHP 5.5+ and is available on virtually every modern hosting plan. The catch: it is not always enabled by default.

Step 1 — Check Whether OPcache Is Already Active

Before editing any file, check the current state. Create a temporary file at your WordPress root:

<?php phpinfo(); ?>

Name it info.php, open it in a browser (yourdomain.com/info.php), and search for the Zend OPcache section. If it appears with status enabled, OPcache is already running — skip to Step 3 to review the configuration. If it is missing or says disabled, continue below.

Important: delete info.php as soon as you finish. Never leave that file on a production server.

Step 2 — Enable OPcache for Your Environment

On cPanel (shared hosting)

Most modern control panels let you choose your PHP version and extensions from the UI:

  1. Log in to cPanel → MultiPHP Manager or PHP Selector.
  2. Select the PHP version for your domain (recommended: PHP 8.1 or 8.2).
  3. Go to PHP Extensions and check opcache.
  4. Save. The change takes effect immediately — no Apache restart required.

On a VPS or Dedicated Server (php.ini)

Find your active php.ini with:

php --ini | grep "Loaded Configuration"

Open that file and add or uncomment these lines:

zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=0

Then restart PHP-FPM or Apache:

sudo systemctl restart php8.2-fpm
# or
sudo systemctl restart apache2

Via .user.ini (shared hosting without php.ini access)

If your host uses per-directory .user.ini, place this file at your WordPress root:

opcache.enable=1

Note: .user.ini cannot load extensions (zend_extension). The extension must already be enabled at the server level. Use .user.ini only to tune values, not to activate the extension from scratch.

Once OPcache is active, the defaults are conservative. These values maximize performance for a typical WordPress site:

DirectiveRecommended ValueWhat It Controls
opcache.memory_consumption128RAM reserved for the cache (MB)
opcache.interned_strings_buffer16Internal string buffer (MB)
opcache.max_accelerated_files10000Maximum files in cache
opcache.revalidate_freq60Seconds between file-change checks
opcache.validate_timestamps1Detect changed files (keep 1 in development)
opcache.fast_shutdown1Release memory faster on shutdown
opcache.save_comments1Required by WordPress (phpdoc annotations)

Add these lines to the OPcache block in your php.ini or .user.ini.

On low-RAM shared hosting, drop memory_consumption to 64. On plugin-heavy sites (50+ plugins), raise max_accelerated_files to 20 000.

Step 4 — Verify OPcache Is Working

Reload phpinfo() and look for the Zend OPcache section. It should read:

  • Opcode Caching: Up and Running
  • The configuration values you just set.

For a richer dashboard, install the free OPcache Manager plugin by Pierre Lannoy. It displays hit rate, memory usage, and real-time statistics directly inside your WordPress admin.

A hit rate above 90 % means OPcache is working well. If it is lower, increase memory_consumption or max_accelerated_files.

For more optimization techniques, browse the performance category or get hands-on help from the team at elenlace.com, who audit PHP stacks for WordPress sites in production.

Key Takeaways

  • OPcache stores compiled PHP bytecode in RAM, eliminating recompilation on every request.
  • On cPanel, enable it from the PHP Selector; on VPS, edit php.ini and restart PHP-FPM.
  • Critical WordPress settings: memory_consumption (128 MB), max_accelerated_files (10 000), and save_comments=1.
  • A cache hit rate above 90 % confirms your configuration is correct.
  • Always delete info.php after verifying — never expose it in production.

Still seeing a slow WordPress site after enabling OPcache? There may be database bottlenecks, unoptimized images, or redundant plugins dragging performance down. Reach out to elenlace.com for a full performance audit.

FAQ

Does OPcache replace a caching plugin like WP Rocket?

No. OPcache caches PHP bytecode at the server layer; page caching plugins generate static HTML for anonymous visitors. They are complementary: OPcache speeds up PHP execution, while WP Rocket or W3 Total Cache reduce how often PHP runs at all.

Do I need to clear OPcache when updating WordPress or a plugin?

With validate_timestamps=1 and revalidate_freq=60, OPcache auto-detects modified files within 60 seconds. For immediate invalidation after an update, use the OPcache Manager plugin or call opcache_reset() from a script.

Does OPcache work with both PHP-FPM and mod_php?

Yes. OPcache is a Zend extension that works with any PHP SAPI — mod_php, PHP-FPM, FastCGI, and so on. The extension is always loaded via zend_extension=opcache.so in the php.ini corresponding to your SAPI.

How much RAM does OPcache consume?

With the recommended 128 MB setting, that is the maximum it can reserve. In practice, a WordPress site with 30–50 plugins typically uses 32–80 MB. If your server has less than 512 MB total RAM, lower memory_consumption to 64 MB.

Compare providers

Other providers and guides worth comparing:

← All