I'm trying to run a simple LAMP config on Ubuntu through WSL 2. So I installed Apache2, PHP and MariaDB. As it does not start automatically on WSL, I start everything with this command sudo service apache2 start && sudo service mysql start. Everything starts well, I can access localhost, PHP is working as well as MariaDB. But, when I try to add Vhost, it's not working that well. So, here is the Apache config file

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        # ServerName eyaka.local

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

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

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

<VirtualHost *:80>
        ServerName work.local

        ServerAdmin [email protected]
        DocumentRoot /home/axiol/work

        <Directory />
            Options FollowSymLinks
            AllowOverride all
        </Directory>
        <Directory /home/axiol/work/>
            Options Indexes FollowSymLinks
            AllowOverride all
            Require all granted
        </Directory>


        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn
        SetEnvIf Request_URI "\.(jpg|xml|png|gif|ico|js|css|swf|js?.|css?.)$" D$

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined env=!DontLog
</VirtualHost>

<VirtualHost *:80>
        ServerName perso.local

        ServerAdmin [email protected]
        DocumentRoot /home/axiol/perso

        <Directory />
            Options FollowSymLinks
            AllowOverride all
        </Directory>
        <Directory /home/axiol/perso/>
            Options Indexes FollowSymLinks
            AllowOverride all
            Require all granted
        </Directory>


        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn
        SetEnvIf Request_URI "\.(jpg|xml|png|gif|ico|js|css|swf|js?.|css?.)$" D$

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined env=!DontLog
</VirtualHost>

<VirtualHost *:80>
        ServerName adminer.local

        ServerAdmin [email protected]
        DocumentRoot /home/axiol/adminer

        <Directory />
            Options FollowSymLinks
            AllowOverride all
        </Directory>
        <Directory /home/axiol/adminer/>
            Options Indexes FollowSymLinks
            AllowOverride all
            Require all granted
        </Directory>


        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn
        SetEnvIf Request_URI "\.(jpg|xml|png|gif|ico|js|css|swf|js?.|css?.)$" D$

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined env=!DontLog
</VirtualHost>

And here is the Windows hosts file

127.0.0.1 work.local
127.0.0.1 perso.local
127.0.0.1 adminer.local

When I try to access any of the vhost, page loads for a while, then redirect to the https version (nothing makes this redirect, all folder just containt an empty HTML file for testing) and it ends with the page not loading. Anything I could do to make the vhost work?


Solution 1:

Ok, I randomly found the solution. Turns out Windows try to ping WSL with IPV4. So, you also need to attach your custom domain to the IPV6 of your local. To make it work here is how the hosts file should look like:

127.0.0.1 work.local
::1 work.local
127.0.0.1 perso.local
::1 perso.local
127.0.0.1 adminer.local
::1 adminer.local