PHP OPcache is one of the highest-impact, lowest-effort performance improvements you can make on a DirectAdmin server. By caching compiled PHP bytecode in shared memory, OPcache eliminates the need to parse and compile PHP scripts on every request — reducing CPU usage by 30–50% on busy servers and cutting page load times dramatically. This guide walks you through installing OPcache via DirectAdmin's CustomBuild, tuning it correctly for production workloads, and configuring it for servers running multiple PHP versions.
What PHP OPcache Does and Why It Matters
Every time a PHP script runs without OPcache, the PHP engine must:
- Read the source file from disk
- Tokenise and parse the PHP code
- Compile it to opcodes (bytecode)
- Execute those opcodes
With OPcache enabled, steps 1–3 happen only once per file (or until the file changes). The compiled opcodes are stored in shared memory and reused on subsequent requests. For a typical WordPress site with 400–800 PHP files loaded per request, this removes thousands of disk reads and compile operations per second.
The result: lower CPU, faster TTFB (Time to First Byte), and the ability to handle significantly more concurrent visitors on the same hardware.
Step 1: Check If OPcache Is Already Installed
Before installing, check whether OPcache is already present on your DirectAdmin server:
php -m | grep -i opcache
If the output shows Zend OPcache, OPcache is installed (though it may be disabled or misconfigured). Check its status:
php -i | grep -i opcache.enable
If nothing is returned or you see opcache.enable=0, proceed with installation.
💡 None of these worked? Skip the guesswork.
Get Expert Help →Step 2: Install OPcache via DirectAdmin CustomBuild
DirectAdmin uses CustomBuild 2.0 to manage PHP extensions including OPcache. All installation is done as root via SSH.
cd /usr/local/directadmin/custombuild
./build set opcache yes
./build opcache
CustomBuild compiles OPcache for each PHP version installed on the server. This may take 5–15 minutes depending on how many PHP versions are configured. You will see output for each version as it compiles.
php -m | grep -i opcache
You should now see Zend OPcache in the output.
Step 3: Configure OPcache for Production Performance
The default OPcache settings that ship with PHP are conservative and not tuned for a busy web server. You need to edit the php.ini file for each PHP version.
Find all OPcache-enabled php.ini files:
find /usr/local/php* -name php.ini 2>/dev/null
# Also check:
find /etc/php* -name php.ini 2>/dev/null
For each php.ini file, find the [opcache] section (or add it at the bottom) and apply these settings:
[opcache]
opcache.enable=1
opcache.enable_cli=0
opcache.memory_consumption=256
opcache.interned_strings_buffer=32
opcache.max_accelerated_files=20000
opcache.revalidate_freq=60
opcache.validate_timestamps=1
opcache.save_comments=1
opcache.fast_shutdown=1
opcache.enable_file_override=0
What Each Setting Means
opcache.enable=1— enables OPcache for web requests (always set this to 1)opcache.enable_cli=0— disables OPcache for CLI PHP (leave off unless you run long-running CLI scripts that benefit from caching)opcache.memory_consumption=256— RAM (in MB) allocated to the OPcache shared memory segment. See sizing guide belowopcache.interned_strings_buffer=32— separate RAM pool for interned strings (class/function names). 32 MB is sufficient for most WordPress/Laravel setupsopcache.max_accelerated_files=20000— maximum number of PHP files OPcache can store. Must be higher than the total number of PHP files on the serveropcache.revalidate_freq=60— how often (in seconds) OPcache checks if a file has changed on disk. Set to 0 only in developmentopcache.validate_timestamps=1— OPcache checks file modification time before using cached version. Keep at 1 unless you have a deployment process that flushes OPcache on deployopcache.save_comments=1— required by Doctrine, Laravel, and many frameworks that use PHP docblock annotations
Step 4: Size OPcache Memory Correctly for Your Server
The most common OPcache mistake is setting memory_consumption too low. When the cache fills up, OPcache starts evicting entries and performance degrades to near-uncached levels.
To size OPcache correctly, first count the PHP files on your server:
find /home -name "*.php" | wc -l
Then estimate the memory needed. As a rule of thumb, each PHP file consumes approximately 10–15 KB of OPcache memory after compilation. Multiply the file count by 15 KB to get a conservative estimate:
# Example: 8,000 PHP files × 15 KB = 120 MB → set memory_consumption=192
For shared hosting servers with many accounts, set memory_consumption to at least 256 MB. For large servers with 50+ WordPress installations, 512 MB is appropriate.
Check OPcache memory usage after 30 minutes of production traffic:
php -r "print_r(opcache_get_status());" | grep -E 'used_memory|free_memory|wasted'
If used_memory is within 10% of total allocated memory, increase memory_consumption. If wasted_percentage exceeds 5%, consider increasing max_wasted_percentage or restarting PHP-FPM more frequently to compact the cache.
Step 5: Configure OPcache for Multiple PHP Versions
DirectAdmin servers often run PHP 7.4, 8.1, 8.2, and 8.3 simultaneously for different user accounts. You must configure OPcache separately in each version's php.ini.
Find all PHP version ini files:
ls /usr/local/php*/lib/php.ini
Apply the same OPcache settings to each file. You can automate this with a loop:
for ini in /usr/local/php*/lib/php.ini; do
echo "Configuring OPcache in: $ini"
# Enable OPcache
sed -i 's/^;opcache.enable=.*/opcache.enable=1/' "$ini"
sed -i 's/^opcache.enable=.*/opcache.enable=1/' "$ini"
# Set memory
sed -i 's/^;opcache.memory_consumption=.*/opcache.memory_consumption=256/' "$ini"
sed -i 's/^opcache.memory_consumption=.*/opcache.memory_consumption=256/' "$ini"
# Set max files
sed -i 's/^;opcache.max_accelerated_files=.*/opcache.max_accelerated_files=20000/' "$ini"
sed -i 's/^opcache.max_accelerated_files=.*/opcache.max_accelerated_files=20000/' "$ini"
done
After editing any php.ini file, restart PHP-FPM for each version:
systemctl restart php-fpm74
systemctl restart php-fpm81
systemctl restart php-fpm82
systemctl restart php-fpm83
Or restart all at once:
systemctl restart 'php-fpm*'
Step 6: Verify OPcache Is Working
Create a phpinfo test file in a domain's web root:
echo " /home/USER/domains/DOMAIN/public_html/phpinfo.php
Visit https://yourdomain.com/phpinfo.php in a browser and search for "opcache". You should see a section headed Zend OPcache with Opcode Caching: Enabled.
Delete the phpinfo file immediately after testing:
rm /home/USER/domains/DOMAIN/public_html/phpinfo.php
For a more detailed status check, create a temporary monitoring script:
cat > /tmp/opcache_check.php << 'PHP'
A healthy OPcache shows a hit rate above 95% after the first few minutes of traffic.
Disabling OPcache for a Specific Domain (Per-Domain Override)
If a specific application breaks with OPcache enabled (rare, but can happen with dynamic class generation), you can disable OPcache for that domain without affecting others. Add this to the domain's .htaccess:
php_flag opcache.enable 0
Or if using PHP-FPM pools, add to the domain's PHP-FPM pool config:
php_admin_value[opcache.enable] = 0
For professional DirectAdmin performance tuning — including OPcache sizing, PHP-FPM pool configuration, and MySQL query optimisation — CloudHouse's server management team can audit your entire server stack and implement changes that immediately reduce load and improve page speeds.
