×


Install PHP OPcache on Ubuntu 20.04 LTS - Step by step guide ?

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. Therefore, any consequent requests for the same script then OpCache stores this script on it memory on the first execution, to be reused afterward, thus leading to performance boosts.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related PHP queries.

In this context, we shall look into how to install PHP OPcache on Ubuntu 20.04 LTS. 


Steps to Install PHP OPcache on Ubuntu 20.04 LTS Focal Fossa 

1. Perform System Update

To begin, ensure that all your system packages are up-to-date by running the following apt commands in the terminal:

$ sudo apt update
$ sudo apt upgrade


2. Install PHP OPcache on the system

Ubuntu 20.04 ships with PHP 7.4 in its upstream repositories. 

So install it and the extensions with the apt package manager command:

$ sudo apt install php php-cli php-fpm php-opcache php-mysql php-zip php-gd php-mbstring php-curl php-xml

Once the package has finished installing, we can test PHP in the command line:

$ php -version
PHP 7.4.3 (cli) (built: May 20 2021 18:46:36) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies


How to Configure PHP OPcache on the Ubuntu system ?

Now we open the PHP configuration file with the following command.

For Apache module, run the below command:

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

For Nginx module, run the below command:

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

Then work on the following settings for OPcache for good performance.

You can enable a configuration by uncommenting it:

opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=3000
opcache.revalidate_freq=200

Finally, you need to restart php-fpm and Apache or Nginx for reflecting the changes that you made:

$ sudo systemctl restart apache2 ### Apache
$ sudo systemctl restart nginx   ### Nginx
$ sudo systemctl restart php7.4-fpm


[Need help in configuring Nginx ? We can help you. ]


Conclusion

This article covers the complete procedure for installing the PHP OPcache on your Ubuntu 20.04 LTS Focal Fossa system. In fact, OPcache is an Apache module for the PHP interpreter which is used to increase performance by storing precompiled scripts in shared memory space. It basically removes the need for PHP to load and parse scripts on each request.


How to Install and Configure PHP OPcache with Nginx ?

1. First, install the Nginx, PHP and other PHP extensions with the following command:

$ apt-get install nginx php php-fpm php-cli php-opcache php-mysql php-zip php-gd php-mbstring php-curl php-xml -y

2. Once all the packages are installed, verify the PHP version with the following command:

$ php -version

3. Next, you will need to enable the PHP OPcache by editing php.ini file.

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

Uncomment the following lines:

opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.revalidate_freq=200

Next, Save and close the file then restart Apache service to apply the changes:

$ systemctl restart nginx php7.4-fpm

4. You can now verify the PHP OPcache installation with the following command:

$ php -i | grep opcache