OPcache is PHP's built-in bytecode cache. It keeps compiled scripts in shared memory so PHP doesn't have to parse and compile the same files on every request. This page explains how it works, then shows how to install, configure, check and tune it correctly on XAMPP, Debian/Ubuntu and RHEL-family Linux systems.
OPcache, also known as PHP OPcode cache, is a built-in caching system in PHP that dramatically improves the performance of websites and applications. It works by storing precompiled script bytecode in shared memory, eliminating the need for PHP to parse and compile scripts on each request. This significantly reduces server load and speeds up execution, leading to faster page loading times for users.
Here’s a breakdown of how OPcache works:
- Compilation: When a PHP script is executed for the first time, PHP parses it and converts it into an intermediate representation called bytecode.
- Caching: OPcache stores this bytecode in shared memory, which is accessible to all PHP processes running on the server.
- Subsequent requests: When the same script is executed again, instead of re-compiling it, PHP retrieves the precompiled bytecode from the cache. This significantly reduces the processing time and resources required.
The benefits of using OPcache include:
- Faster page loading times: Websites can be up to 3 times faster with OPcache enabled.
- Reduced server load: By pre-compiling scripts, OPcache frees up CPU and memory for other tasks, making the server more responsive.
- Improved scalability: Applications can handle more concurrent requests efficiently with OPcache.
- Reduced disk I/O: Pre-compiling scripts eliminates the need to read and parse files on each request, reducing disk usage.
How to install OPcache in Linux?
$ /opt/lampp/lampp stop
$ apt-get update -y
$ php --version
$ apt-get install php8.2-opcache
$ cd /opt/lampp/etc
$ vi php.ini
zend_extension=opcache.so
[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.revalidate_freq=200
$ /opt/lampp/lampp restartCode language: JavaScript (javascript)
Corrections to the example above
The commands above mix two different PHP installations. XAMPP (/opt/lampp) ships its own PHP binary and extensions. apt-get install php8.2-opcache installs OPcache for the system PHP under /usr/lib/php/, and XAMPP never loads it. In the same way, php --version reports the system PHP, not the XAMPP one. The php.ini snippet also loads the extension twice. That produces a warning and does nothing useful.
Here is a corrected procedure for XAMPP. XAMPP builds already include opcache.so, so no package install is needed:
# Stop XAMPP
sudo /opt/lampp/lampp stop
# Check the PHP version XAMPP actually uses
/opt/lampp/bin/php -v
# Confirm the bundled OPcache extension exists
sudo find /opt/lampp -name opcache.so
# Edit XAMPP's php.ini
sudo vi /opt/lampp/etc/php.iniCode language: PHP (php)
[opcache]
; Load the extension exactly once. If php.ini already has a
; commented ;zend_extension=opcache.so line, uncomment that one instead.
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.validate_timestamps=1
opcache.revalidate_freq=2
sudo /opt/lampp/lampp start
/opt/lampp/bin/php -v
If OPcache loaded, the version output includes a line similar to with Zend OPcache v8.2.x. The original set opcache.revalidate_freq=200, which means an edited PHP file can take more than three minutes to show up. That's confusing during development, so the example uses the default of 2 seconds. The production tuning section covers when a longer interval, or disabling checks completely, makes sense.
Note: The "3 times faster" figure in the original is not a guaranteed number. How much OPcache helps depends on the application, how many files it loads per request, and how much time goes to database or network calls. Measure your own application before and after enabling it.
Install OPcache for system PHP on Debian and Ubuntu
For PHP installed from distribution packages (Apache with mod_php, or Nginx with PHP-FPM), install the OPcache package that matches the PHP version. It's often already installed as a dependency of the PHP CLI or FPM package.
- Check the PHP version:
php -v. - Install the package:
sudo apt-get update && sudo apt-get install -y php-opcache. Thephp-opcachemetapackage follows the distribution's default PHP version. If you use a specific version, for example one from a third-party repository, installphp8.3-opcacheor the matching versioned package instead. - Enable the module, in case it isn't enabled yet:
sudo phpenmod opcache. - Put tuning settings in a separate file so package upgrades don't overwrite them, for example
/etc/php/8.3/fpm/conf.d/99-opcache-tuning.ini(useapache2/conf.dfor mod_php). - Restart the PHP process that serves web requests:
sudo systemctl restart php8.3-fpmorsudo systemctl restart apache2.
The zend_extension=opcache.so line already exists in /etc/php/<version>/mods-available/opcache.ini. Don't add it again in your tuning file.
Install OPcache on RHEL, Rocky, AlmaLinux and Fedora
sudo dnf install -y php-opcache
# Main OPcache config file on these distributions
sudo vi /etc/php.d/10-opcache.ini
# Restart PHP-FPM (and httpd if it proxies to FPM)
sudo systemctl restart php-fpm
sudo systemctl restart httpdCode language: PHP (php)
10-opcache.ini already contains the zend_extension line and commented defaults for every setting. Edit the values in place.
Note: The newest PHP releases compile OPcache directly into PHP, so a separate package or zend_extension line may not exist. Check with php -v: if it already shows with Zend OPcache, skip the install step and only adjust the settings.
Verify that OPcache is working
The CLI and the web server can read different php.ini files, and OPcache is disabled for the CLI by default (opcache.enable_cli=0). For that reason, php -v only proves the extension can load. Check the web context as well.
# Which ini files does the CLI read?
php --ini
# Is the extension loaded and what are the effective values?
php -i | grep -E 'opcache.enable|opcache.memory_consumption'Code language: PHP (php)
To check the web server, temporarily put this file in the document root, open it in a browser, then delete it:
<?php
// opcache-check.php: remove after use
$s = opcache_get_status(false);
header('Content-Type: text/plain');
printf("Enabled: %s\n", $s['opcache_enabled'] ? 'yes' : 'no');
printf("Cached scripts: %d\n", $s['opcache_statistics']['num_cached_scripts']);
printf("Hit rate: %.2f%%\n", $s['opcache_statistics']['opcache_hit_rate']);
printf("Memory used: %.1f MB\n", $s['memory_usage']['used_memory'] / 1048576);
printf("Cache full: %s\n", $s['cache_full'] ? 'yes' : 'no');Code language: HTML, XML (xml)
After the site has handled some traffic, the hit rate should be close to 100%. If cache_full is yes, or the oom_restarts and hash_restarts counters keep going up, the cache is too small. See the tuning table below.
Key settings and production tuning
| Setting | Default | What it controls | Typical production value |
|---|---|---|---|
opcache.enable | 1 | Turns OPcache on for web SAPIs | 1 |
opcache.enable_cli | 0 | OPcache for command-line scripts | 0 (CLI processes are short-lived) |
opcache.memory_consumption | 128 | Shared memory for bytecode, in MB | 128 to 512, depending on code size |
opcache.interned_strings_buffer | 8 | Memory for deduplicated strings, in MB | 16 to 32 for large frameworks |
opcache.max_accelerated_files | 10000 | Maximum number of cached scripts | More than the number of PHP files in the application |
opcache.validate_timestamps | 1 | Check files for changes | 0 when code changes only via deployments |
opcache.revalidate_freq | 2 | Seconds between change checks | 2 to 60 if timestamps are validated |
opcache.save_comments | 1 | Keep docblock comments | 1 (annotation-based libraries need it) |
To size max_accelerated_files, count the PHP files, including vendor libraries:
find /var/www/myapp -type f -name '*.php' | wc -lCode language: JavaScript (javascript)
With opcache.validate_timestamps=0, PHP never checks files on disk again, so changed code keeps running from the old cached copy until the cache is cleared. Add a cache reset to every deployment, usually sudo systemctl reload php8.3-fpm (or restarting Apache or XAMPP).
Note: Running opcache_reset() from the command line only clears the CLI's own cache. It doesn't affect PHP-FPM or Apache. Reload the service, or call the function through a web request that you have protected.
Common errors and fixes
| Symptom | Cause | Fix |
|---|---|---|
| Warning: Cannot load Zend OPcache – it was already loaded | zend_extension=opcache.so appears more than once, as in the original snippet or in two ini files | Keep one line. Search with grep -rn opcache.so /etc/php* /opt/lampp/etc |
| Warning that OPcache must be loaded as a Zend extension | Used extension=opcache.so | Change it to zend_extension=opcache.so |
OPcache missing from phpinfo() | Edited the wrong php.ini, or didn't restart | Check "Loaded Configuration File" in phpinfo(), then restart FPM, Apache or XAMPP |
| Code changes don't appear | validate_timestamps=0 or a large revalidate_freq | Reload PHP-FPM or Apache, or lower revalidate_freq |
| Hit rate drops, frequent restarts | Cache memory or key slots are full | Increase memory_consumption, interned_strings_buffer or max_accelerated_files |
Security notes
- Don't leave status pages such as
opcache-check.phpor third-party OPcache dashboards publicly reachable. They expose file paths and configuration. - Use
opcache.restrict_api=/var/www/adminso that only scripts under that path can callopcache_reset()andopcache_get_status(). - On shared servers where several users share one PHP-FPM master, set
opcache.validate_permission=1andopcache.validate_root=1so users can't read each other's cached scripts. Separate FPM pools per user are the stronger option.
For more server and language guides, see More Linux tutorials and More PHP tutorials.
Frequently asked questions
Is OPcache enabled by default in PHP?
OPcache ships with PHP, and opcache.enable defaults to 1. On many distributions the extension still has to be installed or loaded with zend_extension=opcache.so. Run php -v or check phpinfo() to confirm.
How do I clear the OPcache after deploying new code?
Reload the PHP service that serves web requests, for example sudo systemctl reload php8.3-fpm, or restart Apache or XAMPP. Calling opcache_reset() from the CLI won't clear the web server's cache.
Why does php -v show OPcache but my site isn't using it?
The CLI and the web server often read different ini files. Check the "Loaded Configuration File" and the opcache section in a phpinfo() page served by the web server, then restart PHP-FPM or Apache after editing.
What is the difference between OPcache and APCu?
OPcache caches compiled PHP bytecode. APCu is a separate extension that caches application data such as arrays and objects in memory. Many applications use both.
Should I enable OPcache JIT?
JIT is part of OPcache in PHP 8 and later. It needs a non-zero opcache.jit_buffer_size, and it mostly helps CPU-heavy code rather than typical database-bound web requests. Benchmark your own application before turning it on in production.
Related tutorials
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals