Configure Zend Opcache extension for PHP

Zend Opcache extension installation

If you want to speed up your PHP application maybe you are looking for a PHP accelerator such APC cache.

Since PHP 5.5 there is another option for caching Opcodes (details): Zend OpCache extension

If you are using PHP 5.5.0 and later Zend Opcache extension may be compiled with PHP, you have only to configure it.

If you are using an older version of PHP you can compile the extension following a few simple steps:

  • Download the latest version here https://github.com/zendtech/ZendOptimizerPlus
  • Configure the compilation with your PHP config
  • ./configure \
          --with-php-config=$PHP_DIR/bin/php-config
  • Build the package
    make
    
  • The install command will copy the binary file to your extension directory (or you can copy by your own)
     make install

After the compilation was successfully finished you can declare your new extension into php.ini file

 zend_extension=/...full path.../opcache.so

 Zend Opcache configuration

The full parameters list are listed on Zend Opcache documentation

I will just mention a few parameters you have to consider in your configuration

opcache.revalidate_freq=0
opcache.validate_timestamps=0
opcache.max_accelerated_files=7963
opcache.memory_consumption=192
opcache.interned_strings_buffer=16
opcache.fast_shutdown=1
  •  revalidate_freq – configure how often the file timestamp is checked to detect file changes. 0 – means check every time. You have to configure revalidate_freq 0 on your development machine. On production, I am using a large number such as 3600
  • validate_timestamps – When disabled, you must reset the OPcache manually or restart the webserver for changes to the filesystem to take effect. I advise you to use it on the production server and create a hook to clear Opcache after release
  • max_accelerated_files  – the maximum number of scripts stored in opcached memory. you can count your code-base using find . -type f -print | grep php | wc -l
  • memory_consumption – the amount of memory to store pre-compiled PHP files

You can read here a performance benchmark report here on the same subject.