Web & Development

PHP OPcache: How to Speed Up Your Website

PHP OPcache: How to Speed Up Your Website

Learn how to configure PHP OPcache to speed up your website, reduce CPU overhead, monitor cache performance, deploy safely, and choose suitable PHP hosting.

PHP OPcache is a built-in optimization that stores compiled PHP bytecode in shared memory. Instead of parsing and compiling the same scripts on every request, PHP can reuse cached bytecode. For WordPress, Joomla and custom PHP applications, this can reduce CPU overhead and improve response times.

But simply enabling OPcache is not enough. You also need appropriate memory settings, a safe deployment process, and a hosting environment that exposes the controls your application needs.

This practical guide explains how to verify, configure and monitor OPcache, troubleshoot stale code, and evaluate PHP hosting.

1. What does OPcache actually cache?

PHP compiles source files into opcodes before executing them. OPcache retains the compiled representation for reuse by compatible PHP workers. It is not a full-page cache, database cache, object cache or CDN.

For example, a Joomla request may load dozens of framework and extension files. OPcache reduces repeated compilation of those files, but it cannot eliminate slow SQL queries or external API latency.

2. Check whether OPcache is enabled

On a server with command-line access, run:

php -v
php --ri 'Zend OPcache'

Remember that PHP CLI and the web-serving PHP-FPM process may use different configuration files and separate caches. A CLI result does not establish the website's OPcache status.

To inspect the web runtime, run the following code only in a secured administrative context, then remove it:

<?php

header('Content-Type: application/json');

$status = function_exists('opcache_get_status')
    ? opcache_get_status(false)
    : false;

echo json_encode([
    'phpVersion' => PHP_VERSION,
    'sapi' => PHP_SAPI,
    'opcacheEnabled' => $status['opcache_enabled'] ?? false,
], JSON_PRETTY_PRINT);

Do not leave public diagnostics online: they can disclose information about your server.

3. Configure OPcache for a PHP website

The following settings are an illustrative starting point for a medium-sized application, not universal optimal values:

opcache.enable=1
opcache.memory_consumption=192
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.validate_timestamps=1
opcache.revalidate_freq=2
opcache.save_comments=1
  • memory_consumption allocates shared memory in MB for compiled scripts.
  • interned_strings_buffer reserves memory for interned strings.
  • max_accelerated_files requests script-key capacity; the actual capacity is selected from supported values.
  • validate_timestamps controls checks for modified source files.
  • revalidate_freq controls the interval between those checks when validation is enabled.
  • save_comments preserves comments that some applications and tools rely on.

A small site may need less memory; a large application with many extensions or dependencies may need more. On shared or managed hosting, some directives may only be changed by the provider or through its control panel. Check the effective values rather than assuming a local configuration file overrides them.

4. Avoid outdated PHP code after deployment

Suppose you upload a new version of a plugin while the website continues receiving traffic. Cached bytecode from the previous version can remain in use until OPcache recognizes the changes.

Illustration of the article's main concept

For ordinary websites, keeping timestamp validation enabled is a practical default:

opcache.validate_timestamps=1
opcache.revalidate_freq=2

For carefully controlled releases, some teams disable timestamp checks:

opcache.validate_timestamps=0

Only do this when the deployment process reliably invalidates the correct web-runtime cache or restarts the appropriate PHP workers.

PHP offers opcache_reset(), but invoking it from CLI generally does not clear the PHP-FPM cache serving web requests. Never expose an unauthenticated reset endpoint.

A safer deployment sequence is to prepare a complete release separately, validate its dependencies, apply compatible database migrations, switch the active release, refresh the appropriate PHP runtime if necessary, and run a health check. An atomic release switch prevents mixing files from different versions, but it does not automatically solve every OPcache invalidation scenario.

5. Monitor OPcache memory and cache hits

Use opcache_get_status() from the actual web PHP runtime:

<?php

$status = opcache_get_status(false);

if ($status === false) {
    exit('OPcache is unavailable.');
}

$memory = $status['memory_usage'] ?? [];
$stats = $status['opcache_statistics'] ?? [];

