🚀 How to Enable OPcache for PHP Print

  • 0

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

  1. Locate the php.ini File:

    • Common paths: /etc/php.ini, /etc/php/<version>/php.ini, or in the hosting control panel.

  2. 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
  3. 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

  1. Log in to cPanel:

    • Use your credentials to access your hosting account.

  2. Navigate to MultiPHP INI Editor:

    • Under the Software section, click MultiPHP INI Editor.

  3. Select Your Domain:

    • Choose the domain you want to configure from the dropdown menu.

  4. 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
  5. Save Changes:

    • Click the Apply button to save.

🌐 Option 3: Using PHP Extensions Installer (WHM/cPanel)

  1. Access WHM:

    • Log in to WHM as root.

  2. Search for PHP Extensions:

    • Navigate to Software > EasyApache 4.

  3. Enable OPcache:

    • Locate and enable the OPcache extension for the required PHP version.

  4. Restart Services:

    • Restart Apache or NGINX to apply the changes.


🛡️ Verifying OPcache Status

  1. Create a PHP Info Script:

    • Save the following code as info.php in your web directory:

      <?php
      phpinfo();
      ?>
  2. Access the Script:

    • Open the script in your browser (e.g., http://yourdomain.com/info.php).

  3. 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

  1. OPcache Not Appearing in PHP Info:

    • Ensure the OPcache extension is installed and enabled.

  2. Configuration Not Taking Effect:

    • Check for conflicting php.ini files or .htaccess overrides.

  3. 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. 🌟

 


Was this answer helpful?

« Back