Laravel Artisan Serve Website in WSL, Expose to LAN in Windows

One way to test a dev site on other devices on your LAN is to configure a proxy port listener for WSL then build assets and change the port on the Artisan Serve command.

Configure WSL

First find the IP address WSL is currently using, this may change when it restarts.

wsl hostname -I

Set a proxy port in Windows Powershell to forward traffic from WSL, requires admin rights. Replace the connectaddress value with the result from above.

netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=8000 connectaddress=172.20.224.105 connectport=8000

Check it has been set with

netsh interface portproxy show all

The setting for the proxy port can be deleted later with

netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=8000

Configure Laravel

Build assets

npm run build

Check the hot file has been removed from the public folder if Vite was being used, it will break the site now Vite is not running.

app/public/hot

Run PHP Artisan serve with new port.

php artisan serve --host=0.0.0.0 --port=8000

Firewall Configuration

If the site is still not accessible on the LAN you may need to an inbound rule in Windows Firewall. From Powershell with elevated rights run.

New-NetFirewallRule -DisplayName "Laravel Dev Server - TCP 8000" -Direction Inbound -Protocol TCP -LocalPort 8000 -Action Allow -Profile Private -RemoteAddress LocalSubnet

The profile setting may need changing depending on your environment.

Leave a Reply

Your email address will not be published. Required fields are marked *