OPcache setup guide PHP 8.3 users on Ubuntu should read before they pay for a bigger VPS, because the biggest PHP speed win on a small server is often free. OPcache stores compiled PHP scripts in shared memory, so the server skips parsing and compiling on every request. The first request compiles the script, and every request after that runs the compiled version. On a WordPress site the difference is easy to feel: pages that took half a second to build now take a fraction of that, and the CPU has room for real work instead of recompiling the same files.
OPcache ships with PHP 8.3 and is usually enabled by default on Ubuntu, but the default settings are conservative. They fit a shared host with hundreds of small sites, not a VPS that runs one or two apps. This guide checks the current setup, tunes the memory and revalidation settings for a 2 GB VPS, and verifies the result with real numbers.
OPcache Setup Guide PHP 8.3: Check What Is Running
First, confirm OPcache is enabled and see its current settings:
php -i | grep -i opcache
php -r "print_r(opcache_get_status(false));" | head -30
The first command prints the OPcache section of the PHP info. Look for Opcode Caching => Enabled and the opcache.enable line. If it says disabled, the guide below turns it on. The second command prints the live status: memory used, hit rate, and how many scripts are cached.
On Ubuntu 24.04 with PHP 8.3, the OPcache config lives in /etc/php/8.3/fpm/conf.d/10-opcache.ini for PHP-FPM and /etc/php/8.3/cli/conf.d/10-opcache.ini for the command line. The CLI config does not matter much, because CLI scripts run once and exit, but the FPM config decides how the website performs. Edit the FPM file:
sudo nano /etc/php/8.3/fpm/conf.d/10-opcache.ini
Before you change anything, note the current hit rate from the status command. A healthy OPcache shows a hit rate above 95 percent after a warm-up period. Below that, scripts are being evicted or the cache is too small, and the settings below fix both.
Memory Settings That Fit a 2 GB VPS
OPcache memory is shared memory, so it competes with PHP-FPM workers and the database for the same 2 GB. The default of 128 MB is enough for a small WordPress site, but a site with many plugins or a Laravel app with a large vendor folder can outgrow it. When the cache fills up, OPcache evicts scripts and the hit rate drops, which is the symptom to watch.
For a 2 GB VPS running one WordPress site, these values are a solid starting point:
opcache.enable=1
opcache.memory_consumption=192
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.validate_timestamps=1
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.memory_consumption at 192 MB leaves room for PHP-FPM workers and the database. opcache.max_accelerated_files at 10000 covers a WordPress install with a healthy plugin list; a big Laravel app may need 20000. opcache.interned_strings_buffer at 16 MB holds the repeated strings from scripts, which saves memory across workers.
Do not raise the memory blindly. Every megabyte OPcache takes is one a PHP worker cannot use, and the right number of workers matters as much as the cache size. This guide to PHP-FPM tuning and pm.max_children shows how to size the worker pool on the same 2 GB budget, and the two settings have to be balanced together. A related walkthrough of OPcache settings for WordPress and PHP on Ubuntu 24.04 covers the same ground with a WordPress focus.
Revalidate Frequency and When to Turn Validation Off
opcache.validate_timestamps decides whether OPcache checks if a script file changed. With it on, OPcache rechecks the file every revalidate_freq seconds, which is 60 seconds in the config above. That means a code change takes up to a minute to appear, which is fine for most sites. With it off, OPcache never rechecks, and a code change does not appear until you restart PHP-FPM or clear the cache manually.
Turning validation off is faster, because OPcache skips the file check on every request. It is the right choice for a production site that deploys code on a schedule: deploy, then restart PHP-FPM or run opcache_reset() as part of the deploy. It is the wrong choice for a site where you edit files directly on the server, because your changes will not appear and you will spend an hour debugging a fix that is already on disk.
For a small VPS with one app, keep validation on and set revalidate_freq to 60 seconds. The performance cost of a check once a minute is tiny, and the safety of seeing your edits is worth it. If you deploy with a script, turn validation off in production and add the cache reset to the deploy steps.
Verify the Hit Rate with opcache_get_status()
Restart PHP-FPM after editing, then let the site run for an hour before you judge the settings:
sudo systemctl restart php8.3-fpm
php -r "print_r(opcache_get_status(false));"
Run the status command again after the warm-up period and look at three numbers. The hit rate should be above 95 percent. Memory usage should sit comfortably below the limit you set, not pressed against it. The number of cached scripts should be below max_accelerated_files, with room to spare. If the hit rate is low and memory is full, raise memory_consumption by 64 MB and check again. If the hit rate is low and memory is half empty, the problem is elsewhere, usually a plugin that generates code on the fly or a file that changes constantly.
A small script makes this check easy to repeat. Save it outside the web root and run it from the command line:
<?php
$s = opcache_get_status(false);
printf("Hit rate: %.2f%%n", $s['opcache_statistics']['hits'] / ($s['opcache_statistics']['hits'] + $s['opcache_statistics']['misses']) * 100);
printf("Memory used: %.1f MB of %.1f MBn", $s['memory_usage']['used_memory'] / 1024 / 1024, ($s['memory_usage']['used_memory'] + $s['memory_usage']['free_memory']) / 1024 / 1024);
printf("Cached scripts: %dn", $s['opcache_statistics']['num_cached_scripts']);
Run it weekly for a month after you tune. A cache that slowly fills up as you add plugins will show up here before it shows up as a slow site, and catching it early is a one-line config change instead of a debugging session.
What OPcache Does Not Fix
OPcache makes PHP faster, but it does not fix a slow database, a bad query, or a server that is out of memory. If pages are slow after OPcache is tuned, the bottleneck is somewhere else. A slow query shows up in the database logs, a memory problem shows up in free -h, and a full disk shows up in df -h. OPcache is one layer of a fast stack, not a replacement for the others.
Also remember that OPcache caches compiled code, not results. A page that runs the same query a hundred times still runs that query a hundred times, just faster. Caching the query result is a different job, and it belongs to Redis or the database cache. This guide to installing WordPress on a LEMP stack with Nginx, PHP, and MariaDB shows the full stack these settings fit into, and this walkthrough of reducing WordPress TTFB below 200 ms on a cheap VPS covers the other layers that decide how fast a page feels. Tune OPcache first, because it is free and it takes ten minutes, then chase the next bottleneck with real numbers instead of guesses.