install php opcache rocky alma centos

The OPcache is meant to improve the performance of PHP by storing precompiled script bytecode in shared memory. By caching PHP scripts in memory, it removes the need for PHP to load and parse scripts on each request it receives. When hosting websites or web applications build in PHP, utilizing OPcache will significantly enhance the responsiveness and the general load times.

This is how PHP OPcache works:

  1. Script compilation: OPcache compiles a script requested into bytecode.
  2. Bytecode caching: Once it’s compiled, the bytecode is stored in shared memory for faster access.
  3. Subsequent requests execution: Next time a request is received for the same script, the cached bytecode is used hence bypassing compilation step resulting in quicker execution.

To summarize, by using OPcache you will experience faster execution time of PHP scripts, reduced footprint on CPU and memory, and better system scalability since it can handle more concurrent requests.

How then can you install and configure PHP OPcache on Rocky Linux / AlmaLinux, and CentOS Stream 9|8 Linux system. Follow along for the how-to.

Switch to PHP edition from REMI

We can only enable OPcache on a system with PHP installed. The systems mentioned distribute multiple versions of PHP in AppStream repositories. But Opcache is provided in REMI repositories.

To check if PHP is installed, execute the command:

$ php --version
PHP 8.2.13 (cli) (built: Nov 21 2023 09:55:59) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.2.13, Copyright (c) Zend Technologies

From my system we can confirm the version is 8.2. So, we can reset PHP module.

sudo dnf module reset php -y

Next we install Remi’s RPM repository:

  • Enterprise Linux 9 – (Rocky/Alma/CentoS Stream 9)
sudo dnf -y install https://rpms.remirepo.net/enterprise/remi-release-9.rpm
  • Enterprise Linux 8 – (Rocky/Alma/CentoS Stream 8)
sudo dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm

The installer will automatically set up EPEL repository as dependency. To validate REMI is working run:

$ sudo dnf -y repolist
repo id                                                                                 repo name
appstream                                                                               Rocky Linux 8 - AppStream
baseos                                                                                  Rocky Linux 8 - BaseOS
epel                                                                                    Extra Packages for Enterprise Linux 8 - x86_64
extras                                                                                  Rocky Linux 8 - Extras
remi-modular                                                                            Remi's Modular repository for Enterprise Linux 8 - x86_64
remi-safe                                                                               Safe Remi's RPM repository for Enterprise Linux 8 - x86_64

All PHP modules can be checked by running the following command:

 dnf module list php

Here is the sample output:

Rocky Linux 8 - AppStream
Name                                        Stream                                              Profiles                                                          Summary
php                                         7.2 [d][x]                                          common [d], devel, minimal                                        PHP scripting language
php                                         7.3 [x]                                             common [d], devel, minimal                                        PHP scripting language
php                                         7.4 [x]                                             common [d], devel, minimal                                        PHP scripting language
php                                         8.0 [x]                                             common [d], devel, minimal                                        PHP scripting language
php                                         8.2 [x]                                             common [d], devel, minimal                                        PHP scripting language

Remi's Modular repository for Enterprise Linux 8 - x86_64
Name                                        Stream                                              Profiles                                                          Summary
php                                         remi-7.2 [x]                                        common [d], devel, minimal                                        PHP scripting language
php                                         remi-7.3 [x]                                        common [d], devel, minimal                                        PHP scripting language
php                                         remi-7.4 [x]                                        common [d], devel, minimal                                        PHP scripting language
php                                         remi-8.0 [x]                                        common [d], devel, minimal                                        PHP scripting language
php                                         remi-8.1 [x]                                        common [d], devel, minimal                                        PHP scripting language
php                                         remi-8.2 [x]                                        common [d], devel, minimal                                        PHP scripting language
php                                         remi-8.3 [x]                                        common [d], devel, minimal                                        PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

Now, activate specific PHP module from REMI. From earlier output the PHP installed was 8.2, this means we should activate php:remi-8.2 module.

sudo dnf module install php:remi-8.2 -y

In most cases, it will upgrade your installed PHP version and not uninstalling the packages.

Dependencies resolved.
==============================================================================================================================================================================================================================
 Package                                               Architecture                                   Version                                                      Repository                                            Size
==============================================================================================================================================================================================================================
Upgrading:
 php-cli                                               x86_64                                         8.2.21-1.el8.remi                                            remi-modular                                         5.5 M
 php-common                                            x86_64                                         8.2.21-1.el8.remi                                            remi-modular                                         1.3 M
 php-fpm                                               x86_64                                         8.2.21-1.el8.remi                                            remi-modular                                         1.9 M
 php-mbstring                                          x86_64                                         8.2.21-1.el8.remi                                            remi-modular                                         583 k
 php-xml                                               x86_64                                         8.2.21-1.el8.remi                                            remi-modular                                         264 k
Installing dependencies:
 oniguruma5php                                         x86_64                                         6.9.9-1.el8.remi                                             remi-safe                                            213 k
Installing module profiles:
 php/common
Enabling module streams:
 php                                                                                                  remi-8.2

Transaction Summary
==============================================================================================================================================================================================================================
Install  1 Package
Upgrade  5 Packages

