tips@lastplaceonthe.net

How To: Enable PHP 7 OPcache on Ubuntu 16.04

Reading Time: 2 minutes

When looking for how to enable OPcache on Ubuntu 16.04 and PHP 7.0, I found several guides for legacy platforms such as Ubuntu 14.04 and PHP 5.5

Many sites, particularly those built on CMS frameworks such as WordPress, Joomla and Drupal, use a complex code and consume many processes. Several website speed enhancement plugins are available, heavily featured within the yield of a typical search engine recommendation. Most of them do not explain how to enable opcode caching server-side – thankfully, it’s simple to implement.

A cursory search for ways to speed up websites will yield plugins and bolt on that only address part of the requirement. The Power Users amongst us need to know how to enable functionality server-side. This short guide will cover off the basics to get you started with OPcache on Ubuntu 16.04 with PHP 7.

For more detailed runtime options, take a look at the php.net site which has a page of all the runtime options available.

To get OPcache set up requires some changes in the php.ini file on your server.

Open php.ini in your favourite text editor

To get started open your php.ini file. Depending on your configuration, this will be in one of two locations:

Apache web servers

/etc/php/7.0/apache2/php.ini

Nginx web servers

/etc/php/7.0/fpm/php.ini

Enable OPcache

To enable the OPcache, change to the following lines of your php.ini file:

;opcache.enable=0

Change to:

opcache.enable=1

Note: Remember to uncomment this line and other configuration lines (remove the “;”) as well as change the “0″ to “1″ Otherwise your change won’t take effect.

Modify the amount of RAM the OPcache will use

With OPcache, there is a trade-off between speed and the amount of RAM used. The more RAM you are willing to dedicate to storing opcode, the more opcode that can be stored. There is a diminishing return at some point, because some code will execute rarely, or your code base might not be that big. It is worth playing with this setting to see where you get the best performance-versus-RAM trade-off.  This setting is in megabytes.

;opcache.memory_consumption=64

Change to:

opcache.memory_consumption=128

Boost the number of scripts that can be cached

OPcache has a strange setting that requires you to not only adjust the amount of RAM but also define the number of scripts that can be cached. You have the option of tuning this parameter for your own application too, especially if you find that your hit rate is not close to 100 percent.

;opcache.max_accelerated_files=2000

Change to:

opcache.max_accelerated_files=4000

Change the revalidate frequency

To make sure that the OPcache notices when you change your PHP code, you can set the revalidate frequency. Basically, this will tell the cache how often to check the timestamp on the files. This is measured in seconds.

;opcache_revalidate_freq = 2

Change to:

opcache_revalidate_freq = 240

Verify that the PHP OPcache mod is enabled

Believe it or not, that converts most of the settings you will need to get started. PHP7 has its own module system (since 5.4), so make sure that OPcache is enabled.

sudo phpenmod opcache

Restart PHP

You should now be all set to start using PHP 7’s OPcache. You just need to restart the appropriate service to get it going.

Apache web servers

sudo service apache2 restart

Nginx web servers

sudo service nginx restart

You’re done! Enjoy.

Inspiration taken from Hosting Advice. Credit also to the commentors of that post. I've simply gelled the info into a guide for the current trunk versions.
Featured image credit: Michelangelo van Dam (Flickr - Creative Commons)
%d bloggers like this: