An AI's attempt to visualize object cache. Image by midjourney.com

How to Install Jailed Redis for WordPress

by

Redis is an object caching server, which means it will store database data in RAM so it can be quickly called upon. This can drastically improve the performance of dynamic websites like WordPress that store most information in a SQL database. Fortunately, several WordPress plugins allow you to leverage Redis, but it does have to be implemented on your server. This tutorial shows how to implement Jailed Redis for WordPress on FreeBSD, but it’s even easier if you want to install it without jailing it. Regardless of your system, you could probably use this tutorial to figure it out by just ignoring the jail parts and replacing pkg with your system’s package manager.

I like jailing Redis because 1) I like jailing as many things as possible and 2) I only give the jail access to a localhost address so it’s cut off from the internet.

This tutorial utilizes the following setup:

  • FreeBSD 13.2
  • iocage for jail management (already installed—can be installed with pkg install iocage)
  • WordPress in an iocage jail (already installed)
  • Redis in a separate jail (not set up yet)

Redis + WordPress

Time needed: 20 minutes

How to Install Redis in a Jail

  1. Create Redis jail

    # iocage create -b -r 13.2-RELEASE -n redis

  2. Configure networking for Redis jail

    example: # iocage set ip4_addr="em0|10.0.0.1/24"

  3. Add localhost config

    Edit iocage config file for jail to add a localhost IP by adding the following lines:
    "assign_localhost": 1,
    "localhost_ip": "127.0.0.4",
    note: if you put this at the bottom of the list, do not include a comma after the final item

  4. Enter jail

    # iocage console redis

  5. Install Redis

    # pkg install redis

  6. Configure Redis

    Edit /usr/local/etc/redis.conf
    There are two lines we need to change to look like the following:
    bind 127.0.0.4
    protected-mode no
    note: If your Redis server will have access to the internet, you’ll want to set a password. If you’re not using jails and you are running the Redis server on the same system as WordPress, then you will want your values to be 127.0.0.1 and protected-mode set to yes.

  7. Enable Redis in /etc/rc.conf

    # sysrc redis_enable=yes

  8. Start Redis

    # service redis start

  9. Check Redis status

    # service redis status
    The status should tell you that Redis is running.

  10. On host, remove public networking from jail

    Edit config.json to remove connection to public interface (leave localhost IP)

  11. Restart Redis Jail

    # iocage restart redis

  12. Enter jail for WordPress

    On the host system:
    # iocage console wordpress
    Change wordpress to whatever the name of your WordPress jail is.

  13. Install Redis PHP Module

    note: change “php82” to whatever version of php you have installed
    # pkg install php82-pecl-redis

  14. Install WordPress plugin for Redis

    You have many options here. There is a simple Redis plugin that only connects to Redis, but then you’ll want to pair this with a page caching plugin. The two big ones you’ll see are W3 Cache and Lightspeed. The former I found rather unwieldy while I haven’t used the latter, but as I understand it, it’s optimized for Lightspeed servers. My recommendation is Hummingbird. There is a pro version, but it only has a few features missing from the free one.

    Whichever plugin you use, configure Redis with the IP address 127.0.0.4 and no password (unless you set one). In Hummingbird, this is in Hummingbird –> Caching –> Integrations. If your website is packed to the gills with javascript, you may want to spring for the Pro version.

    If you use the dedicated Redis plugin, you will have to add some lines to your wp-config.php file:

    // adjust Redis host and port if necessary
    define( 'WP_REDIS_HOST', '127.0.0.1' );
    define( 'WP_REDIS_PORT', 6379 );
    // change the prefix and database for each site to avoid cache data collisions
    define( 'WP_REDIS_PREFIX', 'my-moms-site' );
    define( 'WP_REDIS_DATABASE', 0 ); // 0-15
    // reasonable connection and read+write timeouts
    define( 'WP_REDIS_TIMEOUT', 1 );
    define( 'WP_REDIS_READ_TIMEOUT', 1 );


    Make sure it’s above the comment that says /* That's all, stop editing! Happy publishing. */

    If you go this route, I recommend you also install WP Super Cache for the page caching and other optimizations.

Enjoy a Faster Website

Your website should now load faster with jailed Redis for WordPress. Hooray! In the future I might put the various plugin options to the test.

F

Back to Top