Total download size: 9.7 M
Is this ok [y/N]: y

Installing PHP OPcache

With the REMI’s RPM repository configured, we can search for available versions of OPcache.

$ sudo dnf search opcache
Last metadata expiration check: 0:07:55 ago on Sun 28 Jul 2024 10:12:13 PM UTC.
============================================================================================== Name & Summary Matched: opcache ===============================================================================================
php-opcache.x86_64 : The Zend OPcache
php56-php-opcache.x86_64 : The Zend OPcache
php70-php-opcache.x86_64 : The Zend OPcache
php71-php-opcache.x86_64 : The Zend OPcache
php72-php-opcache.x86_64 : The Zend OPcache
php73-php-opcache.x86_64 : The Zend OPcache
php74-php-opcache.x86_64 : The Zend OPcache
php80-php-opcache.x86_64 : The Zend OPcache
php81-php-opcache.x86_64 : The Zend OPcache
php82-php-opcache.x86_64 : The Zend OPcache
php83-php-opcache.x86_64 : The Zend OPcache
php84-php-opcache.x86_64 : The Zend OPcache

In this example we are installing php82-php-opcache which is specific to PHP version 8.2. Change to a version suitable for your PHP installed.

sudo dnf install php82-php-opcache

More information about the package installed can be queried using rpm -qi commands:

$ rpm -qi php82-php-opcache
Name        : php82-php-opcache
Version     : 8.2.21
Release     : 1.el8.remi
Architecture: x86_64
Install Date: Sun 28 Jul 2024 10:21:52 PM UTC
Group       : Unspecified
Size        : 1024035
License     : PHP-3.01
Signature   : RSA/SHA256, Tue 02 Jul 2024 02:07:23 PM UTC, Key ID 555097595f11735a
Source RPM  : php82-php-8.2.21-1.el8.remi.src.rpm
Build Date  : Tue 02 Jul 2024 01:33:41 PM UTC
Build Host  : builder.remirepo.net
Relocations : (not relocatable)
Packager    : Remi Collet
Vendor      : Remi's RPM repository <https://rpms.remirepo.net/> #StandWithUkraine
URL         : http://www.php.net/
Bug URL     : https://forum.remirepo.net/
Summary     : The Zend OPcache
...

Configure PHP OPcache

Use the find command to locate OPcache module path.

$ sudo find / -name opcache.so
/usr/lib64/php/modules/opcache.so
/usr/lib64/php-zts/modules/opcache.so

The module should be loaded automatically upon installation.

$ php -m  | grep -i opcache
Zend OPcache
Zend OPcache

The installer will also create a configuration template inside /etc/php.d, you can customize it.

$ ls /etc/php.d/|grep opcache
10-opcache.ini
opcache-default.blacklist

Open the file for editing.

sudo vi /etc/php.d/10-opcache.ini

In our server, we are using the following settings to provide a goog performance:

; Enable Zend OPcache extension module
zend_extension=opcache

; Determines if Zend OPCache is enabled
opcache.enable=1

; Determines if Zend OPCache is enabled for the CLI version of PHP
opcache.enable_cli=1

; The OPcache shared memory storage size (in MB)
opcache.memory_consumption=512

; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=128

; The maximum number of keys (scripts) in the OPcache hash table.
opcache.max_accelerated_files=10000

; The maximum percentage of "wasted" memory until a restart is scheduled.
opcache.max_wasted_percentage=5

; Enable timestamp validation, ensuring that cached files are up to date.
opcache.validate_timestamps=1

; Checks for script changes every 2 seconds.
opcache.revalidate_freq=2

; Path where OPcache can store file cache.
opcache.file_cache=/tmp

; Enables or disables opcode caching in shared memory.
opcache.file_cache_only=0

; Enables or disables checksum validation when script loaded from file cache.
opcache.file_cache_consistency_checks=1

; Other performance settings
opcache.fast_shutdown=1
opcache.enable_file_override=1
opcache.optimization_level=0x7FFFBFFF

Refer to the official OPcache documentation for a complete list of options that you can configure and customize.

Test OPcache is working

Restart your webserver and php-fpm services.

  • Nginx web server
sudo systemctl restart php-fpm nginx
  • Apache httpd web server
sudo systemctl restart php-fpm httpd

Listing of OPcache runtime configurations can be done by executing below commands:

php -i | grep opcache

Create a sample PHP script for testing if OPcache is working in our system.

  • Nginx web server
sudo vi /usr/share/nginx/html/opcache.php
  • Apache httpd web server
sudo vi /var/www/html/opcache.php

Add the contents into the file.

<?php
if (function_exists('opcache_get_status')) {
    $status = opcache_get_status();
    if ($status) {
        echo "OPcache is enabled on the server.";
    } else {
        echo "OPcache is disabled on the server..";
    }
} else {
    echo "OPcache is not installed.";
}
?>

Save and open the file from http://serverIP_or_fqdn/opcache.php

enable opcache website02

This confirms PHP OPcache is working fine in our system. We hope this article was helpful and wishing you faster website loading times!.

LEAVE A REPLY

Please enter your comment!
Please enter your name here