How to Enable PHP OPcache for Better Performance | Complete Guide

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

Introduction

PHP OPcache is one of the easiest and most effective ways to improve PHP application performance. Instead of compiling PHP scripts every time a request is received, OPcache stores the compiled bytecode in shared memory so future requests can execute much faster.

Whether you are running WordPress, Laravel, Symfony, or a custom PHP application, enabling OPcache can significantly reduce CPU usage and improve response times.

This guide explains what OPcache is, how to enable it, recommended configuration options, and how to verify that it is working correctly.

What Is PHP OPcache?

Normally, every PHP request goes through the following steps:

PHP Script
      │
      ▼
Parse Source Code
      │
      ▼
Compile to Opcode
      │
      ▼
Execute

Without OPcache, these compilation steps happen on every request.

After enabling OPcache:

First Request
PHP Script
      │
Compile
      │
Store Bytecode in Memory
Later Requests
Memory
      │
Execute Directly

The PHP source file is compiled only once, allowing subsequent requests to skip the compilation process.

Benefits include:

Faster page loading

Lower CPU utilization

Better server performance

Reduced response time

Improved scalability for high-traffic websites

Check Whether OPcache Is Enabled

Run the following command:

php -m | grep OPcache

If OPcache is installed, you should see:

Zend OPcache

For more detailed information:

php --ri Zend OPcache

If nothing is returned, OPcache may not be installed or enabled.

Find the Active php.ini File

php --ini

Example output:

Loaded Configuration File: /etc/php.ini

For PHP-FPM installations, the configuration file may differ from the CLI version. Always verify that you are editing the correct php.ini.

Enable PHP OPcache

Open your php.ini file.

sudo vim /etc/php.ini

Locate the OPcache section or add the following configuration if it does not already exist.

[opcache]

opcache.enable=1
opcache.enable_cli=0

opcache.memory_consumption=256
opcache.interned_strings_buffer=16

opcache.max_accelerated_files=20000

opcache.validate_timestamps=1
opcache.revalidate_freq=2

opcache.save_comments=1
opcache.fast_shutdown=1

opcache.jit=off

Recommended OPcache Settings

opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.validate_timestamps=1
opcache.revalidate_freq=2
opcache.save_comments=1
opcache.jit=off

Restart PHP

For PHP-FPM:

sudo systemctl restart php-fpm

For Apache:

sudo systemctl restart httpd

For Nginx:

sudo systemctl reload nginx

Verify OPcache Is Working

php --ri Zend OPcache

Or create a PHP file:

<?php
phpinfo();

Common Issues

Changes Do Not Take Effect

Possible causes include:

PHP-FPM has not been restarted.

You edited the wrong php.ini file.

The CLI and PHP-FPM use different configuration files.

A web server cache is serving old content.

OPcache Is Not Loaded

Check whether the extension is installed:

php -m

If Zend OPcache is missing, install or enable the extension for your PHP version.

Updated PHP Files Are Not Reflected

If you have configured:

opcache.validate_timestamps=0

PHP will not automatically detect modified files. Restart PHP-FPM or manually clear the OPcache after deploying updates.

Best Practices

Enable OPcache on all production servers.

Allocate sufficient shared memory based on your application size.

Keep opcache.save_comments=1 for framework compatibility.

Monitor OPcache usage and increase memory_consumption if the cache becomes full.

Test configuration changes in a staging environment before deploying them to production.

Conclusion

PHP OPcache is a built-in performance feature that can dramatically reduce script compilation overhead and improve application responsiveness. With only a few configuration changes, most PHP websites can achieve faster page loads and lower CPU usage.

If you are running PHP in production, enabling and tuning OPcache should be one of the first optimization steps after installation.

Test Environment

Test Environment
Operating System : AlmaLinux 10
PHP Version      : PHP 8.3.x
Web Server       : Nginx
PHP-FPM          : Enabled
Last Tested      : July 2026

For even better performance, consider tuning your PHP-FPM process manager settings.

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.enable Explained: Configuration, Performance, and Best Practices

Southeast Asia Insights →