How to Install and Configure PHP OPcache on Ubuntu 20.04

How to Install and Configure PHP OPcache on Ubuntu 20.04

This post helps new users and students how to install and configure PHP OPcache on Ubuntu 20.04.

OPcache is an Apache server module for the PHP interpreter that can be used for increase performance and speed by storing pre-compiled scripts bytecode in shared memory space. It is a caching module built into PHP. When it is enabled, it surprisaly increases the performance of PHP based websites.

If you are using WordPress and other PHP based applications and websites, then it is helpful in speeding up WordPress and PHP based applications and websites.

Configure PHP | PHP-FPM

If you are running Apache server, then PHP will be used. And if you running Nginx server ,then PHP-FPM is used.

Run the following command for open the PHP configuration file.

For Apache server module:

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

For Nginx server module:

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

Change the following line to enable the use of OPcache:

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

with this Line:

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

you just need to remove the ; initial if it is present.

Save the changes and you are done.

Restart PHP

For Apache server module:

sudo systemctl restart apache2

For Nginx server module:

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

Improvement Settings for better performance

Increase the memory consumption.

opcache.memory_consumption=128

Increase number of scripts stored.

opcache.max_accelerated_files=3000

Update the frequency.

opcache_revalidate_freq = 200

Save the changes and exit. And then restart PHP once again.

That’s All! , We have successfully Installed and Configured PHP OPcache on Ubuntu 20.04.

Leave a Comment

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.

Scroll to Top