One of the most used programming languages â€‹â€‹today by millions of users is PHP, which is useful and practical for application development and can be found on all web hosting servers available today..
One of the advantages of using PHP is its great options of using various extensions in its default distribution; with which the uses of PHP for the development of the applications are extended in a staggered way. One of these extensions is OPcache, which helps improve the overall performance of PHP.
It is for this reason that today and through this study, TechnoWikis will explain how to install and configure OPcache in CentOS 7 to be even more productive with PHP..
What is OPcache?
OPcache has been developed seeking to improve PHP performance by storing the byte code of a precompiled script in the shared memory, so that the need for the PHP language itself to load and analyze the scripts in each request is eliminated, which translates Finally in better performance.
OPcache is included in PHP version 5.5.0 and later, and is available through PECL for PHP versions 5.2, 5.3 and 5.4.
No external library is required to compile this PHP extension so that it is available for all distributions where PHP runs without problem..
1. How to install OPcache on CentOS 7 Linux
First, we will install the EPEL repository and then the REMI repository using the following commands:
yum install epel-release yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
Enter the letter s to confirm the download and installation.
Subsequently we must accept the corresponding GPG keys:
The next step is to install yum-utils which is a collection of utilities to extend the default functions of yum, thanks to them, it will be possible to manage yum repositories, as well as packages without manual configuration and many more tasks, for its installation we execute what following:
yum install yum-utils
Subsequently we must confirm the download and installation of the packages:
Now, when yum-utils is installed, we must use yum-config-manager to enable the Remi repository as the default repository to install different versions and PHP modules, we execute the following based on the desired version:
For PHP 5.5
yum-config-manager --enable remi-php55
For PHP 5.6
yum-config-manager --enable remi-php56
For PHP 7.0
yum-config-manager --enable remi-php70
For PHP 7.1
yum-config-manager --enable remi-php71
For PHP 7.2
yum-config-manager --enable remi-php72
Finally, we proceed to install the OPcache extension and verify the PHP version in order to confirm that we have the OPcache extension installed, for this we use the following commands. We accept the download and installation of OPcache.
yum install php-opcache
We verify the PHP version and have OPcache. Note the bottom line with Zend OPcache v7.0.5, this indicates that Opcache is correctly installed in CentOS 7.
2 . How to configure Opcache extension in CentOS 7 Linux
The last step is to configure OPcache by editing the file /etc/php.d/10-opcache.ini or /etc/php.d/opcache.ini (as the case may be) with our desired editor:
There we can apply the following values â€‹â€‹to get better PHP performance:
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
We save the changes using the following keys:
+ O Ctrl + O
We leave the editor using the following keys:
+ X Ctrl + X
We proceed to restart the web server by executing any of the following lines:
systemctl restart nginx OR systemctl restart httpd
In the opcache.ini file there are the following variables with their respective use:
This option enables the opcode cache. When disabled, the code is not optimized or cached
This option enables the opcode cache for the CLI version of PHP
Manage the size of the shared memory store used by OPcache, in megabytes
Its function is to manage the amount of memory used to store strings, in Megabytes.
Your task is to manage the maximum number of keys in the OPcache hash table, its minimum value is 200 and its maximum value is 100,000 in PHP <5.5.6, and 1000000 in later versions.
Refers to the maximum percentage of wasted memory that is allowed before a restart is programmed in the extension.
Its enablement allows OPcache to add the current working directory to the script key, and thus eliminate possible collisions between files with the same base name.
With its enable, OPcache will check for updated scripts every opcache.revalidate_freq seconds.
Measures the frequency of verification of Unix time stamps of scripts based on updates, in seconds opcache.revalidate_path boolean: when disabled, existing cached files that use the same include_path will be reused by OPcache.
When disabled, all documentation comments will be removed from the opcode cache in order to reduce the optimized code size.
With its enablement, a quick shutdown sequence is created which is used so that it does not release every allocated block, but depends on the memory manager of the Zend Engine to optimize PHP shutdown.
It is a bitmask that controls which optimization permissions are executed by OPcache
It is a hack that should be enabled to work only in order to avoid errors "Cannot redeclare class
Indicates the maximum file size that will be cached, in bytes
Refers to the amount of time to wait for the start of a scheduled restart if the cache is not active, in seconds
OPcache error logs are hosted there
It is the main memory model that OPcache will use
Your task is to protect shared memory from unexpected writes while running scripts
Its function is to call OPcache API functions only from PHP scripts whose path begins with the specific string
The general functions of OPcache are
It is responsible for compiling and caching a PHP script without executing it
Get configuration information about the cache
Allows access to status information about the cache
Indicates if a script is cached in OPcache
Reset the contents of the opcode cache
As we can see, this PHP extension will be useful to significantly improve the performance of this language and thus work in a much more comprehensive way.