OPcache is a built-in PHP extension that improves performance by caching precompiled script bytecode in memory, reducing the need for PHP to recompile scripts on every request. This guide explains how to enable and configure OPcache to optimize your PHP applications. 🛠️
🔍 Why Use OPcache?
-
🏃 Improved Performance: Faster execution of PHP scripts.
-
💾 Reduced Server Load: Decreases CPU usage by avoiding repeated script compilation.
-
📈 Enhanced Scalability: Handles increased traffic effectively.
💡 Prerequisites
-
Access to your server’s control panel (cPanel, Plesk, or DirectAdmin) or terminal.
-
PHP version 5.5 or higher (OPcache is included by default).
-
Basic knowledge of server configurations.
📖 Steps to Enable OPcache
🛠️ Option 1: Using the php.ini
File
-
Locate the
php.ini
File:-
Common paths:
/etc/php.ini
,/etc/php/<version>/php.ini
, or in the hosting control panel.
-
-
Edit the File: Open the file in a text editor and add or modify the following lines:
opcache.enable=1 opcache.memory_consumption=128 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=10000 opcache.revalidate_freq=2
-
Restart the Web Server: Apply the changes by restarting your web server:
sudo systemctl restart apache2
or
sudo systemctl restart nginx
⚙️ Option 2: Using cPanel’s MultiPHP INI Editor
-
Log in to cPanel:
-
Use your credentials to access your hosting account.
-
-
Navigate to MultiPHP INI Editor:
-
Under the Software section, click MultiPHP INI Editor.
-
-
Select Your Domain:
-
Choose the domain you want to configure from the dropdown menu.
-
-
Enable OPcache:
-
Add or modify the following directives in the editor:
opcache.enable = 1 opcache.memory_consumption = 128 opcache.max_accelerated_files = 10000 opcache.revalidate_freq = 2
-
-
Save Changes:
-
Click the Apply button to save.
-
🌐 Option 3: Using PHP Extensions Installer (WHM/cPanel)
-
Access WHM:
-
Log in to WHM as root.
-
-
Search for PHP Extensions:
-
Navigate to Software > EasyApache 4.
-
-
Enable OPcache:
-
Locate and enable the OPcache extension for the required PHP version.
-
-
Restart Services:
-
Restart Apache or NGINX to apply the changes.
-
🛡️ Verifying OPcache Status
-
Create a PHP Info Script:
-
Save the following code as
info.php
in your web directory:<?php phpinfo(); ?>
-
-
Access the Script:
-
Open the script in your browser (e.g.,
http://yourdomain.com/info.php
).
-
-
Check OPcache Section:
-
Look for the OPcache section to confirm it is enabled and configured correctly.
-
📈 Recommended OPcache Settings
Setting | Recommended Value | Description |
---|---|---|
opcache.enable |
1 |
Enables OPcache. |
opcache.memory_consumption |
128 |
Allocates memory for OPcache. |
opcache.interned_strings_buffer |
8 |
Optimizes string handling. |
opcache.max_accelerated_files |
10000 |
Caches up to 10,000 files. |
opcache.revalidate_freq |
2 |
Revalidates cache every 2 seconds. |
🔄 Troubleshooting
-
OPcache Not Appearing in PHP Info:
-
Ensure the OPcache extension is installed and enabled.
-
-
Configuration Not Taking Effect:
-
Check for conflicting
php.ini
files or.htaccess
overrides.
-
-
Errors After Enabling OPcache:
-
Verify settings and reduce
memory_consumption
if necessary.
-
🎯 Conclusion
Enabling OPcache is an effective way to enhance PHP performance and reduce server load. By following the steps in this guide, you can configure OPcache to optimize your web applications. 🚀
📌 Related Articles
For more tutorials, visit our Knowledgebase. 🌟