C4f1151075b447779af31e99d6cf70e2c6eb47ac
By:
newbie

Configure & tune opcache on php7

March 7, 2017 6.7k views
PHP LEMP Ubuntu 16.04

Hi,
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
    opcache.revalidate_freq=60
    ;how many files does your project have? find . -type f -iname *.php | wc -l
    opcache.max_accelerated_files=9500
    ;the following two lines is the amount of RAM to allocate to the cache
    opcache.memory_consumption=192
    opcache.interned_strings_buffer=8
    
1 Answer

@newbie

Normally I use something like:

opcache.revalidate_freq=0
opcache.validate_timestamps=0 (comment this out in your dev environment)
opcache.max_accelerated_files=7963
opcache.memory_consumption=192
opcache.interned_strings_buffer=16
opcache.fast_shutdown=1

Which is from:

https://www.scalingphpbook.com/blog/2014/02/14/best-zend-opcache-settings.html

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?

    opcache.enable=1
    

    or i need to add following as well

    zend_extension=opcache.so
    

    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.

      http://php.net/manual/en/function.opcache-get-status.php

      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.
      http://php.net/manual/en/opcache.configuration.php

      The configuration actually gives a very good explanation of each variable:
      http://php.net/manual/en/opcache.configuration.php#ini.opcache.validate-timestamps
      http://php.net/manual/en/opcache.configuration.php#ini.opcache.revalidate-freq

      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:

      opcache.revalidate_freq=60
      opcache.validate_timestamps=1
      

      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
      opcache.validate_timestamps=0
      
    • @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:
      http://php.net/manual/en/opcache.configuration.php#ini.opcache.file-cache
      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.

          i.e.

          <?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 0.0.0.0:8080 -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.

Have another answer? Share your knowledge.