How to install PHP opcache

OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.

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 18.04 with PHP 7.3 but approach is more or less the same for all Ubuntu versions.

For more detailed runtime options, take a look at the 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:

sudo nano /etc/php/7.3/apache2/php.ini


sudo nano /etc/php/7.3/fpm/php.ini

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


Change to:


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.


Change to:


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.


Change to:


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.


sudo service apache2 restart


sudo service nginx restart