Spin up a complete Nginx + Redis + multi‑version PHP stack on WSL2 Ubuntu 22.04 with just a handful of commands.
What | Why |
---|---|
Windows 10/11 + WSL 2 | Follow Microsoft’s WSL 2 guide if you haven’t. |
Ubuntu 22.04 inside WSL 2 | Other distros work, but the commands below assume Deb/Ubuntu paths. |
Optional but recommended – systemd enabled in WSL | Lets systemctl manage Nginx, Redis, PHP‑FPM, etc. |
1. Edit /etc/wsl.conf
in the WSL terminal:
# /etc/wsl.conf
[boot]
systemd=true
sudo apt update
sudo apt install -y nginx
sudo systemctl enable --now nginx # starts it and autostarts at boot
sudo apt-get install lsb-release curl gpg
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
sudo chmod 644 /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
sudo systemctl enable redis-server
sudo systemctl start redis-server
Test Redis is accepting commands:
redis-cli ping # → PONG
Firewall note (Windows): WSL2 listens only on the VM’s virtual NIC. If you need external access (e.g., from the host to Redis), create a Windows firewall rule or use
wsl --ip
forwarding.
The toolkit gives you two commands:
phpinstall <version>
– installs PHP x.y with a full extension setphpswitch <version>
– flips CLI + FPM + Nginx socket to that versionserve <domain>
- starts nginx config with foldername asdomain.test
Run the one‑liner (no sudo
—the script uses it internally where needed):
curl -fsSL https://raw.githubusercontent.com/kalprajsolutions/wsl2-laravel-php-development/main/install-php-toolkit.sh | bash
# – or –
wget -qO- https://raw.githubusercontent.com/kalprajsolutions/wsl2-laravel-php-development/main/install-php-toolkit.sh | bash
What it does
- Downloads
functions.sh
to~/.local/lib/wsl2-php-toolkit/
- Appends
source ~/.local/lib/wsl2-php-toolkit/functions.sh
to your~/.bashrc
- You’ll have the commands in every new shell.
Reload the shell once:
source ~/.bashrc # or open a new tab
# Install PHP 8.3 + common extensions + FPM
phpinstall 8.3
# Install PHP 8.1 side‑by‑side
phpinstall 8.1
# Switch Nginx + CLI to PHP 8.1 (creates /run/php/php-fpm.sock link)
phpswitch 8.1
Verify:
php -v
curl -s http://localhost | grep "PHP Version"
If nginx gives 404 cause of permissions issues run below comamnd and restart
sudo usermod -a -G $USER www-data
Update Laravel Installer after switching phpversion
composer global update laravel/installer