Skip to content

cPanel OpCache settings: size the four that matter

Size OpCache on a cPanel ea-php83 or other EasyApache 4 PHP version from live cache statistics, apply the change safely, and recover if PHP-FPM goes down.

Four OpCache directives change measured response time on a cPanel server, and the rest make no difference on a web workload. They are opcache.memory_consumption, opcache.max_accelerated_files, opcache.interned_strings_buffer, and opcache.validate_timestamps with its companion opcache.revalidate_freq. The hard part is choosing the numbers, because most values in circulation were copied from a post about somebody else’s server. This article works the other way round. You read the cache’s own counters, let them show which directive is running short, change that one, restart the PHP-FPM pool, and read the counters again. It also covers the WHM alert that follows a bad value, The service "apache_php_fpm" appears to be down, and how to bring the pool back.

What you’ll accomplish

  • Find the ini file your cPanel PHP version actually parses, instead of editing one that is never read.
  • Read live OpCache statistics from inside the PHP-FPM pool, which is the only place they are real.
  • Size max_accelerated_files from the site’s actual .php file count, and memory_consumption from observed usage.
  • Tell an out-of-memory restart apart from a hash-table restart, because they need different fixes.
  • Recover a PHP-FPM pool that refuses to start after an ini change, using the log line that names the cause.

Prerequisites

  • A cPanel or WHM server with EasyApache 4 and root shell access. The ini files live outside any account’s home directory, so a cPanel user cannot make these changes.
  • PHP 8.1 through 8.4 installed through MultiPHP, with the matching ea-php8X-php-opcache package. If php -i prints no opcache.* line, the extension is not loaded and ini editing will not change that.
  • PHP-FPM as the handler. The worker lives across requests, so the opcode cache persists and sizing it matters. Under suPHP or CGI a process is created and destroyed per request, the cache starts empty every time, and tuning it accomplishes nothing — switching handler is the real fix there. Under LiteSpeed with mod_lsapi the lsphp process also persists and the sizing logic applies unchanged, but LiteSpeed restarts its own children rather than an ea-phpXX-php-fpm unit.
  • Per-version ini paths. Every EasyApache PHP version has its own tree under /opt/cpanel/ea-php8X/, so editing PHP 8.1’s file changes nothing for an account on 8.3. CloudLinux PHP Selector accounts run alt-php builds under /opt/alt/ — a separate tree again, managed per user with selectorctl.
  • A low-traffic window: every change below ends in a PHP-FPM restart, which drops the warm cache for every site on the server.

Throughout, ea-php83 stands for the version you are tuning and USER for the cPanel account. Substitute both.

How it works

Without OpCache, every request reads the PHP source off disk, tokenises it, parses it and compiles it to Zend opcodes before a single line runs. OpCache does that once and keeps the opcodes in a shared memory segment attached to the PHP-FPM master, so later requests skip straight to execution.

Two request paths side by side: a cold path paying read, compile, execute and database time, and a warm path where only the compile segment is gone

The segment has two hard limits and a third buffer inside it. Running out of any of them silently returns you to the cold path.

The first is memory. opcache.memory_consumption sets the size of the shared segment in megabytes; the default is 128. When it fills, OpCache does not evict the least-used script — it has no LRU. It schedules a restart, discards the whole cache, and recompiles everything from nothing. The counter is oom_restarts, and the symptom is latency that drifts up through the day and resets whenever somebody restarts PHP.

The second is the script hash table. opcache.max_accelerated_files caps how many distinct files the cache can hold; the default is 10000. This is where WordPress overflows. A bare install is roughly 1,200 PHP files; add WooCommerce and its dependencies and you are near 5,000; add a page builder, a security plugin, an SEO plugin, a caching plugin and a theme with its own vendor/ directory, and one site clears 12,000 with nothing unusual going on. Now put four such sites on one account. When the table fills, OpCache flushes and rebuilds exactly as it does for memory, and the counter is hash_restarts.

The third pressure is interned strings. PHP deduplicates every class name, function name and literal string into a shared buffer sized by opcache.interned_strings_buffer, default 8 MB. Large plugin trees exhaust it quietly: when the buffer is full, strings simply stop being interned, nothing errors, memory use climbs, and the hit rate sags for no visible reason.

opcache_get_status() reports all of it. memory_usage gives used_memory, free_memory and wasted_memory; opcache_statistics gives num_cached_scripts, max_cached_keys, hits, misses, opcache_hit_rate, oom_restarts and hash_restarts; interned_strings_usage gives that buffer’s own used and free bytes. Between them they name which of the three limits you are actually against.

Decision tree from the OpCache status array through oom_restarts, hash_restarts, script count and miss rate to the specific directive to raise

What ea-php83 means on cPanel

ea-php83 is EasyApache 4’s package name for PHP 8.3; ea-php82 is PHP 8.2, and so on. A cPanel server can run several versions side by side, each in its own tree under /opt/cpanel/. The ea-php83 tree keeps its php.ini in /opt/cpanel/ea-php83/root/etc/, extra ini files such as 10-opcache.ini in the php.d directory beside it, and its own PHP binary at /opt/cpanel/ea-php83/root/usr/bin/php. OpCache is therefore set per PHP version: a change in the ea-php83 tree does nothing for a site running ea-php82. Check which version a site uses in WHM’s MultiPHP Manager first. The commands below use ea-php83, so swap in your version where it differs.

Step-by-step

  1. Find the ini files this PHP version actually loads. Ask PHP rather than assuming a path.

    /opt/cpanel/ea-php83/root/usr/bin/php --ini

    The output names the parse order, and the last line is the part that matters:

    Configuration File (php.ini) Path: /opt/cpanel/ea-php83/root/etc
    Loaded Configuration File:         /opt/cpanel/ea-php83/root/etc/php.ini
    Scan for additional .ini files in: /opt/cpanel/ea-php83/root/etc/php.d
    Additional .ini files parsed:      /opt/cpanel/ea-php83/root/etc/php.d/10-opcache.ini

    Files in the scan directory are parsed after php.ini, alphabetically, and the last assignment wins. That is why a change made in php.ini through WHM’s MultiPHP INI Editor can appear to do nothing: 10-opcache.ini is parsed afterwards and sets the directive again.

  2. Record what is in effect today. You need this to compare against later, and to roll back to.

    /opt/cpanel/ea-php83/root/usr/bin/php -i | grep -E '^opcache\.(enable|memory_consumption|interned_strings_buffer|max_accelerated_files|validate_timestamps|revalidate_freq)'

    A stock EasyApache 4 PHP 8.3 install prints close to this:

    opcache.enable => On => On
    opcache.interned_strings_buffer => 8 => 8
    opcache.max_accelerated_files => 10000 => 10000
    opcache.memory_consumption => 128 => 128
    opcache.revalidate_freq => 2 => 2
    opcache.validate_timestamps => On => On
  3. Count the PHP files the site actually has. This is the input to max_accelerated_files, and it is usually a surprise.

    find /home/USER/public_html -type f -name '*.php' | wc -l

    A number like 14732 on a WooCommerce store is normal, and it is already above the 10000 default. Count every document root on the account, not just the first:

    find /home/USER -maxdepth 3 -type d -name public_html -exec sh -c 'printf "%s " "$1"; find "$1" -type f -name "*.php" | wc -l' _ {} \;

    Sum the results and add roughly 30 percent of headroom for the plugins that will be installed next quarter. That total is your target.

  4. Read the live cache from inside the pool. Most guides skip this, which is why so many servers are tuned blind. opcache_get_status() run from the command line reports the CLI process’s own cache — a fresh, usually disabled one — not the pool serving your traffic. Ask from inside the pool instead, with a short-lived script you delete in the same sitting: a status page left in a document root advertises your file paths, PHP version and full cached script list to anyone who finds it.

    cd /home/USER/public_html
    NAME="oc-$(openssl rand -hex 12).php"
    cat > "$NAME" <<'PHP'
    <?php
    header('Content-Type: text/plain');
    $s = opcache_get_status(false);
    if ($s === false) { exit("OpCache is not active in this SAPI\n"); }
    $m = $s['memory_usage']; $t = $s['opcache_statistics']; $i = $s['interned_strings_usage'];
    printf("hit_rate           %.2f%%\n", $t['opcache_hit_rate']);
    printf("scripts            %d of %d keys\n", $t['num_cached_scripts'], $t['max_cached_keys']);
    printf("misses             %d\n", $t['misses']);
    printf("oom_restarts       %d\n", $t['oom_restarts']);
    printf("hash_restarts      %d\n", $t['hash_restarts']);
    printf("used / free / waste %d / %d / %d\n", $m['used_memory'], $m['free_memory'], $m['wasted_memory']);
    printf("interned free      %d of %d\n", $i['free_memory'], $i['buffer_size']);
    PHP
    chown USER:USER "$NAME"
    curl -s "https://example.com/$NAME"
    rm -f "$NAME"

    On an undersized pool that has been serving traffic for a few hours, the output tells the story plainly:

    hit_rate           88.41%
    scripts            16229 of 16229 keys
    misses             412903
    oom_restarts       0
    hash_restarts      37
    used / free / waste 108216312 / 3457928 / 22588416
    interned free      12480 of 8388608
  5. Read the counters in order and change one directive. oom_restarts above 0: the segment is too small, raise opcache.memory_consumption. hash_restarts above 0, or num_cached_scripts sitting at max_cached_keys as it does above: the file ceiling is too low, raise opcache.max_accelerated_files. interned free down in the low thousands: opcache.interned_strings_buffer is exhausted. High misses with both restart counters flat is not a sizing problem at all — that is timestamp revalidation, or a deploy that just invalidated everything.

  6. Write the values into a file package updates will not overwrite. Do not edit 10-opcache.ini; an ea-php83-php-opcache update replaces it and silently reverts you. Create a higher-numbered file in the same scan directory, which is parsed later and therefore wins.

    cat > /opt/cpanel/ea-php83/root/etc/php.d/99-opcache-tuning.ini <<'INI'
    opcache.memory_consumption=256
    opcache.max_accelerated_files=32531
    opcache.interned_strings_buffer=32
    opcache.validate_timestamps=1
    opcache.revalidate_freq=60
    INI
    /opt/cpanel/ea-php83/root/usr/bin/php --ini | tail -1

    The tail must now list your file after 10-opcache.ini. Two notes on the values. max_accelerated_files is rounded up internally to the next entry in a fixed table of primes — 16229, 32531, 65407 — so asking for 20000 gives you 32531; pick the prime directly and there is no surprise. And memory_consumption is per PHP-FPM master, not per pool and not per server. cPanel runs one ea-phpXX-php-fpm master per PHP version, and OpCache allocates a single shared segment in that master which every account pool on that version then shares. So 256 MB is 256 MB for all of them together — which is the real argument for sizing it generously, but also means one busy account can evict another’s scripts. What does multiply is versions: 8.1, 8.2, 8.3 and 8.4 each running a master at 256 MB reserves a gigabyte. Check which versions are actually in use before raising the value on all of them.

  7. Restart PHP-FPM the cPanel way. cPanel manages the FPM services through its own wrapper, and using it keeps the WHM service monitor in step with reality.

    /usr/local/cpanel/scripts/restartsrv_apache_php_fpm

    Success looks like this, with a fresh PID:

    Waiting for “apache_php_fpm” to restart ……waiting for “apache_php_fpm” to initialize ………finished.
    
    apache_php_fpm (/opt/cpanel/ea-php83/root/usr/sbin/php-fpm) is running as root with PID 214093 (systemd+/proc check method).

    /scripts/restartsrv_apache_php_fpm is a symlink to the same script. To touch a single version rather than all of them, systemctl reload ea-php83-php-fpm re-reads the configuration while keeping the master alive — though a reload still recycles workers, so the cache still starts cold.

  8. Confirm the directives took, then let real traffic warm the cache. Configuration state is readable from the CLI even though live statistics are not.

    /opt/cpanel/ea-php83/root/usr/bin/php -i | grep -E '^opcache\.(memory_consumption|max_accelerated_files|interned_strings_buffer|revalidate_freq)'
    opcache.interned_strings_buffer => 32 => 32
    opcache.max_accelerated_files => 32531 => 32531
    opcache.memory_consumption => 256 => 256
    opcache.revalidate_freq => 60 => 60

    Then wait. An hour of ordinary traffic is the minimum before the numbers mean anything; a full business day is better, because the tail of rarely-hit admin pages fills the last of the cache.

