6

I try to access it through my windows browser and fail. what is my mistake?

I use WSL2 on windows 10, with ubuntu 20.
I have an apache server and a laravel app which I installed at /var/www/law
In my windows hosts file I've added this:

127.0.0.1   law.dev
::1 law.dev

and this is the law.conf file which is located at /etc/apache2/sites-available.

<VirtualHost *:80>
    ServerName law.dev


    DocumentRoot /var/www/law/public

    <Directory /var/www/law>
    Options Indexes MultiViews
    AllowOverride None
    Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

1 Answer 1

6

Your WSL2 linux system has its own ip address.

You can get its current ip address with this command (on your windows host, with PowerShell or CMD.EXE): wsl hostname -I

You can now use this ip address in your hosts file (on windows host).

The ip address of your WSL2 system is not static so if you restart your pc your WSL2 host will have a different ip address.

Have a look at this ticket for more informations: https://github.com/microsoft/WSL/issues/4210

If you want to access your apache webserver on localhost you have to use WSL1 which uses the interface of the windows host.

You can convert your WSL2 linux system to WSL1 with this command ("Ubuntu" might differ on your system):

wsl --set-version Ubuntu 1

To get the name of your WSL2 system:

wsl --list --verbose

Output (Example):

  NAME      STATE           VERSION
* Ubuntu    Running         2
3
  • 1) great. thanks. 2) i never would have thought about it, since the http://localhost redirect to the wsl.. why is that?
    – yossi
    Commented Mar 31, 2021 at 11:51
  • 4
    The ::1 law.dev entry in my Windows hosts file works fine for me with WSL2 on Ubuntu 18.04. Just in case the OP overlooked this: their browser URL should be law.dev. I find it useful to put a static HTML file like test.html in the my application home directory (in the OP's case, /var/www/law) and then try that in my browser (law.dev/test.html) to ensure my problem is not caused by PHP, etc.
    – Martin_W
    Commented Jun 9, 2021 at 4:52
  • This is really useful. What are the consequences of setting wsl version to 1 rather than 2? Commented Aug 29, 2022 at 22:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .