Running Redis on Windows via WSL

Running Redis on Windows via WSL

Port Forwarding Redis from Ubuntu 22.04 on WSL2 to it's Windows 11 host

I assume that you are on Windows 11.

If you want to install Redis on Windows and access redis data from a client application like redis-cli or using a script, we're out of options to do a classic Next Next Finish type of installation.

The current version of Redis stands at version 7.0.8 and there are no Windows binaries for Redis - at least not past version 3.2.100 available at https://github.com/microsoftarchive/redis/releases/tag/win-3.2.100

The official guide at Redis shows how to install Redis on WSL which is for example, running Ubuntu 22.04 on Windows Subsystem for Linux. But the last point "Connect to Redis" shows how to use redis-cli on the WSL itself.

But if we're developing our web apps on a Windows environment, even though we'll ultimately be deploying to a Linux server, we still would need to connect to the Redis Server running on Ubuntu at WSL from the Windows host.

From https://learn.microsoft.com/en-us/windows/wsl/install : Open PowerShell or Windows Command Prompt in administrator mode by right-clicking and selecting "Run as administrator", enter the wsl --install command, then restart your machine.

wsl --install

Now run wsl from the Start Menu - you should be into a Linux terminal.

sudo apt install lsb-release
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

sudo apt-get update
sudo apt-get install redis

You would need to do sudo vi /etc/redis/redis.conf

Comment out bind 127.0.0.1 ::1 in /etc/redis/redis.conf Comment out protected-mode yes and change it to protected-mode no

sudo service redis-server start

https://serverfault.com/questions/861519/how-to-turn-off-protected-mode-in-redis

This is really not required since we edited the redis.conf file to include protected-mode no, but cross-check anyway :

$redis-cli
127.0.0.1:6379> CONFIG GET protected*
(empty list or set)
127.0.0.1:6379> CONFIG SET protected-mode no

$ifconfig - note down the inet value under eth0 - lets say it's 172.20.143.242 - this is the tricky part - you need to update this IP address which is an internal dynamically generated IP at WSL each time you restart your laptop. Right now I don't know of a way to a fixed / static IP.

Back to the browser in Windows : Install NodeJS from https://nodejs.org/en/ - Download the latest LTS, as of now it's 18.14.2 - typical Next > Next > Next > Finish.

https://redis.com/blog/get-redis-cli-without-installing-redis-server/

Open PowerShell and enter npm install -g redis-cli

This may or may not be required : Open PowerShell as Adminstrator and run :

netsh interface portproxy add v4tov4 listenport=6379 listenaddress=0.0.0.0 connectport=6379 connectaddress=172.20.143.242

Close PowerShell (which we opened as Adminstrator)

Open PowerShell and enter : rdcli -h 172.20.143.242

PS C:\Users\John> rdcli -h 172.20.143.242
172.20.143.242:6379> SET foo bar
OK
172.20.143.242:6379> GET foo
bar
172.20.143.242:6379>

Now let's get this working via PHP - I assume you have XAMPP.

Install PECL :

https://www.php.net/manual/en/install.windows.pecl.php
https://windows.php.net/downloads/pecl/releases/
https://windows.php.net/downloads/pecl/releases/redis/5.3.7/
Download : php_redis-5.3.7-8.1-nts-vs16-x64.zip

Copy the php_redis.dll file and paste it to C:\xampp\php\ext

Then edit C:\xampp\php\php.ini and add extension=php_redis.dll somewhere after extension=bz2

I assume this line is uncommented out : extension_dir="C:\xampp\php\ext"

Please read this if you're facing errors related to php_redis - https://stackoverflow.com/a/63125621/126833 - not only do you need to choose the right PECL release for redis for your PHP version - its different for PHP 7.2, PHP 7.4 and PHP 8.0. For PHP 7.4 it's 5.2.2 (https://pecl.php.net/package/redis/5.2.2/windows) and for PHP 7.2 I had to download 3.1.4 from https://windows.php.net/downloads/pecl/releases/redis/. For PHP 8.x it should be 5.3.7.

Create a file on say D Drive (D:) and name it redis-test.php and enter :

<?php 
$redis = new Redis(); 
$result = $redis->connect('172.20.143.242', 6379); 
# print_r($result);

$redis->set("blogs", "hashnode,blogger,dev.to,medium"); 
echo "blogs string = " .$redis->get("blogs");

echo "\n";

$redis->set("blogs_json", json_encode(["hashnode" => "anjanesh.dev", "blogger" => "code.anjanesh.net","dev.to" => "dev.to/anjanesh", "medium" => "medium.com/@anjanesh"])); 

echo "blogs json = " .$redis->get("blogs_json");

echo "\n";

print_r(json_decode($redis->get("blogs_json")));
?>
PS D:\> C:\xampp\php\php.exe .\redis-test.php
blogs string = hashnode,blogger,dev.to,medium
blogs json = {"hashnode":"anjanesh.dev","blogger":"code.anjanesh.net","dev.to":"dev.to\/anjanesh","medium":"medium.com\/@anjanesh"}
stdClass Object
(
    [hashnode] => anjanesh.dev
    [blogger] => code.anjanesh.net
    [dev.to] => dev.to/anjanesh
    [medium] => medium.com/@anjanesh
)

If you find setting this up difficult, then try a remote Azure Redis Cache for which you just have to connect to the host and get the client (CLI or PHP script) to work with the data stored at Azure.

You can get 250 MB for $0.022/hour which, at the most, would amount to $15 a month if your localhost website were to be refreshed every hour for the whole month. So basically you'll be in the safe zone for localhost development amounting to ~$1 a month only. For testing purposes, it would be within a few cents. See Pricing details here.