Validate it works

Re-run the drop-in script from step 4 after that day of traffic. The proof is not one good reading — it is counters that stopped moving:

hit_rate           99.12%
scripts            18904 of 32531 keys
misses             2119
oom_restarts       0
hash_restarts      0
used / free / waste 178429640 / 89158904 / 1846232
interned free      19204488 of 33554432

Both restart counters at 0, script count comfortably under the key ceiling, free memory in the tens of megabytes rather than near zero, and a hit rate in the high nineties. wasted_memory should stay a small fraction of the segment; if it grows toward free_memory over a week, the cache is fragmenting from repeated invalidation.

Then measure what the customer feels — same URL, same time of day, cache warm on both sides:

curl -s -o /dev/null -w 'connect %{time_connect}s ttfb %{time_starttransfer}s total %{time_total}s\n' https://example.com/shop/

Run it ten times and compare medians, not single shots. On a site that was genuinely thrashing its cache the improvement is usually tens of milliseconds of TTFB on an uncached page — sometimes considerably more, sometimes very little. If the two medians differ by less than the run-to-run spread, OpCache was not your bottleneck, and that is a result worth having.

Troubleshooting

oom_restarts climbing. The segment is too small for the code in it. Raise opcache.memory_consumption one step — 128 to 192, 192 to 256 — restart, and check again after a day. Do not jump straight to 512: the segment is reserved at master start whether it is used or not, and it is reserved once per PHP version you have running, so the cost lands several times over on a server offering four of them. If used_memory settles well below the new ceiling and the counter stays at 0, you are done.

