Categories: TutorialsUbuntu

How to Install PHP Opcache Module on Ubuntu 20.04

OPcache is a PHP interpreter module for Apache. It boosts performance by storing precompiled scripts in a shared memory pool. The module is typically used to speed up and enhance the performance of PHP web applications such as WordPress, Drupal, and custom PHP sites. With OPcache, there is no need to load and parse PHP scripts for each request.

With that out of the way, let focus on how to install and enable PHP Opcache module on Ubuntu 20.04.

Step 1: Update Ubuntu 20.04

Firstly, ensure that the package lists are refreshed as follows

$ sudo apt update

This ensures the repositories and package lists are up to date. Once complete, head over to the next step.

Step
Sponsored
2: Install Apache and PHP

First install Apache, PHP, and the required PHP extensions on your system. Execute the commands:

$ sudo apt-get install apache2 libapache2-mod-php php php-opcache php-mysql php-mbstring php-cli php-zip php-gd php-curl php-xml -y

After the installation, verify the PHP version as shown:

$ php -v

Step 3: Configure PHP Opcache

In this step, we will configure Opcache. First, edit the php.ini file to enable the Opcache service. Run:

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

Next, uncomment the following lines:

opcache.enable=1

opcache.memory_consumption=128

opcache.max_accelerated_files=10000

opcache.revalidate_freq=200

Next, save the file and exit. To effect the changes made, reload the webserver.

$ sudo systemctl restart apache2

To confirm or check that the OPcache module has been loaded, execute the command:

$ php -i | grep opcache

The output is as shown below:

Sponsored

Install and Configure PHP OPcache with Nginx

You can choose to install PHP OPcache with the Nginx web server instead of Apache. Follow the steps given below:

Step 1: Install PHP and Nginx

Start off by installing Nginx, PHP, and the required PHP extensions as shown:

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

To confirm the installation was successful, check the PHP version installed as follows:

$ php -V

Step 2: Configure OPcache

In this step, we will configure Opcache. First, edit the php.ini file to enable the Opcache service. Run:

$ sudo vim /etc/php/7.4/fpm/php.ini

Yet again, uncomment the following lines as we saw earlier with Apache.

opcache.enable=1

opcache.memory_consumption=128

opcache.max_accelerated_files=10000

opcache.revalidate_freq=200

Next, save the file and exit. Finally, reload the web server and PHP-FPM service to apply the changes.

$ sudo systemctl restart nginx php7.4-fpm

To verify the PHP Opcache installation, execute the command:

$ php -i | grep opcache

PHP Opcache is now successfully installed on Ubuntu 20.04:

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.

Ubuntu Server Admin

Recent Posts

How we used Flask and 12-factor charms to simplify Canonical.com development

Our latest Canonical website rebrand did not just bring the new Vanilla-based frontend, it also…

5 hours ago

Web Engineering: Hack Week 2024

At Canonical, the work of our teams is strongly embedded in the open source principles…

1 day ago

Ubuntu Weekly Newsletter Issue 873

Welcome to the Ubuntu Weekly Newsletter, Issue 873 for the week of December 29, 2024…

3 days ago

How to resolve WiFi Issues on Ubuntu 24.04

Have WiFi troubles on your Ubuntu 24.04 system? Don’t worry, you’re not alone. WiFi problems…

3 days ago

Remembering and thanking Steve Langasek

The following is a post from Mark Shuttleworth on the Ubuntu Discourse instance. For more…

3 days ago

How to Change Your Prompt in Bash Shell in Ubuntu

I don’t like my prompt, i want to change it. it has my username and…

3 days ago