How to Enable and Configure PHP Opcache for WordPress to Reduce Server Response Time
Learning how to enable and configure PHP Opcache for WordPress to reduce server response time is one of the best performance improvements you can make to any WordPress site. PHP Opcache stores precompiled script bytecode in memory. This means PHP doesn’t recompile your scripts on every request. The result is faster execution, lower CPU usage, and noticeably quicker page loads. In this tutorial, you’ll learn exactly how to check if Opcache is installed, how to configure it correctly, and how to verify it’s working. These steps apply to Ubuntu and Debian-based servers running PHP with Apache or Nginx.
Prerequisites and Requirements for Configuring PHP Opcache
Before you start, make sure you have the following in place.
Required access and software:
- A Linux server running Ubuntu 20.04, 22.04, or Debian 11/12
- PHP 7.4, 8.0, 8.1, or 8.2 installed
- A working WordPress installation
- Root or sudo access to the server
- SSH access to your server
Assumed knowledge level:
You should be comfortable running commands in a Linux terminal. You don’t need to be a PHP expert. Basic familiarity with editing files using nano or vim is helpful.
Estimated time to complete:
This tutorial takes roughly 15 to 30 minutes from start to finish.
Opcache ships with PHP 5.5 and later. In most cases, it’s already installed but not enabled or properly configured. You’ll check that first before changing anything. A well-tuned Opcache setup can reduce server response time by 30% to 80% depending on your server load and WordPress configuration. That’s a significant gain for a few minutes of work.
How to Enable and Configure PHP Opcache for WordPress Step by Step
Related tutorial: How to Configure Ssh Key-based Authentication on Ubuntu Server
Follow these steps carefully. Each step builds on the previous one.
Step 1: Check your PHP version
First, confirm which PHP version your server is running.
php -v
You’ll see output like PHP 8.1.12. Note the version number. You’ll need it in the next steps.
Step 2: Check if Opcache is already installed
Run this command to see if the Opcache extension is present.
php -m | grep opcache
If you see Zend OPcache in the output, it’s installed. If you see nothing, you need to install it first.
Step 3: Install Opcache if it’s missing
Replace 8.1 with your actual PHP version.
sudo apt update
sudo apt install php8.1-opcache -y
After installation, confirm it’s present by running the check from Step 2 again.
Step 4: Find your PHP configuration file
You need to edit the correct php.ini file. The location depends on your PHP version and server type.
php --ini | grep "Loaded Configuration"
For Apache with PHP 8.1, the path is typically:
/etc/php/8.1/apache2/php.ini
For PHP-FPM with Nginx, it’s usually:
/etc/php/8.1/fpm/php.ini
Step 5: Configure Opcache settings
Open your php.ini file with nano. Use the correct path from Step 4.
sudo nano /etc/php/8.1/apache2/php.ini
Search for the [opcache] section by pressing CTRL+W and typing opcache. Add or update the following settings. These values work well for most WordPress sites.
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.save_comments=1
opcache.fast_shutdown=1
opcache.enable_cli=0
Here’s what the key settings do:
opcache.memory_consumption=256, Allocates 256MB of memory for cached bytecode. Increase this on busy sites.opcache.max_accelerated_files=10000, Sets the maximum number of PHP files Opcache can store. WordPress with plugins can have thousands of files.opcache.revalidate_freq=60, Checks for file changes every 60 seconds. Set to0in development environments.opcache.save_comments=1, Required for WordPress and many plugins that use PHP docblock annotations.
Save the file with CTRL+O, then press Enter. Exit with CTRL+X.
Step 6: Restart your web server
For Apache, run:
sudo systemctl restart apache2
For Nginx with PHP-FPM, run:
sudo systemctl restart php8.1-fpm
sudo systemctl restart nginx
Step 7: Verify Opcache is active
Create a quick PHP info file to confirm Opcache is running. Do this inside your WordPress web root.
sudo nano /var/www/html/opcache-check.php
Add this single line:
Save and close the file. Open your browser and visit http://yourdomain.com/opcache-check.php. Search the page for “opcache”. You should see a green “enabled” status under the Zend Engine section.
Delete this file immediately after checking. Never leave phpinfo() files on a live server.
sudo rm /var/www/html/opcache-check.php
You can also check Opcache status from the command line:
php -r "var_dump(opcache_get_status());"
This outputs detailed stats including hit rate, memory usage, and cached file count. A hit rate above 90% means Opcache is working well. For more details on Opcache configuration options, refer to the official PHP Opcache documentation.
Step 8: Monitor performance with a WordPress plugin
Install the Query Monitor plugin to track PHP execution time before and after enabling Opcache. This gives you real data on how much faster your WordPress site responds.
Troubleshooting Common PHP Opcache Problems
Even a straightforward setup can hit snags. Here are the most common issues and how to fix them.
Problem: Opcache shows as enabled but WordPress still feels slow
Check your opcache.memory_consumption value. If it’s too low, the cache fills up and files get evicted constantly. Run php -r "var_dump(opcache_get_status());" and look at memory_usage. If used_memory is close to free_memory, increase the allocation to 256MB or higher.
Problem: WordPress shows stale content after updating plugins or themes
Lower the opcache.revalidate_freq value or set it to 0 temporarily. After deploying updates, you can also reset the cache manually.
php -r "opcache_reset();"
Problem: White screen of death after enabling Opcache
Some older plugins don’t handle Opcache well. Disable Opcache temporarily and check your PHP error log.
sudo tail -f /var/log/apache2/error.log
Set opcache.save_comments=1 in your config. This resolves most plugin compatibility issues.
Problem: Changes to PHP files aren’t reflected on the site
This happens when opcache.revalidate_freq is set too high. Lower it to 2 during development. Set it back to 60 in production.
Warning: Always back up your php.ini file before making changes.
sudo cp /etc/php/8.1/apache2/php.ini /etc/php/8.1/apache2/php.ini.bak
Conclusion: Faster WordPress with PHP Opcache
You now know how to enable and configure PHP Opcache for WordPress to reduce server response time on a Linux server. You checked for existing Opcache support, installed it if needed, applied optimized settings, and verified everything was working. These changes alone can cut your PHP processing time dramatically. The next logical step is combining Opcache with a full-page caching plugin like WP Rocket or W3 Total Cache. You might also look at tuning your MySQL configuration or enabling a Redis object cache. Each layer you add builds on the last. Understanding how to enable and configure PHP Opc
