How to Install and Configure PHP OPcache on Ubuntu 20.04

how to install and configure php opcache on 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 removes the need for PHP to load and parse scripts on each request.

In this tutorial, we will show you how to install and configure PHP OPcache on an Ubuntu 20.04 VPS.

Prerequisites

  • An Ubuntu 20.04 VPS (we’ll be using our SSD 2 VPS plan)
  • Access to the root user account (or access to an admin account with root privileges)

Log in to the Server & Update the Server OS Packages

First, log in to your Ubuntu 20.04 server via SSH as the root user:

ssh root@IP_Address -p Port_number

You will need to replace ‘IP_Address’ and ‘Port_number’ with your server’s respective IP address and SSH port number. Additionally, replace ‘root’ with the username of the admin account if necessary.

Before starting, you have to make sure that all Ubuntu OS packages installed on the server are up to date. You can do this by running the following commands:

apt-get update -y
apt-get upgrade -y

Install and Configure PHP OPcache with Apache

In this section, we will show you how to install PHP OPcache and enable it for the Apache web server.

Install Apache and PHP

First, you will need to install Apache, PHP, and other PHP extensions to your server. You can install them with the following command:

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

Once all the packages are installed, verify the PHP version with the following command:

php -version

You should get the following output:

PHP 7.4.3 (cli) (built: Oct  6 2020 15:47:56) ( 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

Configure OPcache

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

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

Uncomment the following lines:

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

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

systemctl restart apache2

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 => 180 => 180

Install and Configure PHP OPcache with Nginx

In this section, we will show you how to install PHP OPcache with Nginx web server.

Install PHP and Nginx

First, install the Nginx, PHP, and other PHP extensions with the following command:

apt-get install nginx php php-fpm php-cli php-opcache php-mysql php-zip php-gd php-mbstring php-curl php-xml -y

Once all the packages are installed, verify the PHP version with the following command:

php -version

You should get the following output:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
PHP 7.4.3 (cli) (built: Oct  6 2020 15:47:56) ( 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

Configure OPcache

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

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

Uncomment the following lines:

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

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

systemctl restart nginx php7.4-fpm

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 => 180 => 180
installing and configure php opcache on ubuntu 20.04

Of course, you don’t have to do any of this if you use one of our PHP VPS Hosting services, in which case you can simply ask our expert Linux admins to setup this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment