Tutorials > How to install and configure PHP OPcache on Ubuntu 18.04

How to install and configure PHP OPcache on Ubuntu 18.04

Published on: 18 January 2020

Apache Caching NGINX PHP

OPcache is an Apache module for the PHP interpreter that allows to increase its performance by storing precompiled scripts in the shared memory. In this way, PHP does not have to load and interpret the same script at every request.

First, connect to your server via an SSH connection. If you haven’t done so yet , following our guide is recommended to securely connect with SSH. In case of a local server, go to the next step and open the terminal of your server.

Enabling OPcache

Open the PHP configuration file with the following command.

If PHP is interpreted using the Apache module:

$ sudo nano /etc/php/7.2/apache2/php.ini

If PHP is interpreted via PHP-FPM / NGINX:

$ sudo nano /etc/php/7.2/fpm/php.ini

To enable the use of OPcache, all you need to do is enable the related item, by changing the following line:

;opcache.enable=0

with this:

opcache.enable=1

remove the; initial if present.

At this point, OPcache will already be enabled, when restarting the web server:

Apache:

$ sudo service apache2 restart

PHP-FPM / NGINX:

$ sudo service nginx restart

Now, to check the correct functioning of OPcache, create the phpinfo.php file inside the folder where your site is and insert the following code in it:

<?php

phpinfo();

?>

Then, save the file and view it in the browser.

If the OPcache section is present, as you can see in this image, then OPcache is working correctly.

OPcache Phpinfo

Now let's see how to specifically configure each option of this module. 

RAM memory to use

In this case, you have to find the rightbetween the RAM memory, which will be used by OPCache, and the desired speed. An expanded memory doesn’t necessarily entail an actual performance improvement, since OPCache saves the rarely used instructions too. This will result in the waste of the allocated memory and consequent no difference in the performance.

Following our guide is therefore recommended to change the default value (64 MegaBytes) for general use

;opcache.memory_consumption=64

into 128 MegaBytes, as follows:

opcache.memory_consumption=128

Then, try to increase this value by verifying the result in terms of performance.

Number of scripts to be stored

In addition to the memory available, OPcache allows you to set the number of scripts to store in the cache., This value can normally be left as default (2000). However, if the hit rate of your application is not close to 100% you can try to increase it according to your needs by uncommenting the following line:

;opcache.max_accelerated_files=2000

increase the value, as follows:

opcache.max_accelerated_files=3000

trying to get the hit rate closer and closer to 100%:

Update frequency

OPcache periodically checks if the stored scripts have been updated. The assessment period can be set by changing the following directive:

;opcache_revalidate_freq = 2

and the default value (into seconds):

opcache_revalidate_freq = 100

How to apply changes

To confirm the changes, restart your web server.

Apache:

$ sudo service apache2 restart

NGINX:

$ sudo service nginx restart

At this point OPcache will start to save your scripts.