PHP opcache.enable Explained: Configuration, Performance, and Best Practices

Published: 2026-07-08
Google AdSense 广告位 (待申请通过后开启)

Introduction

The opcache.enable directive controls whether PHP OPcache is enabled for web requests. OPcache improves application performance by storing compiled PHP bytecode in shared memory, allowing future requests to execute cached bytecode instead of recompiling PHP source files.

Enabling OPcache is one of the most effective ways to improve the performance of PHP applications. Most production servers enable it by default because it reduces CPU usage, lowers response times, and increases overall throughput.

This guide explains how opcache.enable works, how to configure it, how to verify that OPcache is active, and recommended settings for production environments.

Test Environment

Operating System : AlmaLinux 10
PHP Version      : PHP 8.3
Web Server       : Nginx
PHP-FPM          : Enabled
Architecture     : x86_64
Last Tested      : July 2026

What Is opcache.enable?

The opcache.enable directive determines whether OPcache is active for web requests handled by PHP.

Enable OPcache:

opcache.enable = 1

Disable OPcache:

opcache.enable = 0

When OPcache is enabled, PHP stores compiled bytecode in shared memory so it can be reused by subsequent requests.

How OPcache Works

Without OPcache:

Request
    │
    ▼
Read PHP File
    │
    ▼
Parse Source
    │
    ▼
Compile Bytecode
    │
    ▼
Execute
[sty]
Every request repeats the parsing and compilation process.
With OPcache enabled:
[sty]
Request
    │
    ▼
OPcache
    │
 ┌──┴──────────┐
 │             │
Cache Hit   Cache Miss
 │             │
 ▼             ▼
Execute     Compile
Bytecode       │
               ▼
        Store in Cache

Once a script has been compiled, future requests can reuse the cached bytecode instead of compiling it again.

Check Whether OPcache Is Enabled

Run:

php -i | grep opcache.enable

Example output:

opcache.enable => On => On

You can also verify using:

<?php
phpinfo();

Search for:

Zend OPcache

If the OPcache section appears, the extension is loaded.

Locate the Active Configuration File

Run:

php --ini

Many Linux distributions load OPcache settings from:

/etc/php.d/10-opcache.ini

Always modify the configuration used by PHP-FPM for web applications.

Enable OPcache

Open the OPcache configuration file.

sudo nano /etc/php.d/10-opcache.ini

Locate:

opcache.enable = 0

Change it to:

opcache.enable = 1

Save the file.

Restart PHP-FPM

Apply the changes.

sudo systemctl restart php-fpm

Verify:

sudo systemctl status php-fpm

Expected output:

Active: active (running)

Verify OPcache Status

Create a PHP file:

<?php
var_dump(opcache_get_status());

If OPcache is enabled, PHP returns an array containing cache statistics, memory usage, and hit rate information.

If OPcache is disabled, the function returns false.

opcache.enable vs opcache.enable_cli

These directives control different execution environments.

Directive	Purpose
opcache.enable	Enables OPcache for web requests
opcache.enable_cli	Enables OPcache for command-line PHP scripts

Example:

opcache.enable = 1

opcache.enable_cli = 0

This configuration enables OPcache for websites while leaving command-line scripts unaffected.

Related OPcache Settings

opcache.enable works together with several other important directives.

| Directive                       | Purpose                                        |
| ------------------------------- | ---------------------------------------------- |
| `opcache.memory_consumption`    | Shared memory allocated for cached bytecode    |
| `opcache.max_accelerated_files` | Maximum number of cached scripts               |
| `opcache.validate_timestamps`   | Controls automatic file change detection       |
| `opcache.revalidate_freq`       | Controls how often timestamp validation occurs |

A well-configured OPcache installation requires all of these settings to work together.

Recommended Configuration

Development

opcache.enable = 1

opcache.validate_timestamps = 1

opcache.revalidate_freq = 0

This configuration keeps OPcache enabled while ensuring code changes are detected immediately.

Production

opcache.enable = 1

opcache.validate_timestamps = 0

After each deployment, reset OPcache or reload PHP-FPM so the latest code is cached.

Common Issues

OPcache Appears Disabled

Possible causes include:

The OPcache extension is not installed.

The wrong configuration file was modified.

PHP-FPM has not been restarted.

The web server uses a different PHP version than the command line.

Compare the output of php --ini and phpinfo() to confirm which configuration files are active.

Code Changes Are Not Visible

If OPcache is enabled and:

opcache.validate_timestamps = 0

cached scripts remain in memory until OPcache is reset or PHP-FPM is restarted.

No Performance Improvement

Performance gains may be limited if:

The application is very small.

Database queries dominate request time.

OPcache memory is insufficient.

The cache frequently invalidates because of deployment or configuration issues.

Monitoring OPcache statistics can help identify bottlenecks.

Best Practices

Enable OPcache on all production servers.

Verify that OPcache is active after installation.

Allocate sufficient shared memory using opcache.memory_consumption.

Tune opcache.max_accelerated_files for larger applications.

Review opcache.validate_timestamps and opcache.revalidate_freq based on your deployment strategy.

Restart PHP-FPM after modifying OPcache settings.

Conclusion

The opcache.enable directive determines whether PHP OPcache is active for web requests. Enabling OPcache significantly reduces the cost of repeatedly parsing and compiling PHP scripts, making it one of the most effective performance optimizations available for PHP applications.

For production environments, OPcache should generally remain enabled and be configured alongside opcache.memory_consumption, opcache.max_accelerated_files, opcache.validate_timestamps, and opcache.revalidate_freq to achieve the best balance between performance and maintainability.

Explore More

Technology Guides →

PHP opcache.max_accelerated_files Explained: Configuration, Performance, and Best Practices

PHP opcache.validate_timestamps Explained: Configuration, Code Updates, and Best Practices

PHP opcache.revalidate_freq Explained: Configuration, Performance, and Best Practices

PHP opcache.save_comments Explained: Configuration, Compatibility, and Best Practices

Southeast Asia Insights →