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.
Let me illustrate this with an analogy. Assume you’re a chef in a restaurant kitchen, and you’re creating a meal that involves a lot of chopping and combining components. Every time a new client buys this meal, you must start from scratch and spend a significant amount of time cutting and combining all of the ingredients anew. This might be time-consuming and slow down the overall cooking process. But, if you had a sous chef who had already chopped and mixed all of the ingredients for this recipe, you could use their pre-prepared mixture instead of beginning from scratch each time. This will save you time and make the cooking process more efficient.
Hence, just as a sous chef saves time in the kitchen by pre-preparing ingredients, OpCache saves time in PHP applications by pre-compiling code into bytecode and keeping it in memory.
This article will also 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 -y
After installing, use the following command to check if the OpCache was successfully installed.
php -version
Configure OpCache
Once OpCache is installed, you can configure it by editing the php.ini file.
cd /etc/php/7.4/fpm
sudo vi php.ini
Here’s an example configuration file:
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
Let’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.
Change the “your_domain” with the correct directory name.
cd /var/www/your_domain/
sudo wget https://raw.githubusercontent.com/rlerdorf/OpCache-status/master/OpCache.php
Conclusion
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.