Step 5: Configure Wordpress to use Redis

We know that our Redis Docker container is working. Now, let’s configure Wordpress to use our new service.

  1. Log into Wordpress at http://blog.example.com/wp-admin

  2. Add the Redis Object Cache plugin

    1. Plugins -> Add New -> Search for Redis Object Cache

    2. Install and then activate it

  3. Go to the plugin configuration panel

    1. Settings -> Redis

  4. Click Enable Object Cache

  5. Click Show Diagnostics

You will notice that the status reads Not Connected. Clicking Show Diagnostics will show additional details. You will also notice that the host is 127.0.0.1. However, our Redis server is running on a different host, not on the localhost (127.0.0.1). From the perspective of Wordpress, localhost is container wordpressdocker_wordpress_1. Redis is running on container wordpressdocker_redis_1.

Docker knows about this container because we included it in the depends_on property. So, we only need to set the server address to redis. Docker will take care of the rest of the work.

Connect WP to Redis

The Redis plugin help document page provides the information on variables that we can use to configure Redis to work without Docker container.

  • WP_REDIS_HOST: Defines a host other than 127.0.0.1

  • WP_CACHE_KEY_SALT: Separates the cache keys of Wordpress from other services that might use the same Redis instance. It guarantees uniqueness.

  1. Add these two variables to your wp-config.php file. You can change the WP_CACHE_KEY_SALT to something unique.

    define('WP_REDIS_HOST', 'redis');
    define('WP_CACHE_KEY_SALT', 'wp-docker-5DknvYepdjyJMo8gDqrLhrpAJUQ');
    

    We can modify wp-config.php easily because it is part of the Wordpress volume data. Nifty!

    nano wordpress/wp-config.php
    
    File contents of wp-config.php
    define('WP_REDIS_HOST', 'redis');
    define('WP_CACHE_KEY_SALT', 'wp-docker-5DknvYepdjyJMo8gDqrLhrpAJUQ');
    
    // ** MySQL settings - You can get this info from your web host ** //
    /** The name of the database for WordPress */
    define('DB_NAME', 'wordpress');
    .
    .
    .
    
  2. Refresh the webpage. The status should have updated.

    Overview Output
    Status:    Connected
    Client:    Predis (v1.1.1)
    Key Prefix:    wp-docker-5DknvYepdjyJMo8gDqrLhrpAJUQ
    
  3. You can see the cached information in the Diagnostics frame.

    Diagnostics Output
    Status: Connected
    Client: Predis (v1.1.1)
    Drop-in: Valid
    Ping: PONG
    Redis Extension: Not Found
    Predis Client: 1.1.1
    PHP Version: 7.2.16
    Multisite: No
    Global Prefix: "wp_"
    Blog Prefix: "wp_"
    WP_REDIS_HOST: "redis"
    WP_CACHE_KEY_SALT: "wp-docker-5DknvYepdjyJMo8gDqrLhrpAJUQ"
    Global Groups: {"0":"blog-details","1":"blog-id-cache","2":"blog-lookup","3":"global-posts","4":"networks","5":"rss","6":"sites","7":"site-details","8":"site-lookup","9":"site-options","10":"site-transient","11":"users","12":"useremail","13":"userlogins","14":"usermeta","15":"user_meta","16":"userslugs","33":"blog_meta"}
    Ignored Groups: ["counts","plugins","themes"]
    Dropins:
     - Redis Object Cache Drop-In v1.4.1 by Till Krüss
    Plugins:
     - Akismet Anti-Spam v4.1.1 by Automattic (Inactive)
     - Hello Dolly v1.7.1 by Matt Mullenweg (Inactive)
     - Redis Object Cache v1.4.1 by Till Krüss (Active)
    

Disable the Redis Public Port

Currently, any service on your VPS can use the Redis server configured for Wordpress. This may or may not cause you a problem. You should disable it if you want to run another instance of Redis.

  1. To remove the public port on 6379, comment out the port information in docker-compose.yml.

    1. Edit file docker-compose.yml.

    2. Add # before the port information

    Code Snippet of docker-compose.yml
    redis:
      image: redis
      restart: always
      # ports:
      #   - "6379:6379"
    
  2. Restart the Wordpress stack

    docker-compose down && docker-compose up -d
    
  3. Verify that Wordpress can still connect to the Redis instance

Testing Redis without a Public Port

You can explore this more on your own, but you can test the Redis server running on container wordpressdocker_redis_1 without opening up specif ports. You only need to know the IP address of the specific container.

  1. Locate the IP address of the container using docker inspect: docker inspect wordpressdocker_redis_1

  2. Ping to the server using specifying the host using redis-cli: redis-cli -h 192.168.208.2 ping

  3. Run redis-cli commands: redis-cli -h 192.168.208.2 info

    IP Address of Container
    root@vps298933:~/wordpress-docker# docker inspect wordpressdocker_redis_1
    [
        {
            "Id": "913ff74dd94c5a00f9d54b7c2e7a00ad8700322ccc02c5b84339102d1915ba9b",
            "Created": "2019-03-30T07:12:32.603181563Z",
            "Path": "docker-entrypoint.sh",
            "Args": [
                "redis-server"
            ],
            "State": {
                "Status": "running",
                "Running": true,
    .
    .
    .
                        "NetworkID": "9ed37a99240fdadb14fbcce1be2ca1f469f286daa8fff87826c26932756493b8",
                        "EndpointID": "f80cc3e39ef2f73f6c5ecdfa4690d2b2ce3691586a2c0a3a2ee9cf9cddea3908",
                        "Gateway": "192.168.208.1",
                        "IPAddress": "192.168.208.2",
                        "IPPrefixLen": 20,
                        "IPv6Gateway": "",
                        "GlobalIPv6Address": "",
                        "GlobalIPv6PrefixLen": 0,
                        "MacAddress": "02:42:c0:a8:d0:02",
                        "DriverOpts": null
                    }
                }
            }
        }
    ]
    
    root@vps298933:~/wordpress-docker# ping 192.168.208.1
    PING 192.168.208.1 (192.168.208.1) 56(84) bytes of data.
    64 bytes from 192.168.208.1: icmp_seq=1 ttl=64 time=0.063 ms
    64 bytes from 192.168.208.1: icmp_seq=2 ttl=64 time=0.073 ms
    ^C
    --- 192.168.208.1 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1023ms
    rtt min/avg/max/mdev = 0.063/0.068/0.073/0.005 ms
    root@vps298933:~/wordpress-docker#
    root@vps298933:~/wordpress-docker# redis-cli -h 192.168.208.1 ping
    Could not connect to Redis at 192.168.208.1:6379: Connection refused
    root@vps298933:~/wordpress-docker# ping 192.168.208.2
    PING 192.168.208.2 (192.168.208.2) 56(84) bytes of data.
    64 bytes from 192.168.208.2: icmp_seq=1 ttl=64 time=0.181 ms
    64 bytes from 192.168.208.2: icmp_seq=2 ttl=64 time=0.091 ms
    ^C
    --- 192.168.208.2 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1030ms
    rtt min/avg/max/mdev = 0.091/0.136/0.181/0.045 ms
    root@vps298933:~/wordpress-docker# redis-cli -h 192.168.208.2 ping
    PONG
    root@vps298933:~/wordpress-docker# redis-cli -h 192.168.208.2 info clients
    # Clients
    connected_clients:1
    client_recent_max_input_buffer:32774
    client_recent_max_output_buffer:0
    blocked_clients:0
    root@vps298933:~/wordpress-docker#
    

Troubleshooting

  1. Do you need to troubleshoot this further? Open a shell connection to the Wordpress docker container.

    docker exec -it wordpressdocker_wordpress_1 /bin/sh
    
  2. Add some essential tools

    apt update && apt install -y redis-tools iputils-ping
    
    Output
    root@vps298933:~/wordpress-docker# docker exec -it wordpressdocker_wordpress_1 /bin/sh
    # apt update && apt install -y redis-tools iputils-ping
    Get:1 http://security-cdn.debian.org/debian-security stretch/updates InRelease [94.3 kB]
    Get:2 http://security-cdn.debian.org/debian-security buster/updates InRelease [38.3 kB]
    Get:4 http://security-cdn.debian.org/debian-security stretch/updates/main amd64 Packages [481 kB]
    Ign:3 http://cdn-fastly.deb.debian.org/debian stretch InRelease
    Get:5 http://cdn-fastly.deb.debian.org/debian stretch-updates InRelease [91.0 kB]
    Get:6 http://cdn-fastly.deb.debian.org/debian buster InRelease [158 kB]
    Get:7 http://cdn-fastly.deb.debian.org/debian buster-updates InRelease [46.8 kB]
    Get:8 http://cdn-fastly.deb.debian.org/debian stretch-updates/main amd64 Packages [11.1 kB]
    Get:9 http://cdn-fastly.deb.debian.org/debian stretch Release [118 kB]
    Get:10 http://cdn-fastly.deb.debian.org/debian buster/main amd64 Packages [7887 kB]
    Get:11 http://cdn-fastly.deb.debian.org/debian stretch Release.gpg [2434 B]
    Get:12 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 Packages [7084 kB]
    Fetched 16.0 MB in 5s (3165 kB/s)
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    All packages are up to date.
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following additional packages will be installed:
      libcap2 libcap2-bin libjemalloc1 libpam-cap
    Suggested packages:
      ruby-redis
    The following NEW packages will be installed:
      iputils-ping libcap2 libcap2-bin libjemalloc1 libpam-cap redis-tools
    0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
    Need to get 667 kB of archives.
    After this operation, 1863 kB of additional disk space will be used.
    .
    .
    .
    
  3. Test the connection to the Redis server.

    Note

    We have to specify the host using the -h switch because Redis is running on a different host called redis.

    1. Ping the redis server using ping to test the network connection.

    2. Ping redis using redis-cli to test the server functionality

    3. View server info using redis-cli

    Commands
    ping redis                       #Checks the network connection
    redis-cli -h redis ping          #Checks server response
    redis-cli -h redis info server   #View the server info
    
    Output
    # ping redis
    PING redis (192.168.192.2) 56(84) bytes of data.
    64 bytes from wordpressdocker_redis_1.wordpressdocker_default (192.168.192.2): icmp_seq=1 ttl=64 time=0.211 ms
    64 bytes from wordpressdocker_redis_1.wordpressdocker_default (192.168.192.2): icmp_seq=2 ttl=64 time=0.100 ms
    ^C
    --- redis ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1001ms
    rtt min/avg/max/mdev = 0.100/0.155/0.211/0.056 ms
    #
    # redis-cli -h redis ping
    PONG
    #
    # redis-cli -h redis info server
    # Server
    redis_version:5.0.4
    redis_git_sha1:00000000
    redis_git_dirty:0
    redis_build_id:c6d7e1572d62bb79
    redis_mode:standalone
    os:Linux 4.15.0-46-generic x86_64
    arch_bits:64
    multiplexing_api:epoll
    atomicvar_api:atomic-builtin
    gcc_version:6.3.0
    process_id:1
    run_id:21c1137b7c733b539977f3a7f6646a0b95a15654
    tcp_port:6379
    uptime_in_seconds:2510
    uptime_in_days:0
    hz:10
    configured_hz:10
    lru_clock:10425442
    executable:/data/redis-server
    config_file:
    #
    .
    .
    .
    
  4. If these commands are successful, then the problem is in Wordpress.

  5. Type exit to leave the SSH session.

    exit
    
  6. Check the wp-config.php file.