How to Install and Configure PHP OPcache on Ubuntu 20.04
January 18, 2022In this tutorial, we will show you how to install and configure PHP OPcache on an Ubuntu 20.04
OPcache is an Apache module for the PHP interpreter. It is used to increase performance by storing precompiled scripts in shared memory space. Generally, it is used to speed up the performance of WordPress and PHP-based applications. OPcache removing the need for PHP to load and parse scripts on each request.
Install Apache and PHP
Update all available repositories on your system and install the Apache webserver using the apt command below.
sudo apt update && sudo apt upgrade -y
First, you will need to install Apache, PHP and other PHP extensions to your server, if you already have Apache and PHP installed on your server you can skip this step, just make sure you have the php-opcache
PHP extension. You can install them with the following command:
sudo apt-get install apache2 libapache2-mod-php php php-cli php-opcache php-mysql php-zip php-gd php-mbstring php-curl php-xml -y
Configure OPcache
Next, you will need to enable the PHP OPcache by editing php.ini file.
sudo nano /etc/php/7.4/apache2/php.ini
Uncomment the following lines from the php.ini file:
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.revalidate_freq=200
Save and close using the CTRL + X
combination, then press Y
and confirm by pressing ENTER
. After closing the file then restart Apache service to apply the changes: