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: mirror.es.its.nyu.edu
* epel: mirror.math.princeton.edu
* extras: mirrors.rit.edu
* remi: ca.mirror.babylon.network
* remi-php56: ca.mirror.babylon.network
* remi-safe: ca.mirror.babylon.network
* updates: centos.mirror.nac.net
=============================================== 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 'opcache.so'
/opt/remi/php70/root/usr/lib64/php/modules/opcache.so

 

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
zend_extension=/opt/remi/php70/root/usr/lib64/php/modules/opcache.so
;
; Determines if Zend OPCache is enabled
opcache.enable=1
;
; Determines if Zend OPCache is enabled for the CLI version of PHP
; ;opcache.enable_cli=0
;
; The OPcache shared memory storage size.
opcache.memory_consumption=128
;
; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=8
;
; The maximum number of keys (scripts) in the OPcache hash table.
; ; Only numbers between 200 and 100000 are allowed.
opcache.max_accelerated_files=10000
;
; If enabled, a fast shutdown sequence is used for the accelerated code.
opcache.fast_shutdown=1
;
; following can be commented for production server.
opcache.revalidate_freq=360
; 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
/home/debyum/public_content/important/*

 

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
opcache.blacklist_filename=/etc/opt/remi/php70/opcache-blacklist.txt

 

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

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

 

Conclusion

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:
engy

engy

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: