StarDomain

PHP OPcache Configuration

PHP OPcache Configuration

PHP OPcache is a built-in caching engine that dramatically improves PHP performance by storing precompiled script bytecode in shared memory. This eliminates the need for PHP to parse and compile scripts on every request, resulting in significantly faster page load times.

What Is OPcache?

Without OPcache, every PHP request follows this process:

  1. Read the PHP source file from disk
  2. Parse the PHP code into tokens
  3. Compile tokens into bytecode (opcodes)
  4. Execute the bytecode

With OPcache enabled, steps 1-3 are skipped for cached scripts — the cached bytecode is executed directly from memory. This can improve PHP application performance by 2-5x or more.

Checking OPcache Status

To check if OPcache is currently enabled:

  1. Create a file named phpinfo.php in your document root:

```

<?php phpinfo(); ?>

```

  1. Access it via your browser: https://yourdomain.com/phpinfo.php
  2. Search for "OPcache" on the page
  3. Look for opcache.enable — it should say On

Tip: Delete the phpinfo.php file after checking, as it exposes sensitive server information.

Enabling OPcache

Via cPanel (MultiPHP INI Editor)

  1. Log in to cPanel
  2. Navigate to Software > MultiPHP INI Editor
  3. Select the Basic Mode tab
  4. Choose your domain from the dropdown
  5. Find opcache.enable and set it to Enabled
  6. Click Apply

Via DirectAdmin

  1. Log in to DirectAdmin
  2. Navigate to Account Manager > PHP Settings (or CustomBuild for admin)
  3. Enable OPcache for the desired PHP version
  4. Save and apply changes

Via php.ini or .user.ini

If you have access to PHP configuration files, add or modify:

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

For shared hosting, create a .user.ini file in your document root with the settings your hosting plan allows you to override.

For Small Sites (1-2 WordPress sites)

opcache.memory_consumption=64
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60

For Medium Sites (Multiple sites or WooCommerce)

opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60

For Large Sites (High traffic, many plugins)

opcache.memory_consumption=256
opcache.interned_strings_buffer=32
opcache.max_accelerated_files=20000
opcache.revalidate_freq=120

Understanding Key Settings

SettingDescriptionRecommended
memory_consumptionMemory allocated for cached scripts (MB)128
interned_strings_bufferMemory for interned strings (MB)16
max_accelerated_filesMaximum number of files to cache10000
revalidate_freqSeconds between checking for file changes60
validate_timestampsCheck if files have changed (set 0 in production only)1
save_commentsCache PHP docblock comments (required by many frameworks)1

Tip: The max_accelerated_files value is internally rounded up to the next prime number. Common values: 3907, 7963, 10007, 16229.

OPcache and WordPress

WordPress benefits significantly from OPcache. After enabling:

  1. Clear any existing page caches (WP Super Cache, W3 Total Cache, etc.)
  2. Load a few pages to warm up the OPcache
  3. Use a plugin like OPcache Dashboard to monitor cache hit rates
  4. Target a hit rate above 95% for optimal performance

If you see changes not reflecting on your site after updating themes or plugins, the OPcache may be serving old cached versions. Either wait for the revalidate_freq interval or clear the OPcache.

Clearing OPcache

Via PHP Script

Create a temporary file:

<?php opcache_reset(); echo "OPcache cleared"; ?>

Via Command Line (VPS)

php -r "opcache_reset();"

Note: CLI and web OPcache are separate. The CLI command only clears the CLI cache. For web cache, use a PHP script accessed via the browser or restart PHP-FPM:

systemctl restart php8.2-fpm

Troubleshooting

OPcache Not Showing in phpinfo()

  • The OPcache extension may not be installed — contact support or install via your control panel
  • Check if the correct PHP version is selected for your domain

High Memory Usage

  • Reduce memory_consumption if your server is memory-constrained
  • Reduce max_accelerated_files to limit cached scripts

Changes Not Reflecting on Site

  • Set validate_timestamps=1 and reduce revalidate_freq to 2-5 seconds during development
  • Clear OPcache after deploying changes

Need help configuring OPcache? Contact our support team at {{SUPPORT_EMAIL}} or open a ticket at {{SUPPORT_URL}}.