PHP / Server Setup

Zend Opcache

by , , revisited on


We have by far the largest RPM repository with dynamic stable NGINX modules and VMODs for Varnish 4.1 and 6.0 LTS. If you want to install nginx, Varnish and lots of useful modules for them, this is your one stop repository to get all performance related software.
You have to maintain an active subscription in order to be able to use the repository!

PHP is fast. But not when you use CMS with hundreds of small PHP files. WordPress, Magento, Joomla are all great examples of popular CMS solutions, but you can find their performance extremely slow. This is due to the fact, that PHP is interpreted language. Each time a website page is requested, all those files have to be parsed and executed.

You can increase performance of the PHP engine by creating precompiled bytecode cache for all PHP files. This way you remove the need for PHP to load and parse scripts on each request.

Install Zend Opcache on CentOS 7 VPS

These commands install PHP 7 with Opcache extension:

yum -y install epel-release
yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum -y install yum-utils
yum-config-manager --enable remi-php70
yum update
yum -y install php-cli php-fpm php-common php-opcache

File based OPCache

Versions of PHP >= 7.0 are capable of storing OPCaches in file system, in addition to memory. You need to explicitly enable this feature during compilation and in PHP configuration.

Remi builds of those PHP versions are compiled with support for file based OPCache.

Configuring a separate PHP-FPM pool’s cache directory for file based OPCache works and PHP-FPM will save the respective .bin cache files using appropriate users between PHP-FPM pools. For each pool running under a different user, you’d have:

php_admin_value[opcache.file_cache] = /srv/www/foo.example.com/.opcache

You’d need to create /srv/www/foo.example.com/.opcache beforehand and make sure it’s chown-ed with appropriate user.

The obvious benefits are:

  • PHP 7 CLI programs (composer, n98-magerun2, etc.) can now benefit persistent file-based OPCache and run faster
  • Cron PHP scripts, being a subset of PHP 7 CLI programs, can run faster as well
  • You can safely restart PHP-FPM: OPcaches will be copied to memory from file system. Now there’s less time that your website is hit against raw unparsed PHP scripts

Some more insights on the benefits of file-based opcaches is available here:

Security concerns of OPcache cache poisoning can be found here.

In case of a file based OPcache, opcache_reset will not reset it. You need to reset it by simply deleting it on the file system. The proper way seems to be the most performant one, which is calling rm -rf /srv/www/foo.example.com/.opcache/*, or:

// Check if file cache is enabled and delete it if enabled
if ( ini_get( 'opcache.file_cache' ) && is_writable( ini_get( 'opcache.file_cache' ) ) ) {
    $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( ini_get('opcache.file_cache'), RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST );
    foreach ( $files as $fileinfo ) {
        $todo = ( $fileinfo->isDir() ? 'rmdir' : 'unlink' );
        $todo( $fileinfo->getRealPath() );
    }
}

Caveats of file based OPCaches

… are greatly outlined by iquito at the discussion thread:

You would need to delete all cache files and then call opcache_reset(). Race conditions can easily occur: between the rm and opcache_reset() new files could have already been created. Especially when you deploy an application some files will have changed and there is no “atomic” way to delete the file cache and the opcache in sync. PHP-FPM might also have issues if it tries to read a file that has just been deleted

Install Zend Opcache on cPanel based server

https://www.vpsblocks.com.au/support/Knowledgebase/Article/View/171/0/installing-zend-opcache-on-cpanel-servers

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.