On this page
- Which cache does what?
- How do you tune PHP OPcache?
- How do you install Redis and the PHP client?
- How do you connect WordPress to Redis?
- How do you set up the Nginx FastCGI page cache?
- How do you check that the page cache hits?
- How do you clear the cache after publishing?
- What should you watch after enabling caching?
- Questions
Which cache does what?
Each layer removes a different kind of work, so they stack rather than compete.
- OPcache keeps compiled PHP bytecode in memory. Without it, PHP parses every file on every request.
- Object cache (Redis) stores the results of database queries and option lookups between requests. It helps logged-in users, admins and shop carts most, since those pages cannot be fully cached.
- Page cache (Nginx FastCGI) stores the final HTML for anonymous visitors. A cached hit never reaches PHP or MariaDB.
Start from a working site built as in WordPress on Nginx, PHP-FPM and MariaDB. Measure first, so you can see the difference later:
curl -o /dev/null -s -w "%{time_starttransfer}\n" https://example.net/How do you tune PHP OPcache?
OPcache ships with PHP and is usually on; tune the memory and file count so a full WordPress install with plugins fits.
php -i | grep -E 'opcache.enable|opcache.memory'
# Ubuntu 24.04 path shown; Debian uses its own version number
sudo tee /etc/php/8.3/fpm/conf.d/99-opcache.ini > /dev/null <<'EOF'
opcache.enable=1
opcache.memory_consumption=192
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.validate_timestamps=1
opcache.revalidate_freq=60
EOF
sudo systemctl restart php8.3-fpmOn Rocky and Alma put the same lines in /etc/php.d/99-opcache.ini and restart php-fpm. With revalidate_freq=60, PHP checks for changed files at most once a minute, which keeps plugin updates working without manual restarts.
How do you install Redis and the PHP client?
Install Redis from the distribution and bind it to localhost only, which is the default.
# Ubuntu and Debian
sudo apt install -y redis-server php-redis
sudo systemctl enable --now redis-server
sudo systemctl restart php8.3-fpm
# Rocky and Alma
sudo dnf install -y redis
sudo systemctl enable --now redis
redis-cli ping
ss -tlnp | grep 6379redis-cli ping should answer PONG, and port 6379 should only appear on 127.0.0.1 and ::1. Set a memory cap and an eviction policy in /etc/redis/redis.conf (the same path on all four distributions) so the cache never pushes MariaDB out of RAM:
maxmemory 128mb
maxmemory-policy allkeys-lruRestart Redis after the change. The Redis guide covers persistence and passwords if you use it for more than caching.
How do you connect WordPress to Redis?
Install the Redis Object Cache plugin with WP-CLI, give the site a key prefix, and enable the drop-in.
cd /var/www/example.net
sudo -u www-data wp config set WP_REDIS_PREFIX 'examplenet:'
sudo -u www-data wp config set WP_CACHE_KEY_SALT 'examplenet:'
sudo -u www-data wp plugin install redis-cache --activate
sudo -u www-data wp redis enable
sudo -u www-data wp redis statusOn Rocky and Alma, where the PHP Redis extension is not in the base repositories, tell the plugin to use its bundled pure-PHP client by adding sudo -u nginx wp config set WP_REDIS_CLIENT predis before enabling. The status command should report Connected. Watch it working with redis-cli info stats | grep keyspace; hits should climb as you browse the admin.
How do you set up the Nginx FastCGI page cache?
Define a cache zone in the http context, then tell the PHP location to use it. Create /etc/nginx/conf.d/fastcgi-cache.conf:
fastcgi_cache_path /var/cache/nginx/wp levels=1:2 keys_zone=WPCACHE:64m
max_size=1g inactive=60m use_temp_path=off;
fastcgi_cache_key "$scheme$request_method$host$request_uri";Then add the bypass rules and cache directives to the site server block:
set $skip_cache 0;
if ($request_method = POST) { set $skip_cache 1; }
if ($query_string != "") { set $skip_cache 1; }
if ($request_uri ~* "/wp-admin/|/wp-json/|/xmlrpc.php|wp-.*.php|/cart|/checkout|/my-account|sitemap") { set $skip_cache 1; }
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in|woocommerce_items_in_cart") { set $skip_cache 1; }
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_cache WPCACHE;
fastcgi_cache_valid 200 301 60m;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
add_header X-Cache $upstream_cache_status;
}sudo mkdir -p /var/cache/nginx/wp
sudo nginx -t && sudo systemctl reload nginxHow do you check that the page cache hits?
Request the same page twice as an anonymous visitor and read the X-Cache header.
curl -sI https://example.net/ | grep -i x-cache
curl -sI https://example.net/ | grep -i x-cache
curl -o /dev/null -s -w "%{time_starttransfer}\n" https://example.net/The first call shows MISS, the second HIT, and the response time should drop sharply compared with your earlier baseline. Logged-in requests and the admin should show BYPASS. If every request reports BYPASS, a plugin is likely setting a cookie or a query string on every page.
How do you clear the cache after publishing?
A new post should not wait an hour to appear. You have two options: clear the cache directory by hand or from a hook, or install a purge plugin that removes changed pages automatically when content is saved.
# clear the whole page cache
sudo find /var/cache/nginx/wp -type f -delete
# clear the object cache
sudo -u www-data wp redis flush --path=/var/www/example.netFor a busy site, a short cache time such as 10 minutes plus manual flushing after bulk edits is a simple and predictable compromise. Keep the cache folder on the local disk; the NVMe lines make cache reads especially quick.
What should you watch after enabling caching?
Memory is the main constraint on small plans. Check it with free -h after a day of traffic, and make sure Redis, OPcache and MariaDB together leave room for PHP-FPM workers. If the server swaps, lower Redis maxmemory or move to a larger plan as in moving to a larger plan. If pages look stale or a visitor sees someone else's cart, your bypass rules are missing a cookie; add it and flush the cache. For shops, read the WooCommerce guide for the extra pages that must never be cached.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about WordPress caching with Redis
Do I still need a caching plugin with Nginx FastCGI cache?
Not for page caching, since Nginx already does it. A small purge plugin is still handy so new posts appear immediately.
Will Redis help if every page is already page-cached?
It helps the requests that bypass the page cache: the admin, logged-in members, search and shop carts. For a mostly anonymous blog the gain is smaller.
How much RAM does Redis need for WordPress?
Most single sites stay well under 100 MB of keys. A 128 MB cap with allkeys-lru eviction is a safe starting point on a 2 GB plan.
Can caching break my site?
It can show stale or private content if bypass rules miss a cookie or URL. Test logged-in pages, carts and forms after enabling it, and use the X-Cache header to see what happens.
Should I expose Redis to other servers?
No. Keep it on localhost. If another server needs it, use a private tunnel such as WireGuard and set a password with requirepass.
More in Websites, CMS and shops
- WordPress on Nginx, PHP-FPM, MariaDB
- Fix common WordPress errors
- Back up WordPress (3-2-1 rule)
- WooCommerce shop setup
- Ghost publishing platform
- Moodle learning platform
- phpBB forum
- Flarum forum
- Discourse community forum
- MediaWiki on Nginx and MariaDB
- Magento Open Source with OpenSearch
- PrestaShop store on Nginx
- OpenCart shop on Nginx
- Matomo web analytics
- Static site with Nginx and rsync
- Joomla on Nginx and PHP-FPM
- Drupal with Composer and Drush
- Vanilla Forums open source