0

I have very little experience with virtual machines. I successfully installed WSL2 and ubuntu 20 and theninstalled a LAMP stack. It all works fine but when using a browser on my windows machine I can only access apache on the virtualised ubuntu using locahost. 127.0.0.1 says that it can't be reached. I've got all my development domains pointed at 127.0.0.1 in hosts so of course that doesn't work either.

The apache ports conf is

    Listen 80

<IfModule ssl_module>
        Listen 443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 443
</IfModule> 

Virtual hosts are

<VirtualHost *:80>
or <VirtualHost *:443>

BUT from the logs it looks like the requests to 127.0.0.1 arent even reaching apache, which makes sense given the 'can't be reached' message in the browser.

Can anyone point me in the direction of where I should be looking?

Thanks.

Tofuwarrior
  • 111
  • 1
  • 4
  • Did you mean WSL 2? – Michael Harvey Mar 13 '21 at 18:51
  • Yes, I did! Thanks for flagging that. Have edited accordingly. – Tofuwarrior Mar 17 '21 at 22:24
  • Apache treats localhost and 127.0.0.1 differently, if everything is working, why is it important for you to be able to use 127.0.0.1? You will probably want to explicitly configure 127.0.0.1 has the virtual host address – Ramhound Mar 17 '21 at 22:33
  • WSL2 operates in a Hyper-V VM with its own virtual NIC, running NAT'd behind the Windows host. WSL1, on the other hand, ran bridged with the Windows NIC. See [here](https://superuser.com/questions/1618337/localhost-and-127-0-0-1-working-but-not-ip-address-in-wsl-windows-10) – Michael Harvey Mar 17 '21 at 22:39

1 Answers1

0

Thanks for comments. Found solution towards the end of this whole wsl2 ubuntu setup guide here: https://dev.to/aitorsol/wsl2-windows-linux-subsystem-a-guide-to-install-a-local-web-server-ubuntu-20-04-apache-php8-y-mysql8-3bbk

The script included in that post is slightly incompatible with my system because ifconfig has been deprecated so the version I used uses ip addr on the first line rather than ifconfig. From reading elsewhere eth0 may not be used on your system so you may need to adjust that first line according to the adapter

Edit what ports you want forwarding to the virtual machine in the script.

:

$remoteport = bash.exe -c "ip addr list 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,10000,3000,5000,8080);


#[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";
}

Once you have run this you should be able to access apache on the virtual machine by accessing your windows machines LAN IP and obviously 127.0.0.1 works on the machine itself

After testing it I set it run it from .profile so that the port forwarding changes occur when the virtual machine starts up.

/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe "C:\Users\user\wsl-networking-startup-ip-change.ps1"
Tofuwarrior
  • 111
  • 1
  • 4