Have you opened your website and thought, "Why is this so slow"? I have thought the same. After I migrated my
PHP sites to a VPS, some of my pages loaded as if the server was taking a nap. But when I discovered the
PHP Opcache, everything went so much quicker. Let me show you how to set it up.
Why Opcache Matters
If your
VPS is running
PHP, then
Opcache can be beneficial. It stores precompiled
PHP files in memory, so a
PHP file does not have to do the same work every time someone loads your site. It will load more quickly. You may have seen or asked yourself, "Why do
small VPS servers feel quick at all?"
Opcache is one reason below (along with caching).
Step 1: Check If Opcache Is installed
Before changing anything, check to see if
Opcache is already installed.
Create a small file called
phpinfo.php, and search for
"Zend OPcache."
In the case you do not have it installed, you can install it through this command:
“sudo apt install php-opcache”
Step 2: Set Basic Opcache Configurations
Now let’s add a few basic values for better performance. Open the following file:
“/etc/php/*/fpm/php.ini”
Then add or change these settings below:
- opcache.enable=1 — Enables Opcache
- opcache.memory_consumption=256 — Ensures enough memory is allocated for Opcache
- opcache.interned_strings_buffer=16 — Enables Opcache to store common strings
- opcache.max_accelerated_files=20000 — Would be decent for larger websites and prevents any errors
- opcache.validate_timestamps=1 — Determines if a file has changed
- opcache.revalidate_freq=2 — Would check every 2 seconds
When it comes to your
VPS server, these couple of settings would be suitable and would improve speed reasonably well for your website.
Step 3: Restart PHP-FPM
Once you are finished saving, restart PHP-FPM through:
“sudo systemctl restart php-fpm”
Now just refresh your website. You should notice it is running faster, almost like your
VPS is performing better.