Install and Configure PHP OPcache on Ubuntu 20.04

Opcache is a powerful PHP extension used to increase PHP performance by storing precompiled script bytecode in shared memory. So PHP does not need to load and parse scripts on each request. This will speed up the performance of PHP based applications.

In this tutorial, we will show you how to install and enable the Opcache PHP extension with Apache and Nginx on Ubuntu 20.04.

Prerequisites

  • A server running Ubuntu 20.04.
  • A root password is configured the server.

Install Opcache with Apache

In this section, we will show you how to install and enable the Opcache PHP module on the server running Apache.

First, install the Opcache extension with the following command:

apt-get install php-opcache -y

Once the Opcache is installed, edit the php.ini file to enable the Opcache extension.

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

Change the following lines for good performance.

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

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

systemctl restart apache2

Install Opcache with Nginx

In this section, we will show you how to install and enable the Opcache PHP module on the server running Nginx.

First, install the Opcache extension with the following command:

apt-get install php-opcache php-fpm -y

Once the Opcache is installed, edit the php.ini file to enable the Opcache extension.

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

Change the following lines for good performance.

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

Save and close the file then restart the Nginx and PHP-FPM service to apply the configuration changes:

systemctl restart nginx
systemctl restart php7.4-fpm

Conclusion

Congratulations! you have successfully installed and enabled PHP Opcache extension with Nginx and Apache on Ubuntu 20.04. You can now speed up your PHP-based applications easily with Opcache.

Share this page:

Suggested articles

2 Comment(s)

Add comment

Comments

By: ustoopia at: 2021-02-10 19:00:13

Thanks!

By: Mayank talapda at: 2021-02-11 06:07:25

Jethva You explained very well