Digital strategist, front end + email developer, WordPress enthusiast, IT tinkerer, and not-so-horrible writer

How to Create Redis Databases and Use a Different Password For Each

In January I moved between infrastructure companies and, for the first time in ten years, decided to run something other than CentOS for the operating system. As I installed the stack and configured things I discovered the DirectAdmin Redis management plugin I like is only compatible with Red Hat Enterprise Linux (RHEL) or CentOS. Crap.

So, like anyone these days, I asked ChatGPT how to spin up Redis databases from the command line and give each one a unique port number and password. It took some back and forth to nail down the details and that's why I made this post: to document something I know I'll need again and maybe, just maybe, help someone else in the future. 😅

To start, please connect to your linux server via SSH then install Redis.

When that's complete let's make sure it's running: please copy and paste redis-server -v into your command line terminal and hit enter.

Redis should answer with Redis server v=7.0.8 and a few additional details.

Sweet, it's installed.

Now let's add a database: copy and paste redis-server --port 10001 --daemonize yes and change the port number to whatever you'd like then hit enter. The idea is to add a database for each site and increment the port by one from the starting number. And if you're curious the –daemonize yes switch asks Redis to keep the database running in the background.

Now to connect to the database we just created: please copy and paste redis-cli -p 10001 into your terminal hit enter. This will open up the Redis command line interface and allow us to issue commands to the database on port 10001.

Next, copy and paste CONFIG SET requirepass YourVerySecretPasswordSSSSHHH into your terminal, but change YourVerySecretPasswordSSSSHHH to whatever you would like the password to be.

With that done we can type exit and hit enter to leave redis-cli and return to the normal command line.

If you'd like to create additional databases just walk back through the steps but increment the port number by one and give it a unique password.

! Update: I goofed and forgot to include instructions on how to make Redis reload after a reboot, so please follow the next six steps to make sure it happens!

Create a new systemd service file for Redis:

nano /etc/systemd/system/redis.service

Copy and paste the following into Nano:

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

Then hit Ctrl+X to exit, Y to save, and enter to accept the file name.

Now reload the systemd daemon:

systemctl daemon-reload

Next, enable the Redis service to start on boot:

systemctl enable redis.service

Want to test if Redis will survive a reboot? Restart the service and see if it comes back up:

systemctl restart redis

By now you're probably thinking, “Okay cool, but you normally write about WordPress and is there a tie-in or?”

And that's a fair question so let me take a second to tie it all together for you…

So there's this WordPress plugin called Redis Object Cache which tracks queries sent to the SQL database (generally MariaDB, MySQL, or PostgreSQL) and caches them along with the result. This means fewer calls to the SQL database and, because Redis stays in memory, faster load times. A total win-win.

If you install the Redis Object Cache plugin on your site the next thing you'll need to do is edit wp-config.php with the Redis Object Cache connection parameters and add the entire block right above line which reads /* That's all, stop editing! Happy publishing. */.

In my experience only two lines need to be changed:

define( 'WP_REDIS_PORT', 6379 );
// define( 'WP_REDIS_PASSWORD', 'secret' );

To this:

define( 'WP_REDIS_PORT', 10001 );
define( 'WP_REDIS_PASSWORD', 'YourVerySecretPasswordSSSSHHH' );

! Please note: I changed the port number from 6379 to 10001, removed the double slashes in front of the second line to activate the parameter, and changed the password from secret to YourVerySecretPasswordSSSSHHH.

At this point please activate the Redis Object Cache plugin then go to Settings > Redis then click the Enable Object Cache button. If everything worked correctly you should see:

Status: Connected
Drop-in: Valid
Filesystem: Writeable

And that it! You just added an object cache to your WordPress site and, hopefully, your webserver won't have to work as hard and pages will load faster than ever!

Leave a reply