Skip to content

Instantly share code, notes, and snippets.

@monkishtypist
Last active March 6, 2024 22:11
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save monkishtypist/c16f3f7fb0c689641e1a156cd839cecf to your computer and use it in GitHub Desktop.
Save monkishtypist/c16f3f7fb0c689641e1a156cd839cecf to your computer and use it in GitHub Desktop.
Nginx virtual host example for Windows Subsystem for Linux (WSL)
# Location: /mnt/c/windows/system32/drivers/etc/hosts
127.0.0.1 virtualhost.local www.virtualhost.local
<?php
// Location: /mnt/c/server/www/virtualhost.local/html/index.php
phpinfo();
# Location: /etc/nginx/sites-available/virtualhost.local.conf
server {
listen 80;
listen [::]:80;
root /mnt/c/server/www/virtualhost.local/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name virtualhost.local www.virtualhost.local;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
@monkishtypist
Copy link
Author

monkishtypist commented Jan 16, 2019

  1. Create a new virtual host configuration file sudo nano /etc/nginx/sites-available/virtualhost.local.conf
  2. Add your new domain to the hosts file sudo nano /mnt/c/windows/system32/drivers/etc/hosts
  3. Create a new symlink for your virtual host sudo ln -s /etc/nginx/sites-available/virtualhost.local.conf /etc/nginx/sites-enabled/virtualhost.local.conf and restart Nginx sudo service nginx restart
  4. Create and test your virtual host with an index file sudo nano /mnt/c/server/www/virtualhost.local/html/index.php

Note: replace virtualhost.local with your own virtual domain. You may also need to replace /mnt/c/server/www/ with your server location.

@denniel-sadian
Copy link

You saved my life, man ❤

@miftahafina
Copy link

Thanks.

Sometimes I forgot to edit hosts file on windows.

@mainiomooses
Copy link

Thanks. Clear, concise, works.

WSL + Ubuntu 18.04 / MariaDB 10.4 / PHP 7.3-FPM / NGINX 1.14.0

@z1haze
Copy link

z1haze commented Oct 19, 2021

Thanks for this. I was hoping maybe you had something in your steps that I somehow missed, but I had done everything the same. I cannot get my local domain to resolve. It just spins for a second or so then shows the 'site cannot be reached' error. I can get to the default nginx page by using localhost, 127.0.0.1 and through the IP of the wsl instance as well, but it just fails to resolve the domain as registered in my hosts file. thoughts?

@monkishtypist
Copy link
Author

@z1haze every instance is different but double check your hosts file. Make sure your symlink is correct. And triple check for typos. ¯\_(ツ)_/¯

@logan-shakya
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment