Hey @anjanesh
I read your blog post, so I guess you managed to get something going on your end, but for posterity I'll post my alternative solution here.
I think your method was a bit overly complicated and insecure, as you disabled Redis' protected-mode, without setting up any firewall rules to restrict access. This could open up your Redis server to the entire internet.
Your Windows OS can access the localhost address of your WSL distro, so once you get Redis server running, you just need to point Laravel to 127.0.0.1:6379.
Install WSL
-
wsl --install - I also had to run
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linuxin a PowerShell window with elevated privileges
Get Ubuntu Ready
- Install Redis server:
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
- Enable Redis service so it will start automatically on boot:
sudo systemctl enable redis-server.service - Start Redis service for this session:
sudo service redis-server start - Launch Redis CLI:
redis-cli - Monitor server to confirm connection later:
monitor
Set-up Laravel
This part should be super easy if your using the default Laravel configuration. For simplicity, I'll be using the Predis client installed via Composer. Note: I'm using Laravel 10, configuration for older versions may differ.
- Install Predis:
composer require predis/predis - Setup your .env variables:
CACHE_DRIVER=redis
...
REDIS_CLIENT=predis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=
REDIS_PORT=6379
Testing
Let's launch Tinker so we can verify our Laravel app has access to the Redis server running in WSL.
- Launch Tinker:
php artisan tinker - Test cache:
cache()->put('test', 'Hello World!') - If you're still running
monitorfrom yourredis-clithrough your WSL terminal, you should see a log of interactions with your application.
A Note On Security
If you have some special circumstances that require you to access your redis-server over the internet, I do not recommend disabling protected-mode if at all possible.
If you absolutely have to disable it, make sure to configure ACL's for Redis, and ensure your connecting to it over a secure internet connection. If you're using public WIFI, turn on your VPN.
Alternative Measures
Here are some starting points for alternative options if communicating over localhost doesn't suit your use-case:
- Only allow incoming requests for your PC's IP address. See Routing Tables
- Configure your Ubuntu distro's routing table to forward requests from your PC to
127.0.0.1:6379on the virtualized distro. See This Blog Post