MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

What is opcache and Install / Configure in Linux

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:

  1. Compilation: When a PHP script is executed for the first time, PHP parses it and converts it into an intermediate representation called bytecode.
  2. Caching: OPcache stores this bytecode in shared memory, which is accessible to all PHP processes running on the server.
  3. 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.

  1. Check the PHP version: php -v.
  2. Install the package: sudo apt-get update && sudo apt-get install -y php-opcache. The php-opcache metapackage follows the distribution's default PHP version. If you use a specific version, for example one from a third-party repository, install php8.3-opcache or the matching versioned package instead.
  3. Enable the module, in case it isn't enabled yet: sudo phpenmod opcache.
  4. 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 (use apache2/conf.d for mod_php).
  5. Restart the PHP process that serves web requests: sudo systemctl restart php8.3-fpm or sudo 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

SettingDefaultWhat it controlsTypical production value
opcache.enable1Turns OPcache on for web SAPIs1
opcache.enable_cli0OPcache for command-line scripts0 (CLI processes are short-lived)
opcache.memory_consumption128Shared memory for bytecode, in MB128 to 512, depending on code size
opcache.interned_strings_buffer8Memory for deduplicated strings, in MB16 to 32 for large frameworks
opcache.max_accelerated_files10000Maximum number of cached scriptsMore than the number of PHP files in the application
opcache.validate_timestamps1Check files for changes0 when code changes only via deployments
opcache.revalidate_freq2Seconds between change checks2 to 60 if timestamps are validated
opcache.save_comments1Keep docblock comments1 (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

SymptomCauseFix
Warning: Cannot load Zend OPcache – it was already loadedzend_extension=opcache.so appears more than once, as in the original snippet or in two ini filesKeep one line. Search with grep -rn opcache.so /etc/php* /opt/lampp/etc
Warning that OPcache must be loaded as a Zend extensionUsed extension=opcache.soChange it to zend_extension=opcache.so
OPcache missing from phpinfo()Edited the wrong php.ini, or didn't restartCheck "Loaded Configuration File" in phpinfo(), then restart FPM, Apache or XAMPP
Code changes don't appearvalidate_timestamps=0 or a large revalidate_freqReload PHP-FPM or Apache, or lower revalidate_freq
Hit rate drops, frequent restartsCache memory or key slots are fullIncrease memory_consumption, interned_strings_buffer or max_accelerated_files

Security notes

  • Don't leave status pages such as opcache-check.php or third-party OPcache dashboards publicly reachable. They expose file paths and configuration.
  • Use opcache.restrict_api=/var/www/admin so that only scripts under that path can call opcache_reset() and opcache_get_status().
  • On shared servers where several users share one PHP-FPM master, set opcache.validate_permission=1 and opcache.validate_root=1 so 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

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
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.

Related Posts

Packer Tutorials: Packer Install and configurations

HashiCorp Packer is a single binary that builds machine images (AMIs, Azure images, Docker images, VMware templates and more) from one HCL template. The commands above install…

Read More

How to install Kapitan in Windows using wsl?

Kapitan is a Python-based configuration management tool, and on Windows it runs best inside the Windows Subsystem for Linux (WSL). This guide fills in the commands the…

Read More

What are the Authentication type in phpmyadmin which for config.inc.php

phpMyAdmin supports four authentication types, set per server with $cfg['Servers'][$i]['auth_type'] in config.inc.php: cookie (the default), config, http and signon. This page explains how each one works and…

Read More

Elastic Agent Commands Reference

Elastic Agent runs as an operating system service, so you start and stop it with systemd on Linux, launchd on macOS and the Service Control Manager on…

Read More

How to install Ansible in Centos 7/RHEL 7?

On CentOS 7 or RHEL 7 you can install Ansible in two ways: with yum from the EPEL repository, which gives you Ansible 2.9, or with pip…

Read More

Kibana 7.2 Install and configuration in RHEL 7/Centos

This guide covers installing Kibana 7.2 on RHEL 7 or CentOS 7 from the tar.gz archive, pointing it at an Elasticsearch cluster in kibana.yml, and starting it….

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x