hash_restarts climbing, or num_cached_scripts pinned to max_cached_keys. The file ceiling is too low. Raise opcache.max_accelerated_files to the next prime above your counted file total plus headroom, remembering that PHP rounds up to that table on its own — so max_cached_keys reading 32531 when you asked for 20000 is correct, not a bug.

The ini was edited and nothing changed. Either you edited the wrong PHP version’s tree — the account runs 8.2 and you tuned 8.3, or it is a CloudLinux alt-php account for which the EasyApache tree is irrelevant. Confirm with whmapi1 php_get_vhost_versions | grep -A2 example.com. Or the file you edited is parsed before another that sets the same directive; re-read the --ini output from step 1 and edit the later one.

Deploys stop appearing on the site. Someone set opcache.validate_timestamps=0. That is right for an immutable deployment that reloads the pool as part of every release, and actively harmful anywhere a client edits a theme file through File Manager, because cached opcodes are then served forever. With human editors, keep it at 1 and control the cost with opcache.revalidate_freq: 60 checks a file’s mtime at most once a minute per process. If you do want 0, the release process must end in a PHP-FPM reload; there is no middle ground.

WHM alerts that the service “apache_php_fpm” appears to be down. A directive PHP-FPM cannot accept stops the master starting at all, and cPanel’s chkservd notices within a couple of minutes. The usual causes are an opcache.memory_consumption larger than the machine can map, or an opcache.interned_strings_buffer set larger than the segment that has to contain it. Find the reason before restarting anything:

systemctl status ea-php83-php-fpm --no-pager
journalctl -u ea-php83-php-fpm --since '20 min ago' --no-pager | tail -30
tail -40 /opt/cpanel/ea-php83/root/usr/var/log/php-fpm/error.log

A failed OpCache allocation names itself:

Unable to allocate shared memory segment of 8589934592 bytes: mmap: Cannot allocate memory
Failed to create/attach to shared memory segment

Recover by removing your file rather than editing it under pressure, then bring the service back and start again with a smaller value:

mv /opt/cpanel/ea-php83/root/etc/php.d/99-opcache-tuning.ini /root/99-opcache-tuning.ini.bad
/usr/local/cpanel/scripts/restartsrv_apache_php_fpm
/usr/local/cpanel/scripts/restartsrv_apache_php_fpm --status

The --status line should report the service running with a PID. If PHP-FPM starts cleanly once the file is gone, the file is the fault, and the diff against your known-good values is a very short list.

When this is the wrong approach

OpCache caches compiled code. It does not cache query results, and it has no effect whatsoever on an unindexed wp_postmeta lookup, a bloated autoloaded wp_options set, or a plugin making a synchronous call to a payment gateway on every page render. If your TTFB is 2.4 seconds, opcode compilation is not where 2.4 seconds went — turn on the PHP-FPM slowlog and find out where it actually went before touching an ini file.

It is also the wrong lever when there is no page cache in front of PHP. A full-page cache hit returns HTML without PHP running at all, removing the entire PHP segment of the diagram above rather than trimming part of it — for an anonymous-traffic content site that is a categorically larger change than any OpCache setting. And it is wrong on suPHP or CGI accounts, where the process dies after every request and the cache never survives to be used.

If the counters are already flat and the hit rate is already above 98 percent, OpCache is done and further tuning is superstition. The next question is whether opcode execution is even the cost — opcache.jit on cPanel PHP 8 covers that decision and why it usually answers no. For the directives themselves, the PHP manual’s OpCache section is the authority, and cPanel’s documentation covers the MultiPHP and EasyApache 4 layout these paths come from.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.