I have been able to get what I wanted thanks to help from this answer, but I did make some modifications.
Basically, I created an update_wsl_ip_to_domain.ps1 file and saved to this PATH C:\Scripts\update_wsl_ip_to_domain.ps1, then I added the following scripts
#
# Powershell script for adding/removing/showing entries to the hosts file.
#
# Known limitations:
# - does not handle entries with comments afterwards ("<ip> <host> # comment")
#
$file = "C:\Windows\System32\drivers\etc\hosts"
$wsl_ip = (wsl -d debian hostname -I).trim()
$data = @('add','localwsl.com',$wsl_ip)
function add-host([string]$filename, [string]$hostname, [string]$ip) {
remove-host $filename $hostname
$ip + "tt" + $hostname | Out-File -encoding ASCII -append $filename
}
function remove-host([string]$filename, [string]$hostname) {
$c = Get-Content $filename
$newLines = @()
foreach ($line in $c) {
$bits = [regex]::Split($line, "\t+")
if ($bits.count -eq 2) {
if ($bits[1] -ne $hostname) {
$newLines += $line
}
} else {
$newLines += $line
}
}
# Write file
Clear-Content $filename
foreach ($line in $newLines) {
$line | Out-File -encoding ASCII -append $filename
}
}
function print-hosts([string]$filename) {
$c = Get-Content $filename
foreach ($line in $c) {
$bits = [regex]::Split($line, "\t+")
if ($bits.count -eq 2) {
Write-Host $bits[0] `t`t $bits[1]
}
}
}
try {
if ($data[0] -eq "add") {
if ($data.count -lt 3) {
throw "Not enough arguments for add."
} else {
add-host $file $data[1] $data[2]
}
} elseif ($data[0] -eq "remove") {
if ($data.count -lt 2) {
throw "Not enough arguments for remove."
} else {
remove-host $file $data[1]
}
} elseif ($data[0] -eq "show") {
print-hosts $file
} else {
throw "Invalid operation '" + $data[0] + "' - must be one of 'add', 'remove', 'show'."
}
} catch {
Write-Host $error[0]
Write-Host "nUsage: hosts add <ip> <hostname>n hosts remove <hostname>`n hosts show"
}
Next, I created a separate update_wsl_ip_to_domain.cmd file within the same directory as the ps1 file and I added the following commands.
PowerShell Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Powershell -File C:\Scripts\update_wsl_ip_to_domain.ps1
PowerShell Set-ExecutionPolicy Restricted
So in order to get the update_wsl_ip_to_domain.cmd file to run on startup, I created a shortcut to this directory C:\Users\<user_name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\ and gave it admin privileges.
Finally, I also added another shortcut of the update_wsl_ip_to_domain.cmd file on my desktop with admin privileges also, which I run manually, because for some reason the previous one doesn't always run on startup.
UPDATES
On my Debian WSL distro, I thought of running apache on port 80 and nginx on port 81.
So in order to give them a static IP and a domain name I've edited my update_wsl_ip_to_domain.ps1 file and added the following code at the bottom.
#GIVING WSL NGINX A STATIC IP. IT RUNS AT PORT 81 ON MY DEBIAN
netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.65.43.21 connectport=81 connectaddress=$wsl_ip
#GIVING WSL APACHE A STATIC IP. IT RUNS AT PORT 80 ON MY DEBIAN
netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.65.43.22 connectport=80 connectaddress=$wsl_ip
Then I made the following entries to my hosts file on Windows 10.
127.65.43.21 localwslnginx.com
127.65.43.22 localwslapache.com
I believe that there is a lot of potential to this, I just might be able to run multiple apps on different ports with my apache or nginx.
I will make updates as I go on.
UPDATES 2 (28th, December 2021)
I have been able to run multiple apps over wsl with each of them having their unique domain names. So here's what my hosts file on windows looks like:
127.65.43.21 localwslnginx.com
127.65.43.22 localwslapache.com
Laravel Apps
127.65.43.22 evangrest.test
127.65.43.22 firstbarcodes.test
They must bear the same static IP with the localwslapache.com or localwslnginx.com for apache and nginx respectively.
For apache, I used the following config to get my laravel app running:
<VirtualHost *:80>
DocumentRoot "/data/www/firstbarcodes/public"
DirectoryIndex "index.php"
ServerName firstbarcodes.test
ServerAlias firstbarcodes.test
<Directory "/data/www/firstbarcodes">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Directory "/data/www/firstbarcodes/public">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
#Alias /js /data/www/firstbarcodes/public/js
#<Directory /data/www/firstbarcodes/public/js>
# Options Indexes FollowSymLinks MultiViews ExecCGI
# AllowOverride All
# Order allow,deny
# Allow from all
# Require all granted
#</Directory>
</VirtualHost>
You might also need to run this command sudo a2enmod rewrite for apache
I guess this way, I have been able to somehow create a hack for having a static IP for all my apache and nginx apps over WSL.
What I would love to do next is to get my WSL Debian to auto-start my apache and mariadb on windows startup.