What is Zend Opcache
Zend Opcache is a opcode cache.It stores the PHP script in shared memory.It is default opcache shipping with PHP versions greater than 5.5.Opcache improves performance of PHP.Lets understand this with an example.A visitor visits your website.To generate the webpage say first.php file is used.Now a second visitor visits other web page.This web page also uses first.php file.Then PHP can get this file from the opcache itself.So PHP does not have to load and parse the file for each request.Thus PHP becomes faster.
In recent times people used either APC or Xcache to speed up their PHP based website.APC stands for Alternate PHP Cache.I have used APC cache successfully on this website.But I have not used Xcache on this website.APC worked fine for me.There was even talks that it might become official opcode cache in future.But APC is not getting update in recent times.The last version was 3.1.13 in year 2012.So for more than two years there is no update.
Recently I dumped APC to use the Zend Opcache.So far the results have been good.
Configuring Zend Opcache
As Zend Opcache is installed by default with PHP versions greater than 5.5 there is no need to install it explicitly.Also there is no external dependency.If you are using Debian then you can add dotdeb repository and install PHP5.You can use the commands listed on this page.
You can refer installation instructions as per your operating system.After installation follow below steps to configure it.
- Go to the folder /etc/php5/fpm/conf.d.I am referring to Debian system.It will be same in case of Ubuntu I assume.But for other operating systems you can check the location of PHP5 folder
- Open the file 05-opcache.ini using any text editor.I prefer using nano text editor.
- Enter below statements in the file.You can just copy and paste below lines without any issues
; configuration for php ZendOpcache module ; priority=05 zend_extension=opcache.so opcache.memory_consumption=64 opcache.max_accelerated_files=2500 opcache.interned_strings_buffer=8 opcache.revalidate_freq=60 opcache.fast_shutdown=1
- opcache.memory_consumption – The number denotes amount of RAM dedicated for opcache.In this case it is 48MB.You can start with this memory size.Later in the tutorial we can see how to tune this parameter.
- opcache.max_accelerated_files – The number of files which needs to be accelerated with the opcache.Later in the tutorial we can see how to tune this parameter.
- The other parameters like opcache.interned_strings_buffer,opcache.revalidate_freq and opcache.fast_shutdown are just copied from official PHP opcache page.You can leave these options unchanged.
Tuning Zend Opcache
Now we have basic configuration of Zend opcache ready.Each website is different.The number of PHP files vary,the resources like RAM also vary.So fine tuning Zend opcache as per your website requirement is very important.To fine tune it follow below steps.
Installing Graphic viewer
The graphic viewer helps gives you idea about your opcache performance.Enter below command to change your directory to web root directory.
The directory will depend on your operating system.So modify the above command as per your requirement.Now enter below command to download this file in your web root directory
Now open this file in web browser (your-domain-name/opcache.php).You will see something like below
Now we will use the information shown by web viewer to get an idea about above configuration performance.Also we will use the information to tune opcache configuration.
- Whether the opcache is enabled or not.opcache_enabled option under status should be true
- used_memory – total amount of RAM used by opcache.In this case it is 41.31MB
- free_memory – total amount of free RAM.It is equal to amount of RAM you allocated in configuration minus the used RAM.In this case it is 21.06MB
Now based on above information you can set the amount of RAM dedicated to Zend Opcache.In this case the configuration has 64MB RAM and used memory is only 41MB.So we can safely dedicate 48MB RAM to opcache.This will free up some RAM as well.So you make above change and then after few days check the graphic viewer.
This parameter controls maximum number of PHP files opcache can have.Now on the graphic viewer you can see the script tab.The script tab gives idea about number of PHP files in opcache.In this case We have 2500 in configuration and number of files is only 568.So the maximum limit set in configuration is too much.We can safely tune the parameter to 1,000 instead of 2,500.
You can also check the parameter max_cached_keys.In case max_cached_key is less then max_accelerated_files then good.I have found misses to increase a bit if max_cached_keys is greater than max_accelerated_files.So you can increase max_accelerated_files to make it greater than max_cavched_keys.If the number is too large then you can keep it grater than max_cached_scripts (or scripts tab as mentioned above).You can make above change in the configuration and see the performance after few days or hours.
Determining Zend Opcache Cache performance
Now above we have only referred some parameters of graphic viewer.Based on above we can configure the Zend Opcache.Now it is time to check the performance of cache.As with most of the caches the parameter is hit ratio.Hit ratio is amount of requests catered by cache.The parameter opcache_hit_rate gives the idea.It is present on the status tab.You just need to scroll down.This parameter should be more than 99%.If it is then your cache configuration is working fine.
Also on the chart you can see four options Memory,Keys,Hits,Restarts.If you select Hits then you can get idea of cache hits and cache misses.The data is shown graphically and also numerically.If there is no red in the graph means cache is working great.Red denotes the cache misses.
Zend Opcache is available for PHP 5.5 as well as PHP 5.6.It is good to enable this as it will speed up your PHP website.I am using the opcache on this website without any issues.I would be trying out more configuration parameters.I will be updating the posts after that.
Consider sharing this post in case you found this useful.