In C:\Windows\System32\drivers\etc\hosts
1 2 3 | 127.0.0.1 test.tld ::1 test.tld #::1 is really important sometimes |
1 2 3 4 5 | Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WinSock2\Parameters\AppId_Catalog08F7A3] "AppFullPath"="C:\\Windows\\System32\\wsl.exe" "PermittedLspCategories"=dword:80000000 |
Save the above as a .reg file and run it to update registry
Powershell script=> save as .ps1 file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { $arguments = "& '" + $myinvocation.mycommand.definition + "'" Start-Process powershell -Verb runAs -ArgumentList $arguments Break } $remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '" $found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'; if( $found ){ $remoteport = $matches[0]; } else{ echo "The Script Exited, the ip address of WSL 2 cannot be found"; exit; } #[Ports] #All the ports you want to forward separated by coma $ports=@(80,443,3306); #[Static ip] #You can change the addr to your ip config to listen to a specific address $addr='0.0.0.0'; $ports_a = $ports -join ","; #Remove Firewall Exception Rules iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' "; #adding Exception Rules for inbound and outbound Rules iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP"; iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP"; for( $i = 0; $i -lt $ports.length; $i++ ){ $port = $ports[$i]; iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr"; iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport"; } |
netsh interface portproxy show v4tov4
check if your proxy is working
Most of the times you may not even need a proxy for apache2. However mysql inside WSL is extremely tricky for some reason. I have ALWAYS had issues until I changed the default port. Hence try this in your file:
1 | sudo nano /etc/mysql/my .cnf |
1 2 3 4 5 6 7 8 9 10 | !includedir /etc/mysql/conf.d/ !includedir /etc/mysql/mysql.conf.d/ [mysqld] explicit_defaults_for_timestamp= true bind-address = 0.0.0.0 user =root pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3334 |
Now you should be able to connect to this mysql instance from some gui app on windows too like dbeaver with localhost on port 3334.
If you are victim of unpredictive WSL behavior, do leave a comment
Leave a Reply