Article directory
Speed is life; if a website is even a fraction of a second behind, users will turn around and leave.
That's why HestiaCP Optimizing OPcache is crucial to the success or failure of any website that relies on PHP.
What is OPcache?
OPcache is a built-in bytecode caching engine in PHP.
Its function is simple: cache the compiled PHP script in memory, and directly call the cache when accessing it again, instead of recompiling.
This improves website response speed, reduces CPU load, and instantly alleviates server pressure.
According to the PHP official documentation:
"OPcache provides significant performance improvements by reducing execution time by avoiding the duplication of script compilation."
In short: OPcache is a PHP accelerator.
Why optimize OPcache in HestiaCP?

HestiaCP is a lightweight control panel that many people use to manage VPS or dedicated servers.
OPcache is enabled by default, but the parameters are often conservative.
Without adjustments, the performance improvement will be limited.
for example:
On a server with 4 cores and 8GB of memory, the default OPcache memory is only 64MB, and the number of cached files is only 4000.
This is for a medium-sized Wordpress For a website, it's simply not enough.
The result is that the cache is constantly being cleared, causing performance to fluctuate repeatedly.
Check the current OPcache status
Run the following command:
php -i | grep opcache.enable
You will see results similar to:
- opcache.enable => On: This means that OPcache is enabled in PHP-FPM.
- opcache.enable_cli => Off: Not enabled in command-line mode.
- opcache.enable_file_override => Off: File override functionality is not enabled.
This means that although OPcache is already working, there is still room for optimization.
Edit PHP configuration files
In HestiaCP, the PHP configuration file path is usually:
/etc/php/8.x/fpm/php.ini
To enable for CLI mode:
/etc/php/8.x/cli/php.ini
Locate the [opcache] section and add or modify the following parameters:
[opcache] ; 启用 OPcache opcache.enable=1 opcache.enable_cli=1 ; 内存与缓存设置 opcache.memory_consumption=256 opcache.interned_strings_buffer=16 opcache.max_accelerated_files=10000 ; 文件更新检测 opcache.validate_timestamps=0 opcache.revalidate_freq=60 ; 其他优化参数 opcache.save_comments=1 opcache.fast_shutdown=1 opcache.enable_file_override=1
Parameter details and optimization logic
opcache.memory_consumption=256
Allocating 256MB of memory to OPcache is suitable for medium to large websites.
For small websites, you can set it to 128MB.opcache.interned_strings_buffer=16
Provides a 16MB string cache to reduce the usage of duplicate strings.opcache.max_accelerated_files=10000
The maximum number of cached files is recommended to be at least 10000 in a WordPress + plugin environment.opcache.revalidate_freq=60
Files are checked for updates every 60 seconds to reduce frequent I/O.opcache.enable_cli=1
Enabling CLI caching is suitable for running command-line tools such as Artisan and WP-CLI.opcache.enable_file_override=1
Allow OPcache to take over the file system cache, further reducing disk access.- opcache.validate_timestamps=0
- Disable real-time detectionReduce file system I/O and improve performance.
However, this means that you must manually clear the cache (restart the PHP service) after modifying PHP files.
After modifying the configuration, you must restart the PHP service for the changes to take effect.
sudo systemctl restart php<版本>-fpmThe combination of these parameters allows the website to remain stable even under high concurrency.
Restart the PHP-FPM service
After making the changes, remember to restart the service:
sudo systemctl restart php8.x-fpm
Then run it again:
php -i | grep opcache
The parameters have been confirmed to be updated.
Best practices in production environments
In production environments, OPcache optimization involves more than just parameter tuning; it also includes the following strategies:
1. Clear cache during deployment
After each code update, the OPcache must be cleared; otherwise, an older version may be loaded.
You can add the following to the CI/CD script:
<?php opcache_reset();
Alternatively, execute it within the deployment process:
systemctl reload php8.x-fpm
2. Avoid frequent cleaning
Too frequent cache clearing can lead to performance degradation.
It is recommended to clean up only when the code is updated, rather than cleaning up at regular intervals.
3. Monitor OPcache usage.
You can check the cache hit rate in real time by using `php -i | grep opcache` or by installing the Opcache Control Panel plugin.
A hit rate of over 95% indicates a reasonable configuration.
According to Zend's official performance tests, enabling OPcache can improve PHP script execution speed by up to 3 times and reduce CPU usage by 50%.
This isn't some kind of mystical theory; it's solid data.
Conclusion: My Viewpoints and Key Quotes
In my opinion, OPcache is like a "turbocharger" for a website. Without it, PHP is like a car without a turbocharger, running slowly and wasting fuel.
Optimizing OPcache is not just a technical detail, but also a strategic choice for website operation.
Because speed equals conversion rate, and performance equals competitiveness.
Key quote: The future of a website lies not in how fancy its code is, but in how fast it can respond so quickly that users don't even have time to blink.
Therefore, stop letting servers waste resources in inefficient loops.
Optimize your HestiaCP OPcache now and make your website fly.
Do you want me to write a best-in-class OPcache configuration template for your production environment, which you can then directly copy and use in php.ini?
Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ The article "How to Optimize OPcache in HestiaCP? Opcode caching and compiling PHP code to make your website fly" shared here may be helpful to you.
Welcome to share the link of this article:https://www.chenweiliang.com/cwl-34197.html
