Supercharge Your WordPress with Redis: A Complete Setup Guide

Bedrock .env file with Redis settings


Are you looking to boost your WordPress site’s performance? Redis, an open-source caching system, can significantly improve your website’s speed. In this comprehensive guide, we’ll walk you through setting up Redis for both standard WordPress installations and Bedrock by Roots. We’ll also tackle the pesky “Filesystem not writeable” error in Redis Object Cache.

Table of Contents

Setting Up Redis for Standard WordPress Installations

Step 1: Obtain Redis Access Details

  1. Log into your hosting control panel.
  2. Navigate to the Redis management section.
  3. Generate a new Redis instance.
  4. Note down these crucial details:
  • Host (e.g., 127.0.0.1)
  • Port (typically 6379)
  • Password (a strong, unique string)

Step 2: Install the Redis Object Cache Plugin

  1. Access your WordPress admin panel.
  2. Go to “Plugins” > “Add New”.
  3. Search for “Redis Object Cache”.
  4. Click “Install” and then “Activate”.

Step 3: Configure wp-config.php

Open your wp-config.php file (usually in the root directory) and add these lines before the “That’s all, stop editing!” comment:

define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PORT', 6379);
define('WP_REDIS_PASSWORD', 'your_strong_password_here');

Step 4: Activate the Object Cache

  1. Go to the Redis Object Cache settings in your WordPress admin.
  2. Click “Enable Object Cache”.
  3. Verify that the connection status shows “Connected”.

Implementing Redis in Bedrock by Roots

Step 1: Get Redis Credentials

Follow the same process as in the standard WordPress setup to obtain your Redis details.

Step 2: Install Redis Object Cache

In your Bedrock directory, run:

composer require wpackagist-plugin/redis-cache

Activate the plugin in your WordPress admin panel afterward.

Step 3: Configure .env

Add these lines to your .env file in the Bedrock root directory:

WP_REDIS_HOST=127.0.0.1
WP_REDIS_PORT=6379
WP_REDIS_PASSWORD=your_strong_password_here

Step 4: Update config/application.php

Add the following to your config/application.php:

/**
 * Redis configuration
 */
Config::define('WP_CACHE', true);
Config::define('WP_REDIS_HOST', env('WP_REDIS_HOST'));
Config::define('WP_REDIS_PORT', env('WP_REDIS_PORT'));
Config::define('WP_REDIS_PASSWORD', env('WP_REDIS_PASSWORD'));

// Additional Redis settings
Config::define('WP_REDIS_DATABASE', env('WP_REDIS_DATABASE') ?: 0);
Config::define('WP_REDIS_PREFIX', env('WP_REDIS_PREFIX') ?: 'your_prefix_');
Config::define('WP_REDIS_SCHEME', env('WP_REDIS_SCHEME') ?: 'tcp');
Config::define('WP_REDIS_CLIENT', env('WP_REDIS_CLIENT') ?: 'phpredis');
Config::define('WP_REDIS_TIMEOUT', env('WP_REDIS_TIMEOUT') ?: 1);
Config::define('WP_REDIS_READ_TIMEOUT', env('WP_REDIS_READ_TIMEOUT') ?: 1);
Config::define('WP_REDIS_MAXTTL', env('WP_REDIS_MAXTTL') ?: 0);
Config::define('WP_REDIS_IGNORED_GROUPS', env('WP_REDIS_IGNORED_GROUPS') ?: []);

Step 5: Enable Object Cache

Activate the object cache in your WordPress admin panel, as described in the standard WordPress setup.

For more advanced configurations, check out the Redis Cache WordPress GitHub repository.

Resolving the “Filesystem not writeable” Error in Redis Object Cache

The Redis Object Cache plugin by Till Krüss is a popular tool for WordPress performance enhancement. However, some users may encounter a “Filesystem: Not Writeable” error. This is related to the DISALLOW_FILE_MODS constant in the WordPress configuration file.

DISALLOW_FILE_MODS – Better Left Enabled

Many WordPress developers use version control (e.g., Git) and want to prevent file modifications on the live site for security and data integrity. That’s why they set DISALLOW_FILE_MODS to true in the WordPress configuration file.

Changing DISALLOW_FILE_MODS to false isn’t the right solution. This constant should remain enabled if you’re using it for your project, as there’s an alternative solution specifically for Redis.

Why Doesn’t Redis Object Cache Work with DISALLOW_FILE_MODS Enabled?

Since version 2.5.0, the Redis Object Cache plugin “respects” the use of DISALLOW_FILE_MODS and informs the WordPress administrator that the filesystem isn’t writable. Although the plugin still works, it won’t allow re-enabling after deactivation until the issue is resolved.

Solving the DISALLOW_FILE_MODS Problem

To resolve this issue, add the following code to your theme’s functions.php file or as a must-use plugin:

add_filter( 'file_mod_allowed', 'allow_file_mod_for_object_cache', 10, 2 );
function allow_file_mod_for_object_cache( $allow_file_mod, $context ) {
    if ( in_array( $context, array( 'object_cache_dropin' ) ) ) {
        return true;
    }
    return $allow_file_mod;
}

This code allows file modifications only for specific operations related to Redis Object Cache, maintaining overall site security and data integrity.

Frequently Asked Questions

Is Redis compatible with all hosting providers?

Not all hosts support Redis. Verify Redis support with your hosting provider before installation.

Does Redis improve performance for small sites?

Redis is particularly beneficial for high-traffic sites with numerous database queries. Small sites may not see significant performance gains.

Can I use Redis with other caching plugins?

Yes, Redis can be used alongside other caching plugins like LiteSpeed Cache or W3 Total Cache that support Redis.

How can I check if Redis is working correctly?

Check the connection status in the Redis Object Cache plugin settings in WordPress. It should display “Connected”.

Is Redis secure?

Redis is secure when properly configured. Use strong passwords and limit Redis access to trusted IP addresses.

Should I regularly flush the Redis cache?

Flushing the Redis cache is recommended after major site updates or if you encounter content display issues.

Conclusion

Configuring Redis for WordPress and Bedrock can significantly enhance your site’s performance, especially for high-traffic websites with numerous database queries. By following the steps outlined above, you can easily set up and activate Redis on your server, as well as troubleshoot common file access issues. Remember to always prioritize security and regularly monitor your site’s performance for optimal results.

Leave a Reply

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