A great news for the PHP community is that Zend Optimizer+ was became open-source, so it is built-in from PHP 5.5. This tutorial will guide how to install Zend Opcache in PHP 5.5 and will mention a little bit about its performance.
What is Optimizer+?
According to PHP OptimizerPlus RFC, Optimizer+ is the fastest opcode cache available for PHP, and presently supports PHP 5.2 through 5.5, with public builds available for PHP 5.2 through 5.4. It was originally developed in 1998 and was the first opcode cache available for PHP. As Zend made the source code of Optimizer+ available under the PHP License, so that it can become an integrated part of PHP with no strings attached.
What is an Opcode Cache?
An opcode cache is a component that is designed to speed the performance of PHP without altering the behavior of applications in any way.
Without an opcode cache, every time PHP executes a .php file, it invokes the runtime compiler, generates an in-memory representation of the file (called intermediate code), and then invokes the executor on it.
Since on a given version of PHP, compiling the same .php file will always result in the exact same intermediate code – this creates an excellent use case for caching.
An opcode cache performs this exact task – it overrides PHP‘s default compiler callback; When invoked – it will check if a compiled intermediate-code version of the code is already available in-memory. If one exists – it will use it without invoking PHP‘s actual compiler, saving the overhead of compilation. If not – it will invoke PHP‘s internal compiler, generate the code, persist it in memory for future use (saving the need for subsequent compilations of the same file) – and then execute it.
Modern opcode caches (Optimizer+, APC 2.0+, others) use shared memory for storage, and can execute files directly off of it – without having to ‘unserialize’ the code before execution. This results in dramatic performance speedups, typically reduces the overall server memory consumption, and usually with very few downsides.
APC vs Zend OpCache?
Advantages of Optimizer+ over APC
- Performance. Zend Optimizer+ has a consistent performance edge over APC, which, depending on the code, can range between 5 and 20% in terms of requests/second. See Benchmarks section below.
- Availability for new PHP versions. Optimizer+ is typically fully compatible with PHP releases even before they come out; While this advantage was rarely realized because of the closed-source nature of the component, once open-source, both Zend and the community will help ensure that it’s always fully compatible with every element of the PHP language, avoiding any lags.
- Reliability. Optimizer+ has optional corruption detection capabilities that can prevent a server-wide crash in case of data corruption (e.g. from a faulty implementation of a PHP function in C). This handles one of the very few downsides of using a shared-memory-based-opcode-cache – introducing a shared resource that – if corrupted – could bring down an entire server.
- Better compatibility. We strived to make Optimizer+ work with any and all constructs supported by PHP, in exactly the same way they’d behave without it.
Advantages of APC over Optimizer+
- Has a data caching API. APC has a data caching API which Optimizer+ does not have.
- APC can reclaim memory of old invalidated scripts. APC uses a memory manager and can reclaim memory associated with a script that is no longer in use; Optimizer+ works differently, and marks such memory as ‘dirty’, but never actually reclaims it. Once the dirty percentage climbs above a configurable threshold – Optimizer+ restarts itself. Note that this behavior has both stability advantages and disadvantages.
- Check if PHP is in version 5.5+ (php -v). If not, install it (and every dependencies) from remi repo[bash]sudo yum remove httpd php php-*
sudo yum –enablerepo=remi install httpd php php-common
sudo yum –enablerepo=remi install php-devel php-cli php-pear php-pdo php-mysqlnd php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml
systemctl enable httpd.service
- Check whether PHP has opcache installed or not[bash]php -i | grep opcache[/bash]
. If not, install opcache from pecl:
[bash]sudo pecl install zendopcache-7.0.3
echo "zend_extension=/usr/lib64/php/modules/opcache.so" > /etc/php.d/opcache.ini[/bash]
(Since zend opcache does not have stable version in pecl this time, so we must specify a version of opcache. Also remember to change the path of opcache.so file if the system is based on 32bit)
- Restart httpd