Already have your own site(s) up and running on localhost? You need virtual hosts for local domains. Keep the same url structure as production with a .local url.
In this video we look at setting up apache virtual hosts on Windows Subsystem for Linux (WSL)
Why set up virtual hosts?
Virtual hosts allow you to have local domains instead of using localhost/site-name in your local URLs.
Setting up virtual hosts on WSL is a little bit different than windows/or linux because you have to update both OS’s hosts file.
You’ll need to repeat these steps for every virtual host you want to add.
Set up your hosts file
On Windows, open notepad as adminstrator and navigate to c:/windows/system32/drivers/etc/ and open the hosts file. You may have to click see all file types to see this file.
At the bottom, add new lines:
127.0.0.1 mysite1.local
::1 mysite1.local
On WSL, edit /etc/hosts, adding the same two lines to the bottom:
127.0.0.1 mysite1.local
::1 mysite1.local
Set up your apache virtual host conf file
Navigate to /etc/apache2/sites-available
Create a file named mysite1.local.conf here (replacing mysite1.local with your local domain)
Add the following to the file, replacing with your details for servername and document root.
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName mysite1.local
DocumentRoot /var/www/html/mysite1
<Directory /var/www/html/mysite1>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Enable your site
sudo a2ensite mysite1.local.conf
Reload apache
sudo systemctl reload apache2
You’re done setting up a virtual host!
Visit your site in your browser at http://mysite1.local (replace with your site domain)
We learn how to set up SSL in a future video.