Boosting PHP performance with OPCache

  • avatar
  • 7 mins read

OPCache is a robust tool that enhances PHP performance, making web applications faster and more efficient. Included as an extension in PHP, OPCache stores precompiled script bytecode in shared memory. This eliminates the need for PHP to load and parse scripts with each request, significantly reducing execution overhead and improving server response times. This not only enhances the user experience but also reduces server load, allowing it to handle more requests at once. OPCache's benefits are especially noticeable in high-traffic environments where performance and efficiency are critical.

Install OPCache extension

Ensure OPCache is installed on your server. You can verify this by listing all installed modules for your PHP setup:

php --modules

If it's not already installed, the command to install the OPCache extension is:

sudo apt install php8.x-opcache

Replace php8.x-opcache with the appropriate version you are using, such as php8.3-opcache.

Configure OPCache extension

Configuring OPCache through the php.ini file is straightforward. Begin by locating the php.ini file, which serves as the main configuration file for PHP. The location of this file can vary depending on your server setup. You can find its location by running the command:

php --ini

In our case, the file is located in the following directory:


Alternatively, instead of editing the php.ini file, you can create a separate configuration file for OPCache settings, which is often done to keep configurations organized. Create a new file named 10-opcache.ini (the number ensures it’s loaded in the correct order) in the PHP configuration directory.

In our case the file is located in the following directory:


Here is an example of what the OPCache configuration file should look like.


The configuration values are settings that control how the OPCache extension works in PHP. Adjusting settings like the memory size allocated to OPCache and the frequency for cache checks allows fine-tuning for specific needs. Below is an explanation of the most important and commonly used OPCache configuration directives:

  1. zend_extension:

    This directive enables the OPCache extension. It's typically added automatically when OPCache is installed, but it's essential to ensure it's present.

  2. opcache.enable:


    This setting enables or disables the OPCache extension. Setting it to 1 enables OPCache, while 0 disables it.

  3. opcache.memory_consumption:


    This directive sets the amount of memory (in megabytes) allocated to OPCache for storing precompiled script bytecode. Adjust this value based on the memory available on your server and the size of your application.

  4. opcache.interned_strings_buffer:


    This sets the amount of memory (in megabytes) allocated for interned strings in the OPCache. Interned strings are strings that PHP uses internally to save memory. Increasing this can improve performance if your application uses a lot of strings.

  5. opcache.max_accelerated_files:


    This setting determines the maximum number of PHP files that can be cached by OPCache. Increase this value if your application has a large number of PHP files.

  6. opcache.revalidate_freq:


    This directive sets the frequency (in seconds) at which OPCache checks for script updates. Setting it to 0 means OPCache will check for updates on every request, which is useful for development but should be set to a higher value in production.

  7. opcache.fast_shutdown:


    Enabling fast shutdown allows OPCache to use a faster mechanism to free up the memory, which can improve performance.

  8. opcache.enable_cli:


    This setting enables OPCache for the PHP CLI (command-line interface). It's useful for scripts run from the command line that can benefit from caching.

  9. opcache.validate_timestamps:


    When enabled (1), OPCache will validate the timestamps of scripts to determine if they have been updated. Disabling it (0) can improve performance but should only be done if the scripts never change.

  10. opcache.file_cache:


    This setting specifies a directory for storing precompiled script bytecode on disk. It can be useful if you want to persist the cache between server reboots.

  11. opcache.file_update_protection:


    This directive sets the time (in seconds) that must pass before a file is eligible for caching after being updated. It can prevent caching issues related to concurrent updates.

  12. opcache.max_wasted_percentage:


    This specifies the percentage of "wasted" memory (memory that is not used for active scripts) allowed before OPCache attempts to restart. Setting this helps manage memory usage effectively.

Apply OPCache configuration

After making these changes, you need to restart your web server for the new settings to take effect.

  • For Apache:

    sudo systemctl restart apache2
  • For Nginx with PHP-FPM:

    sudo systemctl restart php8.x-fpm
    sudo systemctl restart nginx

    Replace php8.x-fpm with the appropriate version you are using, such as php8.3-fpm.

Flush OPCache

To flush OPCache in PHP, you can use one of the following methods:

  1. Restart PHP: A straightforward way to flush OPCache is to restart the PHP process or web server. This will clear the entire OPCache, ensuring that all cached bytecode is invalidated.

  2. Using PHP Script: You can create a PHP script specifically for clearing OPCache and execute it through the command line or a web request. Here's a simple example script:


    echo 'OPCache flushed successfully.';
  3. Using OPCache Control Panel (OPCache GUI): If you have OPCache Control Panel installed and configured, you can use its interface to flush OPCache. This is a more user-friendly approach and provides additional insights into OPCache usage and performance.

Choose the method that best fits your workflow and requirements.


OPCache is essential for anyone looking to enhance PHP application performance. By storing precompiled script bytecode in memory, OPCache reduces execution time and server load, significantly improving web application speed and efficiency. Properly configuring OPCache ensures smooth application performance, better user experiences, and a robust environment for handling traffic.

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

We never send SPAM nor unsolicited emails


Leave a Reply

Your email address will not be published.

Replying to the message: View original

Hey visitor! Unlock access to featured articles, remove ads and much more - it's free.