Install and Configure PHP OPcache on Ubuntu 20.04 | 18.04

This brief tutorial shows students and new users how to install and configure PHP OPcache on Ubuntu 20.04 | 18.04.

OPcache is an Apache module for the PHP interpreter that can be used to increase performance by storing precompiled scripts in shared memory space.

Using OPcache, PHP doe not need to load and interpret the same script everytime there’s a request. This can speed up performance for PHP based applications.

For those using WordPress and other PHP applications, then may be helpful in speeding up WordPress and others CMS platforms.

Configure PHP | PHP-FPM

If you’re running Apache, then PHP will be used. For Nginx, PHP-FPM is used instead.

Open the PHP configuration file with the following command.

If PHP is interpreted using the Apache module:

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

If PHP is interpreted using Nginx module:

sudo nano /etc/php/7.4/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]
; Determines if Zend OPCache is enabled
;opcache.enable=1

with this:

[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1

remove the ; initial if present.

Save the file and you’re done.

Restart PHP

Apache:

sudo systemctl restart apache2

Nginx | PHP-FPM

sudo systemctl restart php7.4-fpm
sudo systemctl restart nginx

Improvement Settings

The steps above enable OPcache. However, the default settings might not provide much improvements.

Use the line settings below to improve performance.

Increase memory consumption.

opcache.memory_consumption=128

Number of scripts stored

opcache.max_accelerated_files=3000

Update frequency

opcache_revalidate_freq = 200

Save the file and exit. Then restart PHP again.

That’s it!

Conclusion:

This post showed you how to configure OPcache to improve performance for PHP based applications. If you find any error above, please report below:

You may also like the post below:

One Reply to “Install and Configure PHP OPcache on Ubuntu 20.04 | 18.04”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.