WSL2 – Apache with Named Local Hosts

WSL2 (Windows Subsystem for Linux, version 2) is a great piece of software for all kinds of useful reasons. For one it means you don't need to have a separate Linux machine to develop on, and it also avoids you needing to install a VM using Oracle VM VirtualBox or Hyper-V, for example. Plus, it's easy to set up and for most things is probably as close as you'll need to working on a Linux machine, while still using your trusty Windows computer.

Upgrading from WSL to WSL2

Having used WSL for ages, for developing websites, I discovered that there might be some advantages to using WSL2, so I took the plunge, updating my Ubuntu WSL to use WSL2. My Apache config file had several named Virtual Hosts (for example, mysite1.me, and mysite2.me, etc.), which worked just fine with WSL, and the entries in my Apache config file in Ubuntu (e.g. /etc/apache2/sites-enabled/mysites.conf) looked like this:

<VirtualHost *:80>
DocumentRoot /var/www/mysite1/public_html/
ServerName mysite1.me
...
...
...
</VirtualHost>

... while my Windows hosts file (C:\Windows\System32\drivers\etc) simply had entries like this:

# localhost name resolution is handled within DNS itself.
....
127.0.0.1 localhost
127.0.0.1 mysite1.me
127.0.0.1 mysite2.me
....

... along with the other usual bits and pieces in the hosts file. (the dots above and below the addresses are just to indicate other entries which are unrelated to this problem).

However ...

The upgrade to WSL2 went fine, with no problems at all, but on trying to access my sites on the Ubuntu WSL, from my Windows browser, I found they wouldn't load. So, tinkering around for a while, I realized I could get the IP address of the WSL2 Ubuntu instance, and simply change my Windows hosts file, and the Apache config file to explicitly use the IP. This actually works, but one quickly realizes that, unlike with WSL, WSL2 doesn't use static IP addresses, so every time you reboot you'll get a different WSL2 IP address. This, of course, is a pain, and you don't want to be updating host and config files every time you reboot.

A Better Way

If I'd realized this right away, I'd have saved some hassle, so maybe it'll help you too. All you need to do is to leave your Apache config file alone, and simply modify your Windows hosts file to instead look like this:

# localhost name resolution is handled within DNS itself.
....
127.0.0.1 localhost
::1 localhost

# 127.0.0.1 mysite1.me
# 127.0.0.1 mysite2.me
::1 mysite1.me
::1 mysite2.me
....

Note that:

a) the two lines resolving the sites to 127.0.0.1 are commented out

b) a line has been added with "::1 localhost" (this may already exist in your hosts file, but it didn't in mine)

c) the sites are now resolving to ::1 instead of 127.0.0.1

That's it! No tinkering around with ever-changing IP addresses or config files, and everything works just as it's supposed to.

If you're wondering what "::1" is, it's the compressed version of the IPV6 loopback address  (which is 0:0:0:0:0:0:0:1).