OPcache Settings for WordPress: How to Tune PHP OPcache on Ubuntu 24.04

PHP compiles every WordPress script to bytecode on each request, unless OPcache holds the compiled version in memory. Without OPcache, a busy site pays that compile cost on every page load. With it, PHP runs the stored bytecode and skips the compile step. OPcache settings WordPress Ubuntu tuning is therefore one of the cheapest speed wins on a self-managed VPS: no new software, no plugin, just a few lines in php.ini. Ubuntu 24.04 ships PHP 8.3 with OPcache built in, but the defaults are conservative, so a WordPress site with many plugins can outgrow them fast.

OPcache caches compiled PHP code only. It does not cache database queries, and it does not cache finished HTML pages. Those jobs belong to an object cache and a page cache. If you are still building the server itself, follow our guide to installing WordPress on a LEMP stack with Nginx, PHP, and MariaDB first, then come back here to tune OPcache on top of a working stack.

What OPcache Does for WordPress

Every time PHP runs a script, it reads the file, parses it, and compiles it to bytecode. OPcache stores that bytecode in shared memory. The next request for the same file skips the read, parse, and compile steps entirely. On a WordPress site, where each page load runs dozens of PHP files from core, themes, and plugins, that saving adds up on every single request.

OPcache also validates file timestamps by default. When you update a plugin or edit a theme file, OPcache notices the changed timestamp and recompiles the file. That check costs a little speed, but it keeps your site safe from serving stale code after an update. The tuning below keeps that safety on while raising the limits that WordPress sites actually hit.

OPcache Settings WordPress Ubuntu: The Values to Change

Ubuntu keeps separate php.ini files for the command line and for PHP-FPM. WordPress runs under PHP-FPM, so edit the FPM file, not the CLI one. The values below form a php opcache configuration built for a standard opcache ubuntu 24.04 setup running PHP 8.3 under FPM. For PHP 8.3 on Ubuntu 24.04, the path is /etc/php/8.3/fpm/php.ini. Find the OPcache section and set these values:

opcache.enable=1
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.enable_cli=0

What each value does: memory_consumption sets the shared memory pool in megabytes, and 128 MB covers most WordPress sites. Raise it to 256 MB if you run a large plugin set and the cache keeps filling up. interned_strings_buffer holds repeated strings such as class and function names, and 16 MB is a sane start. max_accelerated_files caps how many scripts OPcache tracks; WordPress core plus a typical plugin set stays under 10,000, but a heavy site may need 20,000. revalidate_freq sets how often OPcache checks file timestamps, in seconds, and 60 keeps updates visible within a minute while skipping the check on most requests. save_comments must stay on, because some plugins and frameworks read doc comments at runtime. enable_cli stays off, since command-line scripts run once and gain nothing from caching.

RAM for the cache comes out of the same pool as everything else on the VPS, so size it against the whole server. Our guide on how much RAM a WordPress VPS needs and the guide to choosing VPS specs for RAM, CPU, and storage help you work out how much headroom you have before you raise the memory pool.

How to Edit php.ini Without Breaking PHP

Back up the file before you touch it. One typo in php.ini can stop PHP-FPM from starting, and a backup turns that into a one-command fix. Run these commands over SSH:

sudo cp /etc/php/8.3/fpm/php.ini /etc/php/8.3/fpm/php.ini.bak
sudo nano /etc/php/8.3/fpm/php.ini

Find each opcache line, change the value, and save. If a line starts with a semicolon, remove the semicolon, or the setting stays commented out. After you save, restart PHP-FPM so it reads the new file:

sudo systemctl restart php8.3-fpm
sudo systemctl status php8.3-fpm

If the status shows a failure, restore the backup and restart again:

sudo cp /etc/php/8.3/fpm/php.ini.bak /etc/php/8.3/fpm/php.ini
sudo systemctl restart php8.3-fpm

Warning: never edit php.ini on a live site without a backup, and never restart PHP-FPM during a traffic spike if you can avoid it. The restart drops the whole OPcache, so the first requests after it run slower while the cache warms up again. Do this work in a quiet window.

Verify OPcache Is Working

Check the active settings from the command line first:

php -i | grep -i opcache

That shows the CLI configuration, which is a useful sanity check but not proof for the web server. For proof, create a small status file in your web root, load it once in a browser, read the numbers, then delete the file right away. Leaving a status page public tells attackers exactly how your PHP is configured.

<?php
print_r(opcache_get_status());
?>

Look for two things: opcache_enabled set to true, and memory_usage showing used memory well below the full pool. If used memory sits near 100 percent, raise memory_consumption or max_accelerated_files, restart PHP-FPM, and check again. A full cache forces OPcache to restart itself, which wipes the speed gain.

Mistakes That Break WordPress After Tuning

  • Setting validate_timestamps to 0 without a cache reset step in your deploy flow. Plugin updates then serve stale code until you restart PHP-FPM by hand. Keep it at 1 unless your deploys call opcache_reset for you.
  • Editing the CLI php.ini at /etc/php/8.3/cli/php.ini and wondering why nothing changed. WordPress runs under FPM, so only the FPM file matters.
  • Forgetting to restart PHP-FPM after saving. PHP-FPM reads php.ini once at startup, so edits sit idle until the restart.
  • Setting memory_consumption higher than the free RAM on the VPS. OPcache grabs that memory from the same pool as MySQL, Nginx, and the page cache, so an oversized pool starves the rest of the stack.

When to Leave the Defaults Alone

A low-traffic site with a handful of plugins rarely hits the default limits, so tuning buys little. A site under active development, where theme and plugin files change every hour, also gains little from aggressive revalidate settings, since OPcache must recheck files constantly anyway. And if your host manages PHP for you, you may not have access to php.ini at all, in which case a caching plugin is the safer lever. For context on how the web server choice interacts with PHP performance, see the LAMP vs LEMP comparison for WordPress hosting.

OPcache is one layer of wordpress php performance tuning, not the whole stack. Once the settings above are live and verified, watch the cache for a week. If memory use stays under 80 percent and updates appear within a minute, the tuning is done. Revisit it only when you add a large plugin set or move to a bigger VPS.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.