How to use Zend Opcache to Speed up your PHP Application?

share on:
install zend opcache

Zend Opcache is Open Source Php extension that will help you speed up your PHP execution. It improves Server and Application Performance in a great way. It is easy to install Zend Opcache in any Linux distribution.

Zend Opcache stores the precompiled script bytecode in shared memory to reduce the need for reading the code again and again.

In this post we will install Zend Opcache on CentOS 7. we will also know about blacklisting a directory in Zend Opcache.

Also, do check this guide to secure your PHP installation.

Top 10 PHP Security best practices for Linux sys administrators.

Another good reason to use Opcache is that it is more stable and faster than APC.

About this guide:


Install Zend opcache.

To install Zend Opcache on CentOS 7, we need to install EPEL repository on CentOS 7

[root@debyum ~]# yum update
[root@debyum ~]# yum install epel-release


Search for the zendopcache” package.

[root@debyum ~]# yum search zendopcache
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base:
* epel:
* extras:
* remi:
* remi-php56:
* remi-safe:
* updates:
=============================================== N/S matched: zendopcache ===============================================
php-pecl-zendopcache.x86_64 : The Zend OPcache
php54-php-pecl-zendopcache.x86_64 : The Zend OPcache


we will install php-pecl-zendopcache package.

[root@debyum ~]# yum install php-pecl-zendopcache


To install Zend Opcache on CentOS 7, search the name of Opcache module in the list of PHP 7.0 packages. Install the package.

[root@debyum ~]# yum search php70 | grep '^php70*'
[root@debyum ~]# yum install php70-php-opcache


Also, To install Zend Opcache in PHP 7.1, we can follow the same method and install.

[root@debyum ~]# yum install php71-php-opcache


Next, Find the location of Zend Opcache extension Module. In my server the location was:

[root@debyum ~]# find / -type f -name ''


Create or Edit the opcache.ini file. Enter the location of Zend Opcache extension Module in zend_extension configuration value.

[root@debyum ~]#  vi /etc/php.d/opcache.ini

; Enable Zend OPcache extension module
; Determines if Zend OPCache is enabled
; Determines if Zend OPCache is enabled for the CLI version of PHP
; ;opcache.enable_cli=0
; The OPcache shared memory storage size.
; The amount of memory for interned strings in Mbytes.
; The maximum number of keys (scripts) in the OPcache hash table.
; ; Only numbers between 200 and 100000 are allowed.
; If enabled, a fast shutdown sequence is used for the accelerated code.
; following can be commented for production server.
; opcache.consistency_checks=1


You can find more runtime configuration and explanation of the configuration directives here.


To check Zend Opcache status in Php 5.5.

[root@debyum ~]# php -v
PHP 5.5.8 (cli) (built: Oct 13 2016 14:21:08)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
with Zend OPcache v7.0.3-dev, Copyright (c) 1999-2013, by Zend Technologies


To check Zend Opcache status in Php 7.0.12.

[root@debyum ~]# php70 -v
PHP 7.0.12 (cli) (built: Oct 13 2016 16:56:51) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.12, Copyright (c) 1999-2016, by Zend Technologies



Blacklisting a directory.

We can prevent Zend Opcache from caching any files from a specific directory. We can do this by adding name of that directory in opcache.blacklist_filename configuration value.

To stop Zend Opcache from caching any files from a specific directory. For example, To block /home/debyum/public_content/important/*, you can use opcache-blacklist file option to do that.


I m using PHP 7.0 here and the default opcache-default.blacklist file is in /etc/opt/remi/php70/php.d  directory.

Next, You will see the following information inside this file.

; The blacklist file is a text file that holds the names of files
; that should not be accelerated. The file format is to add each filename
; to a new line. The filename may be a full path or just a file prefix
; (i.e., /var/www/x  blacklists all the files and directories in /var/www
; that start with 'x'). Line starting with a ; are ignored (comments).


Now you can either put your configuration in this file or create another file.

we will create a new file and add the name of the directory you don’t want Opcache to cache anymore.

[root@debyum ~]# vi /etc/opt/remi/php70/opcache-blacklist.txt


Now add this blacklist file path to your php.ini.

To find your loaded  php.ini  file path, you can use:

[root@debyum ~]# php -i | grep "Loaded Configuration File"
Loaded Configuration File => /etc/php.ini


And if you are using PHP 7.0 then you should follow these steps.

[root@debyum ~]# php70 -i | grep "Loaded Configuration File"
Loaded Configuration File => /etc/opt/remi/php70/php.ini


Now, edit  php.ini  file and add the configuration.

[root@debyum ~]# vi /etc/opt/remi/php70/php.ini


Finally. Save this file and Reload the  PHP-FPM  service.

[root@debyum ~]# systemctl restart php70-php-fpm.service



Zend Opcache is easy to set up and it can help you decrease the latency of your PHP application. So it is helpful to use it in your Production PHP environments.

If you have set opcache.revalidate_freq=0  then you must delete the Opcache cache whenever you choose to deploy new code.

Finally, if I am missing something here then please let me know in the comments below.  I’ll add it in the post to improve it.

Thanks for visiting this page and Have a Great Day. 🙂


share on:


Hello there, My name is Rishi Guleria and I work as a Linux system administrator. I have created this blog to share what I have learned so far and to learn new things. Don't forget to leave the feedback. Have a great day. :)

Leave a Response

share on: