How do I connect with an external redis server?

In order for you to connect to a separate, remote redis server we have to change some configuration values.

By default, your redis server is listening on this address:

0.0.0.0 ::1

This is normal, you can leave this as it is. It means that the redis service is listening on all connections. This also brings a dangerous fact: it allows everyone to connect to your redis server (if the firewall allows this).

By default, the port 6379 (the default redis port) is closed. You have 2 options to protect the redis server.

Note: If you're using AWS, you will also have to edit the security rules group inside AWS itself, to open up port 6379.

Allow from explicit IP address

Allow the port 6379 inside the firewall with an explicit IP address where the connection comes from:

Using a password

Of course in this case you will also have to open up port 6379, which is also possible with an explicit IP address. Set a password inside the redis configuration, you can do this by editing the /etc/redis/redis.conf file.

You can specify a password using the requirepass key inside the configuration file. It is recommended to use a long string random string, an example:

requirepass VT0rZuyWBPd5UL5M3TGR

Next test if you can connect to your redis server with a remote application. A Laravel example inside the .env file could look like this:

REDIS_HOST=120.204.227.47
REDIS_PASSWORD=VT0rZuyWBPd5UL5M3TGR
REDIS_PORT=6379

We also have a sample installation for you to test if you're able to connect to your redis server remotely. You can clone this project from here: https://github.com/ploi-deploy/laravel-redis-example

05 October 2020
99 views