How to Install PHP OPcache on Ubuntu 20.04

Install PHP OPcache on Ubuntu 20.04

In this article, we will have explained the necessary steps to install and configure PHP OPcache on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

OPcache is an Apache module for the PHP interpreter. It is used to increase performance by storing precompiled scripts in shared memory space. It is a caching module built into PHP. When it is enabled, it surprisingly increases the performance of PHP-based websites.

Prerequisite:

  • Operating System with Ubuntu 20.04
  • Server IPv4 Address with Superuser Privileges (Root Access)
  • Gnome Terminal for Linux Desktop
  • PuTTy SSH client for Windows or macOS
  • Powershell for Windows 10/11
  • Familiar with APT Commands

Install PHP OPcache on Ubuntu 20.04

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Install PHP OPcache on the Ubuntu system.

  • Install Opcache with Apache.

You will need to install Apache, PHP, and other PHP extensions to your server:

sudo apt install apache2 libapache2-mod-php php php-cli php-opcache

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

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

Add the following lines:

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

Save and close the file then restart the Apache service:

sudo systemctl restart apache2
  • Install Opcache with Nginx.

Now install the Opcache extension with the following command:

sudo apt install nginx php-fpm php-cli php-opcache

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

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

Change the following lines:

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

Save and close the file then restart the Nginx service:

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

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

php -i | grep opcache

You should get the following output:

/etc/php/7.4/cli/conf.d/10-opcache.ini,
opcache.blacklist_filename => no value => no value
opcache.consistency_checks => 0 => 0
opcache.dups_fix => Off => Off
opcache.enable => On => On
opcache.enable_cli => Off => Off
opcache.enable_file_override => Off => Off
opcache.error_log => no value => no value
opcache.file_cache => no value => no value
opcache.file_cache_consistency_checks => 1 => 1
opcache.file_cache_only => 0 => 0
opcache.file_update_protection => 2 => 2
opcache.force_restart_timeout => 120 => 120

That’s all you need to do to install PHP OPcache on Ubuntu 20.04 LTS Focal Fossa. I hope you find this quick tip helpful. For further reading on PHP OPcache, please refer to their official knowledge base. If you have questions or suggestions, feel free to leave a comment below.