+4 votes
1.5k views
Install and configure OPcache for PHP performance in CentOS 7

in PHP by (551k points)
reopened | 1.5k views

1 Answer

+5 votes
Best answer

1. How to install OPcache on CentOS 7 Linux
2 . How to configure Opcache extension in CentOS 7 Linux

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

Step 1

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

Subsequently we must accept the corresponding GPG keys: image
Step 3

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 
image
Step 4

Subsequently we must confirm the download and installation of the packages: image
Step 5

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 
image
Step 6

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 
image
Step 7

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. image

2 . How to configure Opcache extension in CentOS 7 Linux


Step 1

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:
 nano /etc/php.d/opcache.ini 
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 
image

We save the changes using the following keys:

+ O Ctrl + O

We leave the editor using the following keys:

+ X Ctrl + X

Step 2

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
 opcache.enable boolean 
This option enables the opcode cache for the CLI version of PHP
 opcache.enable_cli boolean 
Manage the size of the shared memory store used by OPcache, in megabytes
 opcache.memory_consumption integer 
Its function is to manage the amount of memory used to store strings, in Megabytes.
 opcache.interned_strings_buffer integer 
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.
 opcache.max_accelerated_files integer 
Refers to the maximum percentage of wasted memory that is allowed before a restart is programmed in the extension.
 opcache.max_wasted_percentage integer 
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.
 opcache.use_cwd boolean 
With its enable, OPcache will check for updated scripts every opcache.revalidate_freq seconds.
 opcache.validate_timestamps boolean 
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.
 opcache.revalidate_freq integer 
When disabled, all documentation comments will be removed from the opcode cache in order to reduce the optimized code size.
 opcache.save_comments boolean 
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.
 opcache.fast_shutdown boolean 
It is a bitmask that controls which optimization permissions are executed by OPcache
 opcache.optimization_level integer 
It is a hack that should be enabled to work only in order to avoid errors "Cannot redeclare class
 opcache.dups_fix boolean 
Indicates the maximum file size that will be cached, in bytes
 opcache.max_file_size integer 
Refers to the amount of time to wait for the start of a scheduled restart if the cache is not active, in seconds
 opcache.force_restart_timeout integer 
OPcache error logs are hosted there
 opcache.error_log string 
It is the main memory model that OPcache will use
 opcache.preferred_memory_model string 
Your task is to protect shared memory from unexpected writes while running scripts
 opcache.protect_memory boolean 
Its function is to call OPcache API functions only from PHP scripts whose path begins with the specific string
 opcache.restrict_api string 
The general functions of OPcache are
It is responsible for compiling and caching a PHP script without executing it
 opcache_compile_file 
Get configuration information about the cache
 opcache_get_configuration 
Allows access to status information about the cache
 opcache_get_status 
Overrides a cache script
 opcache_invalidate 
Indicates if a script is cached in OPcache
 opcache_is_script_cached 
Reset the contents of the opcode cache
 opcache_reset 

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.


by (3.5m points)
edited

Related questions

+4 votes
1 answer
asked Apr 27, 2020 in PHP by backtothefuture (551k points) | 1.2k views
+5 votes
1 answer
asked Oct 6, 2019 in PHP by backtothefuture (551k points) | 1.6k views
+4 votes
1 answer
+4 votes
1 answer
asked Oct 12, 2019 in Linux / Unix by backtothefuture (551k points) | 294 views
+3 votes
1 answer
Sponsored articles cost $40 per post. You can contact us via Feedback
10,632 questions
10,764 answers
510 comments
3 users