Installing OPCache to improve PHP performance on CentOS 7

OPCache is a PHP extension designed to improve PHP performance and improve overall application performance.

How does OPCache work?PHP opens the file with the code, compiles it, and executes it. If the files do not change, in order not to constantly perform these actions, opCache caches the result. This saves server resources.

Installing the OPCache extension

Install EPEL and REMI repositories

[[email protected]]# yum update
[[email protected]]# yum install epel-release
[[email protected]]# yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm  

Install the yum-utils application to select the PHP version later

[[email protected]]# yum install yum-utils

Select PHP version using yum-config-manager

[[email protected]]# yum-config-manager --enable remi-php55 # Для PHP 5.5
[[email protected]]# yum-config-manager --enable remi-php56 # Для PHP 5.6
[[email protected]]# yum-config-manager --enable remi-php70 # Для PHP 7.0
[[email protected]]# yum-config-manager --enable remi-php71 # Для PHP 7.1
[[email protected]]# yum-config-manager --enable remi-php72 # Для PHP 7.2
[[email protected]]# yum-config-manager --enable remi-php73 # Для PHP 7.3

Now install the Opcache extension and check the PHP version

[[email protected]]# yum install php-opcache		
[[email protected]]# php -v
PHP 7.1.31 (cli) (built: Jul 31 2019 09:59:01) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.31, Copyright (c) 1999-2018, by Zend Technologies

Configuring the OPCache extension

Let’s open the settings file

[[email protected]]# nano /etc/php.d/10-opcache.ini

Example settings:

opcache.enable_cli = 1
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 4000
opcache.revalidate_freq = 60
opcache.fast_shutdown = 1

To apply the settings, you need to restart the web server (depending on which one you have installed)

[[email protected]]# systemctl restart nginx
[[email protected]]# systemctl restart php-fpm
[[email protected]]# systemctl restart httpd
Sidebar