Opcache on WHM/Cpanel Server

How to install and run Opcache on WHM/Cpanel server

Caching is an important aspect of site speed. In the simplest terms caching is a system you use to speed up your wesbite. When you use caching, content from your webpage is saved to your server’s memory when loaded for the first time and for each subsequent visit it is loaded from the cache which speeds up the process. If the content is loaded from the server directly each time, it might take much more time to load a webpage.

That’s why you can enjoy multiple times faster loading times when you use caching. You have what your client is going to order in your store and therefore it spares you a visit to the warehouse each time. Caching works in a similar manner and by shortening the path, it shortens the time to delivering the content.
Caching is of several kinds like page caching, browser caching, object caching and OPcode caching. In this article, we are going to discuss Opcache (a type of opcode caching) and how to install it on WHM server.

What is OPcache?

OPcache is a type of OPcode caching. This type of caching converts the PHP code that is in human readable form to another form that the server understands or into Opcode. Machines do not read PHP code the way we humans do but need the code in a different format. It is why, the code needs to be converted to the appropriate form. The code then gets saved into the server’s memory when PHP file loads for the first time on your webpage. On each subsequent visit, the PHP file loads from the server memory leading to faster load times. OPcache or other bytecode caching engines like APC or Xcache do it when the page loads for the first time. After that, the server is able to load it from its memory when a client requests. However, OPcache is now considered better compared to the other two.
The Zend OPcache (Opcache module that we will install from EasyAPache4) has acquired very high popularity based on its performance. According to the Zend website, “The Zend OPcache improves PHP performance by storing pre-compiled script bytecode in the shared memory. This eliminates the stages of reading code from the disk and compiling it on future access. For further performance improvement, the stored bytecode is optimized for faster execution. This component works out-of-the-box and therefore does not require any configuration or changes to your code. The Zend OPcache speeds up PHP execution and increases server performance, resulting in better Web application performance.”

The Zend Opcache was owned by Zend which made it open source. It has been available to users since PHP5.5 as a PHP extension. Prior to that APC and Xcache were widely used.

How to install OPcache (The Zend Opcache)

Installing Opcache on a dedicated server with WHM is very easy.
You need to login to the WHM and then search for EasyApache 4.
Type Easy in the search box on the left and it will bring up EasyApache 4. Click on it to open the EasyApache 4 Interface.

Inside the EasyApache 4 interface, you will see the various profiles available on your system. The currently installed profile is listed on the top.

You will need to customize your existing profile to provision Zend Opcache. Click on the customize button and it will open the current profile for you. You can make desired changes to the package from the EasyApache 4 interface.

On the left, you have the option for adding or changing MPM, Apache modules, PHP versions, PHP extensions and more packages.
Right now, we are concerned with enabling Opcache on our server, so let’s get directly there. Click on PHP extensions.

The Zend Opcache is available as a PHP extension in the EasyApache4 Interface. You can select the PHP version for which you want to enable the extension. You can also enable it for multiple PHP versions if you have two or more PHP versions installed on your server.

Clicking on PHP extensions in the left column will open a small search box above the list of available extensions. Type Opcache in the searchbox and it will show you the options. For example, if you have PHP 8.1 and 8.2 installed on your server, you can enable Opcache for both. You just need to toggle the button next to each so it turns blue. The Opcache for PHP 8.2 is php82-php-opcache and that for PHP 8.1 is php81-php-opcache and so on.

SUGGESTED READING:
Install PHP8.2 on WHM/Cpanel Server

You will find a small line of explanation under each module or extension in the EasyApache 4 interface. Under the Opcache extension, you will see ‘The Zend Opcache’ written. Check the image below:

Once you have toggled the buttons on, you can go directly to review instead of clicking on next. Review the changes, and the features that will be affected by these changes at this stage. It is safe to click on provision since there are no risky changes involved.

When you click on provision, the process lasts for a few minutes following which you receive the message that the provision process is completed.
This will mean that the desired changes have taken effect and you are good to go. However, a small step still remains to be completed.
In the next step, you need to go to your root folder and open the php.ini file through cpanel and add the following lines there:

config:

zend_extension=opcache.so

opcache.enable=1

opcache.memory_consumption=128

opcache.interned_strings_buffer=8

opcache.max_accelerated_files=4000

opcache.revalidate_freq=60

opcache.fast_shutdown=1

opcache.enable_cli=1

To enable Opcache you need to set opcache.enable=1 and to disable it you must set its value to zero. You can change the rest values according to your need/preference.

OPcache Variables:

OPcache.max_accelerated_files:
This value implies the maximum number of keys (and therefore scripts) in the OPcache hash table. The actual value used will be the first number in the set of prime numbers { 223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987, 262237, 524521, 1048793 } that is greater than or equal to the configured value. The minimum value is 200 and the maximum value is 1000000. Values outside of this range are clamped to the permissible range.

OPcache.revalidate_freq:
This value implies how often to check script timestamps for updates. It is expressed in seconds. If this value is set to 0, it will result in OPcache checking for updates on every request.

Opcache.max_wasted_percentage:
The maximum percentage of wasted memory that is allowed before a restart is scheduled, if there is insufficient free memory. The maximum allowed value is “50”. If you apply a larger value like 60, the system will automatically apply the maximum allowed value of 50.

Opcache.memory_consumption:
The size of the shared memory storage used by OPcache. This value is expressed in megabytes. The minimum allowed value is “8”. If you set a value smaller than 8, the system will automatically apply the minimum permissible value.

OPcache.interned_strings_buffer:
The amount of memory used to store interned strings. This value is expressed in megabytes. In the example above, it is set at 8mb and you can set higher according to your need.

You can find more information about PHP OPcache configuration on the PHP website.

SUGGESTED READING:
Install Memcached on WHM/Cpanel Server