Are you looking for an easy way to install and use PHP OPcache in DirectAdmin? PHP OPcache is a module designed for caching PHP scripts and improve site performance. It stores precompiled scripts bytecode in shared memory, eliminating the loading of scripts from every new request. By using OPcache you can experience faster loading of your websites and the load on your servers. Furthermore, memory and CPU resource usage on the server will be efficient, which is a crucial requirement when hosting websites with high traffic.
In this short guide we provide you with the process of installing and using PHP OPcache on your DirectAdmin server.
1 – Login to DirectAdmin
Login to your DirectAdmin as a user with sudo privileges.
ssh user@serverip
Check if the module is already loaded in the environment.
php -m|grep -i opcache
If you don’t get any output, it means OPcache is not installed.
2 – Install PHP OPcache on DirectAdmin
In DirectAdmin, the OPcache is not installed from OS repositories, but compiled using CustomBuild – which is a tool used to manage DirectAdmin-related services.
Switch your current working directory to CustomBuild.
cd /usr/local/directadmin/custombuild
Perform an update operation on custombuild script to ensure you are using the latest version.
$ sudo ./build update
directadmin current v1.665 92be28b19a1bf46189bd72b648cc39ee0792ce51 is already latest
Enable opcache PHP extension in the php_extensions.conf
file:
sudo da build set_php opcache yes
Run the following commands to build and install OPcache.
sudo ./build opcache
You should get the following output:
PHP 8.1 extension 'opcache' installed successfully
Restarting php-fpm81
Verify OPcache installation on DirectAdmin:
$ php -m | grep -i opcache
Zend OPcache
Zend OPcache
3 – Configure OPcache on DirectAdmin
We have confirmed OPcache is loaded in our server and ready for use. What we need to do is add custom OPcache settings to optimize it.
Check the default version of PHP in your DirectAdmin server.
$ php -v
PHP 8.1.29 (cli) (built: Jul 2 2024 11:42:02) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.29, Copyright (c) Zend Technologies
with the ionCube PHP Loader v13.3.0, Copyright (c) 2002-2024, by ionCube Ltd.
with Zend OPcache v8.1.29, Copyright (c), by Zend Technologies
Open PHP .ini configuration file relevant to your PHP version, this is insider /usr/local/
directory. For our PHP version the file to edit will be:
sudo nano /usr/local/php81/lib/php.ini
Add your configurations inside [opcache]
block.
; Enable OPcache
opcache.enable=1
opcache.enable_cli=1
; Memory consumption settings
opcache.memory_consumption=512
opcache.interned_strings_buffer=64
opcache.max_accelerated_files=30000
; Optimization and cache settings
opcache.revalidate_freq=2
opcache.max_wasted_percentage=5
opcache.consistency_checks=0
opcache.validate_timestamps=1
; File cache settings
opcache.file_cache=/tmp
opcache.file_cache_only=0
opcache.file_cache_consistency_checks=1
; Other performance settings
opcache.fast_shutdown=1
opcache.enable_file_override=1
opcache.optimization_level=0x7FFFBFFF
; Logging settings
opcache.log_verbosity_level=1
opcache.error_log=/var/log/php_opcache.log
Explanation of the settings used:
Setting | Value | Description |
---|---|---|
opcache.enable | 1 | Enables OPcache. |
opcache.enable_cli | 1 | Enables OPcache for the CLI, useful for CLI scripts. |
opcache.memory_consumption | 512 | Allocates 512MB of RAM for the OPcache shared memory storage. |
opcache.interned_strings_buffer | 64 | Allocates 64MB for interned strings in OPcache. |
opcache.max_accelerated_files | 30000 | Allows up to 30,000 PHP files to be cached. |
opcache.revalidate_freq | 2 | Checks for script changes every 2 seconds. |
opcache.max_wasted_percentage | 5 | Performs a restart when the wasted memory exceeds 5%. |
opcache.consistency_checks | 0 | Disables consistency checks; set to 0 for production environments to improve performance. |
opcache.validate_timestamps | 1 | Enables timestamp validation, ensuring that cached files are up to date. |
opcache.file_cache | /tmp | Path where OPcache can store file cache. |
opcache.file_cache_only | 0 | Specifies whether to use file-based cache only. Set to 0 to use both memory and file cache. |
opcache.file_cache_consistency_checks | 1 | Enables consistency checks for file-based cache. |
opcache.fast_shutdown | 1 | Enables a faster shutdown sequence. |
opcache.enable_file_override | 1 | Enables the override mechanism for precompiled scripts. |
opcache.optimization_level | 0x7FFFBFFF | Enables all optimization levels except for redundant code elimination. |
opcache.log_verbosity_level | 1 | Sets the verbosity level for logging. 1 logs errors only. |
opcache.error_log | /var/log/php_opcache.log | Specifies the log file for OPcache errors. |
For DirectAdmin installation with multiple versions of PHP, you will need to replicate the settings to each php.ini
file.
4 – Check PHP OPcache
We can test if a website is using OPcache, use DirectAdmin File manager and create a new file called check_opcache.php
in your website web root. Add the following contents into the file.
<?php
if (function_exists('opcache_get_status')) {
$status = opcache_get_status();
if ($status) {
echo "OPcache is enabled.";
} else {
echo "OPcache is disabled.";
}
} else {
echo "OPcache is not installed.";
}
?>
Visit your website and load the page http://fqdn/check_opcache.php
If it’s loaded from the output you will see OPcache is enabled
. After you’ve confirmed delete the check_opcache.php
file.
Conclusion
We have covered in detail how you can install and configure PHP OPcache on DirectAdmin. As addition, we showcased how you can check if the website is using opcache.
At CloudSpinx, we offer best-in-class technical support – 24/7, 365 days a year. We have a team of dedicated engineers ready to assist with any IT related challenge at your request. Contact our team to discover how CloudSpinx can impact your business in positive way.