How To Install PHP OPcache on Ubuntu 22.04 LTS
In this tutorial, we will show you how to install PHP OPcache on Ubuntu 22.04 LTS. For those of you who didn’t know, OPcache is a popular open-source PHP extension that helps speed up the performance of PHP scripts. It caches the compiled version of PHP scripts in memory, which reduces the overhead of loading and compiling these scripts each time they are executed. This can result in significant performance gains, particularly on busy websites.
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 the PHP OPcache on Ubuntu 22.04 (Jammy Jellyfish). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.
Prerequisites
- A server running one of the following operating systems: Ubuntu 22.04, 20.04, and any other Debian-based distribution like Linux Mint.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies for PHP OPcache.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-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 22.04 LTS Jammy Jellyfish
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 sudo apt install wget apt-transport-https gnupg2 software-properties-common
Step 2. Installing PHP.
By default, PHP is not available on Ubuntu 22.04 base repository. Now run the following command below to install newer versions of PHP 8.2, you must add this PPA to your Ubuntu system:
sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt update
After successfully adding the PPA repository, update the package list and now you can install PHP 8.2 on Ubuntu 22.04:
sudo apt install php8.2
To check the version of PHP currently installed on the system run the following command:
php -v
Output:
[root@idroot.us]# php -v PHP 8.2.2 (cli) (built: Jan 31 2023 13:31:55) (NTS gcc x86_64) Copyright (c) The PHP Group Zend Engine v4.2.2, Copyright (c) Zend Technologies
For additional resources on installing PHP, read the post below:
Step 3. Installing PHP OPcache on Ubuntu 22.04.
Now run the following command to install the OPcache extension:
sudo apt install php8.2-opcache
After installing OPcache on Ubuntu 22.04 LTS, you need to configure PHP to use the extension. To do this, follow these steps:
nano /etc/php/8.2/apache2/php.ini
You’ll need to update your php.ini
file with the following recommended settings:
opcache.memory_consumption=128 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=4000 opcache.revalidate_freq=60 opcache.fast_shutdown=1 opcache.enable_cli=1
Once you have modified your PHP settings you need to restart your Apache for the changes to take effect:
sudo systemctl restart apache2
Finally, check the version of PHP using the following command below:
php -v
Output:
[root@idroot.us]# php -v PHP 8.2.2 (cli) (built: Jan 31 2023 13:31:55) (NTS gcc x86_64) Copyright (c) The PHP Group Zend Engine v4.2.2, Copyright (c) Zend Technologies with Zend OPcache v8.2.2, Copyright (c), by Zend Technologies
Congratulations! You have successfully installed OPcache. Thanks for using this tutorial for installing the PHP OPcache on Ubuntu 22.04 LTS Jammy Jellyfish system. For additional help or useful information, we recommend you check the official OPcache website.