Use OPcache for PHP 5.6

Oct 8, 2016 PHP 中文版

There are various ways to improve the performance of a PHP application, and using OPcache is an easy and simple way to achieve the goal. This post is about how to enable and set OPcache for your PHP applications.



Enable OPcache

Following steps will be executed in a PHP docker container (php:5.6.13-apache). In general, OPcache is bundled with PHP 5.6, so we don’t have to install it. All we have to do is to enable it. First, get the path of your OPcache:

# Enter into your container
docker exec -it <your-container-name> bash

# Get the path of your OPcache
find / -name "opcache.so"

Then put OPcache settings at the end of php.ini (e.g. /usr/local/etc/php/php.ini).

# Skip some content ...

# Add OPcache's path and settings
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=5000
opcache.fast_shutdown=1
opcache.enable_cli=1
opcache.save_comments=0
# You can disable below in production env
opcache.revalidate_freq=60

Don’t forget to restart the container to make sure OPcache can work correctly. To restart:

docker restart <your-container-name>

After the restart, you can check whether the OPcache is enabled or not:

# Get PHP info in your container
php -v

# Then you should get information about OPcache, like this:
PHP 5.6.13 (cli) (built: Sep 23 2015 19:47:17)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies


Benchmarking with OPcache

I used Apache HTTP server benchmarking tool (ab) to do benchmarking. The command:

ab -n 10000 -c 20 http://your-app

Here are the result of benchmarking:



You might also like:




If you have any suggestions, questions or even find some typos, feel free to contact me. Thank you! :)

zeckli.devforgalaxy@gmail.com   © 2015-2019 zeckli, thanks to Jekyll and GitHub.