In this tutorial, we will show you how to install PHP OPcache on Ubuntu 20.04 LTS. For those of you who didn’t know, 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, it simply means 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.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘
sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of PHP OPcache on Ubuntu 20.04 (Focal Fossa). You can follow the same instructions for Ubuntu 18.04, 16.04, and any other Debian-based distribution like Linux Mint.
- A server running one of the following operating systems: Ubuntu 20.04, 18.04, and any other Debian-based distribution like Linux Mint or elementary OS.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
non-root sudo useror access to the
root user. We recommend acting as a
non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.
Install PHP OPcache on Ubuntu 20.04 LTS Focal Fossa
Step 1. First, make sure that all your system packages are up-to-date by running the following
apt commands in the terminal.
sudo apt update sudo apt upgrade
Step 2. Installing PHP OPcache on Ubuntu 20.04.
Ubuntu 20.04 ships with PHP 7.4 in its upstream repositories. Just install it and the extensions you with the apt package manager:
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 2020 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
Step 3. Configure PHP OPcache on the Ubuntu system.
Now we open the PHP configuration file with the following command:
PHP is interpreted using the Apache module:
sudo nano /etc/php/7.4/apache2/php.ini
PHP is interpreted using the Nginx module:
sudo nano /etc/php/7.4/fpm/php.ini
The following settings should get you started with using OPcache and are generally recommended 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
Congratulations! You have successfully installed PHP OPcache. Thanks for using this tutorial for installing the PHP OPcache on your Ubuntu 20.04 LTS Focal Fossa system. For additional help or useful information, we recommend you check the official PHP website.