Configure & tune opcache on php7

Posted March 7, 2017 124k views
PHPLEMPUbuntu 16.04

as written on subject, how can i configure opcache on php 7 and what should be the optimized configuration for 10$ droplet?

thanks in advance.

1 comment
  • So a 1GB droplet. How much free RAM do you have to spare?
    This is not perfect, but a setting I use on one server:

    ;when to check files for changes in seconds. set this to 0 and opcache.validate_timestamps=0 to have cache until php restart
    ;how many files does your project have? find . -type f -iname *.php | wc -l
    ;the following two lines is the amount of RAM to allocate to the cache

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer


Normally I use something like:

opcache.validate_timestamps=0 (comment this out in your dev environment)

Which is from:

Depending on file count, you may need to increase opcache.max_accelerated_files. That said, the above is allocating up to 192MB, which may be excessive depending on your application. If you find that to be high (i.e. you’re only using 25% of that at any give time), you can reduce that down further.

  • @jtittle & @hansen
    thanks you both for replying.

    and to enable opcache, i only need to un-comment and change the following line to 1. right?


    or i need to add following as well

    also dont i need to configure opcache location? im also a bit confused about following 2 settings

    opcache.revalidate_freq & opcache.validate_timestamps

    i read the above link @jtittle provided but still confused.
    on my setup code dont get changes too often. but it might change in some cases. so could you please explain above 2 configs little more understandable way.

    thanks a lot.

    • @newbie

      Correct - opcache.enable controls whether it’s enabled or disabled using either 0 or 1 where 0 is disabled and 1 is enabled.

      You can use opcache_get_status to get specific information about current usage as well. This will give you an idea of how much RAM is used, free, wasted, etc as well as quite bit more useful information you can break down.

      It outputs the results to an array, though it’s easy enough to read through.

      You can also use something like:

      … or, if you want something slightly nicer looking:

      Some have not been updated in a while, so I wouldn’t recommend allowing public access but they will provide you with a general overview of how OPC is running so you can then make adjustments as needed.

      Just make sure you restart PHP-FPM after each change is made.

    • @newbie If you’ve installed the default PHP7, then opcache is enabled by default, but yes, it requires that you’ve installed the module (php7.0-opcache), which I think is default recommended.
      If you run CLI-mode, it’s by default disabled.

      The configuration actually gives a very good explanation of each variable:

      But to put it very simply. If you want PHP to automatically update the cache, when you do code changes, then you would use the following for a 60 seconds delay:


      If your code almost never changes, or if you push the code from a staging environment to production, where you know the code already works, then you could set the following, but you would be required to manually flush the cache or restart PHP to activate the caches.

      opcache.revalidate_freq=0 #this is actually ignored because of the line below
    • @newbie
      Sorry, missed the “configure opcache location” question.
      No, opcache is stored in-memory, that’s why it’s so fast. If you want to store the cache even when PHP is restarted, so you have what is known as a warm/hot cache instead of cold cache (meaning it needs to cache everything again), then you can do that in PHP7+ with this parameter:
      Don’t use it - unless you really need it. It’s better that everything gets cleared, so you don’t run into issues with cache.

      • @hansen
        really really appreciated. that clear everything now.

      • @hansen & @jtittle
        i have enabled opcache as you guy’s instructed. many thanks for that.

        do i also need to enable opcache for CLI version too? on php documentation they says its only required for testing & development perpose.

        if i search for configuration file in command line

        root@server:~$ php -i |grep 'Configuration File'
        Configuration File (php.ini) Path => /etc/php/7.0/cli
        Loaded Configuration File => /etc/php/7.0/cli/php.ini

        so im confused again between php fpm & cli :(

        • @newbie
          I wouldn’t recommend using cache for CLI, since that is used for cronjobs and the like. It depends on what you run. Chances are that you’re almost never using CLI, so just leave non-cached.

          When you run a command like php -i |grep 'Configuration File' that’s CLI. It’s a single-threaded process (as far as I can remember).
          When you use php-fpm via Nginx, it’s a multi-threaded process manager, so it can handle many requests at the same time.

          If you want to enable modules in CLI, just copy or link the configuration files from /etc/php/7.0/modules-available/ to /etc/php/7.0/cli/conf.d/
          Wouldn’t recommend it - unless you really need to have something special enabled in CLI, just leave it, since that makes it less error-prone to failure of a module.

        • @newbie

          The CLI version of the PHP package is designed for interaction with the CLI (i.e the same CLI you’re using to install packages).

          For example, when you run:

          php -v

          You’re using the CLI package to get the PHP version. Or when you use:

          php -i

          You’re using the CLI pack to get the same information you’d get (in text format) using phpinfo() inside of a PHP script.


          <?php phpinfo();

          Unless you’re running PHP from the CLI, there’s no real need to enable caching there. It’d be beneficial if you want to test, for example, using PHP’s built-in web server, but with NGINX installed, there’s really no need.

          For example, we could use:

          php -S -t /path/to/public

          Now we’re still using the CLI and we’re running an instance of PHP’s built-in web server, so the CLI package will be used. When we call upon index.php, you’ll see data on the terminal screen (the same thing you’d see in access and error logs).

          That being said, PHP can be used from the CLI much like bash scripts can be, so if you design a PHP script to be used from the CLI, then enabling caching may also be beneficial there too.

          Sometimes things are easier using a programming language over a scripting language (bash) :-).

          Anything that’s accessed over the web (from a browser, api request, etc) is going to use FPM.