How To Install PHP OPcache on DirectAdmin

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:

SettingValueDescription
opcache.enable1Enables OPcache.
opcache.enable_cli1Enables OPcache for the CLI, useful for CLI scripts.
opcache.memory_consumption512Allocates 512MB of RAM for the OPcache shared memory storage.
opcache.interned_strings_buffer64Allocates 64MB for interned strings in OPcache.
opcache.max_accelerated_files30000Allows up to 30,000 PHP files to be cached.
opcache.revalidate_freq2Checks for script changes every 2 seconds.
opcache.max_wasted_percentage5Performs a restart when the wasted memory exceeds 5%.
opcache.consistency_checks0Disables consistency checks; set to 0 for production environments to improve performance.
opcache.validate_timestamps1Enables timestamp validation, ensuring that cached files are up to date.
opcache.file_cache/tmpPath where OPcache can store file cache.
opcache.file_cache_only0Specifies whether to use file-based cache only. Set to 0 to use both memory and file cache.
opcache.file_cache_consistency_checks1Enables consistency checks for file-based cache.
opcache.fast_shutdown1Enables a faster shutdown sequence.
opcache.enable_file_override1Enables the override mechanism for precompiled scripts.
opcache.optimization_level0x7FFFBFFFEnables all optimization levels except for redundant code elimination.
opcache.log_verbosity_level1Sets the verbosity level for logging. 1 logs errors only.
opcache.error_log/var/log/php_opcache.logSpecifies the log file for OPcache errors.
OPcache settings: Refer to OPcache documentation for complete list.

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.

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

GlassFish is an open-source application server project started by Sun Microsystems for the Java EE platform, then sponsored by Oracle […]

This article will cover how to install Wine 8.x on Rocky Linux 8 / AlmaLinux 8. Wine is an application […]

Java is a high-level object-oriented programming language and computing platform intended to let application developers write once and run everywhere. […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.