printf("Used memory: %.1f MB\n", ($memory['used_memory'] ?? 0) / 1048576);
printf("Free memory: %.1f MB\n", ($memory['free_memory'] ?? 0) / 1048576);
printf("Hit rate: %.2f%%\n", $stats['opcache_hit_rate'] ?? 0);
printf("Cached scripts: %d\n", $stats['num_cached_scripts'] ?? 0);
printf("Out-of-memory restarts: %d\n", $stats['oom_restarts'] ?? 0);

Run this only in a protected administrative environment. Watch for memory pressure, script-key saturation and unexpected restarts. A high hit rate is encouraging, but real response times and application profiling matter more than a single cache statistic.

6. OPcache for WordPress and Joomla

WordPress loads core, theme and plugin PHP files for dynamic requests. Joomla loads framework, component, module and plugin code. Both can benefit from reusing compiled bytecode.

However, OPcache does not replace full-page caching. It also cannot fix an expensive WooCommerce database query, a slow Joomla extension, or a remote API timeout.

Use OPcache alongside appropriate page caching, efficient SQL, current PHP versions, and measurements of real traffic.

7. How to choose hosting for OPcache

The useful question is not merely whether a provider mentions OPcache. Ask whether it is enabled in production, whether you can inspect its status, which directives can be adjusted, and how PHP workers are refreshed during deployment.

PlanetHoster: versatile PHP hosting

PlanetHoster is an option to evaluate for WordPress, Joomla and custom PHP websites. Its PHP configuration capabilities make it relevant to developers who want general-purpose hosting.

Before choosing a plan, confirm OPcache activation for the specific web runtime, which settings are adjustable on that offer, and the supported restart procedure. Do not assume every hosting plan provides identical server-level controls.

Technical workflow illustration

Some links are affiliate links: CeiKay may earn a commission at no extra cost to you.

View PlanetHoster hosting plans

Cloudways: managed cloud hosting

Cloudways is worth evaluating for growing PHP applications that need managed cloud infrastructure and operational tooling. Review its available PHP/OPcache controls for the selected server stack and how deployments and PHP service changes are handled.

Explore Cloudways hosting

Kinsta: managed WordPress hosting

Kinsta focuses on managed WordPress hosting, rather than general-purpose Joomla hosting. Its caching behavior differs between production and staging environments; staging benchmarks should not be treated as directly equivalent to production without checking the relevant settings.

Which hosting provider should you choose?

Provider Best fit OPcache question to ask
PlanetHoster Joomla, WordPress, general PHP hosting Which settings can I configure on this plan?
Cloudways Managed cloud PHP applications How are PHP settings and cache refreshes managed?
Kinsta Managed WordPress How does production caching differ from staging?

Choose based on the application, available controls, reliability and support—not on a claim that OPcache alone guarantees a faster website.

Affiliate disclosure: CeiKay may earn a commission when readers use an eligible hosting link, at no additional cost to them. The disclosure and actual hosting buttons should be rendered by the site's centralized affiliate system only when at least one eligible CTA is active.

8. Troubleshooting common OPcache problems

My code changes are not visible. Check validate_timestamps, revalidate_freq, release paths and whether the correct PHP workers were refreshed.

OPcache memory is full. Inspect memory usage and restart counters before increasing the memory budget; make sure the server has enough RAM.

CLI says OPcache is disabled. Check the web runtime separately. opcache.enable_cli is distinct from web OPcache.

The website is still slow. Profile SQL, application logic, external calls and page-cache behavior. OPcache removes compilation overhead, not every bottleneck.

Should I enable JIT? PHP JIT can help some computation-heavy workloads, but it is not a guaranteed performance improvement for typical CMS websites. Benchmark your actual application before tuning it.

9. Production checklist

Before launching or updating your PHP website:

  1. Confirm OPcache is active in the web-serving runtime.
  2. Check cache memory and script capacity.
  3. Configure timestamp validation intentionally.
  4. Test deployment and cache refresh procedures.
  5. Avoid mixing PHP files from different releases.
  6. Monitor cache restarts and real request performance.
  7. Verify which OPcache controls your hosting plan actually exposes.

Conclusion

PHP OPcache is a practical first step toward faster PHP execution. The best results come from correct memory allocation, predictable deployment behavior, monitoring, and a hosting environment that supports the controls you need.

For WordPress, Joomla and custom PHP websites, enable OPcache, measure the impact, and optimize the application as a whole. When choosing hosting, prioritize verifiable PHP capabilities over generic performance promises.

Official references