Last updated on January 2, 2024
The performance of your PHP applications can be significantly enhanced by using OpCache, a PHP opcode cache. OpCache reduces the need to recompile your PHP scripts on each request by storing the generated bytecode in memory, resulting in quicker execution rates and lessened server load.
Image retrieved from https://www.pinterest.ph/pin/flash-running-poster-custom-posters-design-your-own-wall-art-create-personalized-prints–792281759458556733/
This article will walk you through installing and configuring PHP OpCache with Web Viewer on Ubuntu 20.04.
Install OpCache
Install the Nginx, PHP, and additional PHP extensions with the following command:
sudo -i apt-get install nginx php php-fpm php-cli php-OpCache php-mysql php-zip php-gd php-mbstring php-curl php-xml -yAfter installing, use the following command to check if the OpCache was successfully installed.
php -versionConfigure OpCache
Once OpCache is installed, you can configure it by editing the php.ini file.
cd /etc/php/7.4/fpm sudo vi php.iniHere’s an example configuration file:
opcache.enable=1 opcache.memory_consumption=128 opcache.max_accelerated_files=10000 opcache.revalidate_freq=60Let’s go through each configuration one by one:
-
opcache.enable: Opcache may be enabled or disabled using this directive. Set it to 1 to turn on OpCache.
-
opcache.memory_consumption: This directive specifies how much memory opcache may utilize to cache bytecode. The default amount is 64MB. However, if you have adequate RAM, you can raise it.
-
opcache.max_accelerated_files: This directive specifies how many PHP scripts opcache may cache. The default amount is 2000. However, if you have a huge number of PHP scripts, you may raise it.
-
opcache.revalidate_freq: This directive specifies how frequently opcache checks to see whether a script has been changed. The default number is 2 seconds, but you may raise it if you don’t need to monitor for changes on a regular basis.
Enable Web Viewer
The visual viewer provides information on the performance of your OpCache. To activate the Web Viewer, copy the code below.
Replace the “your_domain” with the correct directory name.
cd /var/www/your_domain/ sudo wget https://raw.githubusercontent.com/rlerdorf/opcache-status/master/opcache.phpConclusion
That’s all! It is good to enable OpCache as it will speed up your PHP website. You can verify the installation by typing your_domain.com/opcache.php on the web browser.
After OpCache is enabled, you may check its status on the opcache status page to see how it is doing. The opcache status page gives details on how much memory is being utilized by the opcache, including how much memory is free, wasted, and how many hits and misses there were.
The definitions of each metric are as follows:
-
Used: The amount of memory that opcache is presently using.
-
Free: The amount of memory that can be used by opcache.
-
Wasted: Memory that has been allotted to scripts that are no longer needed.
-
Hits: The quantity of times an opcache cached script was served.
-
Misses: The quantity of times a script needed to be recompiled because the opcache cache didn’t include it.
A high hit rate and a low miss rate indicate that opcache is being used appropriately. If you notice a large number of hits and a low number of misses, this indicates that opcache is effectively caching your PHP scripts and that your website is benefiting from the enhanced performance. Furthermore, if the quantity of memory wasted is minimal, this indicates that opcache is managing memory efficiently and not allocating memory to scripts that are no longer in use.