大网数据广告活动
服务器运维问答

Wsl ubuntu virtualhost and hosts file on windows

· 52CE 我爱测

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…

问题原文

原帖链接 · yossi · CC BY-SA 4.0

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>

已采纳解答

原帖链接 · Torsten Homberger · CC BY-SA 4.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-03-31)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。