Here's a complete article on activating and optimizing OPcache in php.ini for Apache2 to achieve the best PHP performance:
Boost PHP Performance: Enable and Optimize OPcache in Apache2's php.ini
If you run a PHP-based website on Apache (e.g., WordPress, Laravel, Magento), enabling and tuning OPcache can dramatically improve performance. This guide explains how to activate and optimize OPcache using the php.ini file on Apache2.
What is OPcache?
OPcache is a powerful PHP extension that caches precompiled PHP code (bytecode) in memory, so scripts don’t need to be compiled on every request. This reduces CPU usage and increases response speed.
✅ Benefits of OPcache
️ Step-by-Step Guide: Enable OPcache in Apache2 (Ubuntu/Debian)
1. ✅ Check if OPcache is Installed
Run:
php -v
You should see something like:
PHP 8.x.x (cli) ...
with Zend OPcache v8.x.x, Copyright (c) Zend Technologies
If not, install OPcache:
sudo apt install php-opcache
Then restart Apache:
sudo systemctl restart apache2
2. Locate Your php.ini File
To find your active php.ini:
php --ini
Common paths:
Make sure to edit the Apache version, not CLI.
3. ✍️ Enable and Configure OPcache in php.ini
Open the file:
sudo nano /etc/php/8.1/apache2/php.ini
Search for [opcache] or add it at the bottom if missing, and apply the recommended settings:
[opcache]
; Enable OPcache
opcache.enable=1
; Enable OPcache for the CLI version (optional)
opcache.enable_cli=1
; Amount of memory for storing precompiled scripts
opcache.memory_consumption=128
; Number of files that can be cached
opcache.max_accelerated_files=10000
; How often to check for script changes (in seconds)
opcache.revalidate_freq=60
; Validate timestamps to detect file changes
opcache.validate_timestamps=1
; Save precompiled bytecode to disk (optional)
opcache.file_cache=/tmp
; Optimize for performance
opcache.interned_strings_buffer=16
opcache.fast_shutdown=1
opcache.enable_file_override=0
✅ These settings are a balance of performance and compatibility for most production servers.
4. Restart Apache2
After saving changes, restart Apache:
sudo systemctl restart apache2
Optional: Verify OPcache Status
Create a phpinfo() file to confirm OPcache is enabled:
phpinfo.php:
<?php phpinfo(); ?>
Visit it in your browser, then search for “OPcache” to see all active configurations.
Or, for a cleaner view, use a dashboard:
⚠️ Troubleshooting Tips
Not seeing changes? Make sure you're editing the correct php.ini file for Apache.
Restart Apache after changes: sudo systemctl restart apache2
For WordPress, use plugins like Query Monitor or W3 Total Cache to observe OPcache behavior.
Best Practice Summary
Setting | Recommended |
---|
opcache.enable | 1 |
opcache.memory_consumption | 128 or 256 |
opcache.max_accelerated_files | 10000 |
opcache.revalidate_freq | 60 (or 0 for dev) |
opcache.validate_timestamps | 1 (or 0 in production) |
opcache.enable_cli | 1 (optional) |
opcache.interned_strings_buffer | 16 |
opcache.file_cache | /tmp or a persistent path |
Conclusion
Activating and optimizing OPcache via php.ini in Apache2 is a must-do for any PHP website seeking maximum speed and stability. A few simple changes can dramatically reduce server load and increase responsiveness.