How to Install and Configure PHP OPcache on Ubuntu 22.04

how to install and configure php opcache on ubuntu 22.04

OPcache is a PHP module used for caching pre-compiled script bytecode in the shared memory. It can speed up your website up to 3 times.

Whether you are running a simple blog, a complex web application, or an e-commerce site, Opcache can significantly enhance your application’s responsiveness by eliminating the need for repetitive PHP script compilation.

Opcache stores pre-compiled bytecode in shared memory, allowing PHP to skip the compilation step and execute code directly from memory.

This will result in dramatically reduced response times and an overall improvement in the performance of your PHP-powered website.

Prerequisites

  • A server with Ubuntu 22.04 as OS
  • User privileges: root or non-root user with sudo privileges

Step 1. Update the System


Log in to the Server & Update the Server OS Packages
First, log in to your Ubuntu 22.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

Step 2. 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.

First, you will need to install Apache, PHP, and other PHP extensions on 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 8.1.2-1ubuntu2.13 (cli) (built: Jun 28 2023 14:01:49) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.13, Copyright (c), by Zend Technologies


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

nano /etc/php/8.1/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 the 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:

…
opcache.file_cache_consistency_checks => On => On
opcache.file_cache_only => Off => Off
opcache.file_update_protection => 2 => 2
opcache.force_restart_timeout => 180 => 180
opcache.huge_code_pages => Off => Off
opcache.interned_strings_buffer => 8 => 8
opcache.jit => tracing => tracing
opcache.jit_bisect_limit => 0 => 0
opcache.jit_blacklist_root_trace => 16 => 16
opcache.jit_blacklist_side_trace => 8 => 8
opcache.jit_buffer_size => 0 => 0
opcache.jit_debug => 0 => 0
opcache.jit_hot_func => 127 => 127
opcache.jit_hot_loop => 64 => 64
opcache.jit_hot_return => 8 => 8
opcache.jit_hot_side_exit => 8 => 8
opcache.jit_max_exit_counters => 8192 => 8192
…

Step 3. Configure PHP OPCache on NGINX

To install and enable the Opcache PHP module on your server running with Nginx.

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

First, you will install the Opcache extension with the following command:

apt-get install php-opcache php-fpm -y

Then you can enable the Opcache extension in the php.ini file and open the php.ini file with your favorite editor.

nano /etc/php/8.1/fpm/php.ini

And change the following lines for the best performance.

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

Then go ahead and save the file and restart the NGINX and php-fpm services to apply the changes.

systemctl restart nginx
systemctl restart php8.1-fpm

That was all. You successfully installed and enabled OPcache on Ubuntu 22.04.

If you do not want to do this alone, sign up for one of our NVMe VPS plans and submit a support ticket. Our admins are available 24/7 and will start to work on your request immediately. Always trust our epic support.

If you liked this post on installing OPcache on Ubuntu 22.04, please share it with your friends on social networks or simply leave a reply below. Thanks.

Leave a Comment