How to Enable PHP OPcache for Better Performance | Complete Guide
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:
Without OPcache, these compilation steps happen on every request.
After enabling OPcache:
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:
If OPcache is installed, you should see:
For more detailed information:
If nothing is returned, OPcache may not be installed or enabled.
Find the Active php.ini File
Example output:
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.
Locate the OPcache section or add the following configuration if it does not already exist.
Recommended OPcache Settings
Restart PHP
For PHP-FPM:
For Apache:
For Nginx:
Verify OPcache Is Working
Or create a PHP file:
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
For even better performance, consider tuning your PHP-FPM process manager settings.
Explore More
› 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