How to set up virtual hosts on Windows

Overview

PHP developers know that they can only have one domain per server, which is http://localhost. However, what if we wanted to have more than one project? It’s simple! Under the root of our server, we can separate projects into different folders and access them as follows:

  • http://localhost/projectA
  • http://localhost/projectB

This solution seems to be appropriate, but:

  • It does not reflect all the realities in the production server, where each site has its own domain (http://projectA.com).
  • You may encounter issues with relative paths.

Given these reasons, it’s recommended to have a local environment that looks a bit similar to the production one.

In this shot, we’ll learn how to set up a virtual host on a Windows machine.

First, let’s make sure we have everything we need to get started.

Prerequisites

  • Be on a Windows machine.
  • Have a LAMP environment installed. For example MAMP for Windows.
  • In the root of your server (htdocs on MAMP), create a folder for our project with unigrasys as it name. You can put a basic PHP “hello world” inside index.php.
MAMP on Windows 10
MAMP on Windows 10

In the next step, we’ll set up a virtual host for a website called http://unigrasys.local.

Create a new virtual host

To add a new virtual host, we need to edit the configuration file httpd-vhosts.conf. This file is located at C:\MAMP\bin\apache\conf\extra\httpd-vhosts.conf.

With your editor, open the configuration file and at the end of the document add the code snippet below.

<VirtualHost *:80>
    DocumentRoot "C:\MAMP\htdocs\unigrasys"
    ServerName unigrasys.local
    ServerAlias www.unigrasys.local
</VirtualHost>

Apache configuration files use directives to set rules that should be followed by the server.

  • DocumentRoot: This specifies the path from which the server will serve files.
  • ServerName and ServerAlias: The domain name we want the server to respond to. In our case, the server will respond to http://unigrasys.local and www.unigrasys.local.

Note:

  • You should not add a trailing slash on DocumentRoot directory path.
  • Windows does not allow domain names with .dev.

Register the new virtual hostname

After we create our new domain, we allow the computer to recognize it.

To register a host name, we need to edit the hosts file, which is located at C:\Windows\System32\drivers\etc\hosts. At the bottom of the file, add the name of your virtual host.

127.0.0.1 unigrasys.local

The hosts file requires administrator privilege to edit. See the note below on how to do so.

Note: How to open a file as admin:

  • Open Notepad (or any text editor) as admin.
  • Open the file from Notepad with “Ctrl + O”.
  • Edit and save.

Enable virtual host conf

This is the last step. Virtual host is disabled by default. The file responsible for this is httpd.conf. Let’s edit it.

Go to C:\MAMP\conf\apache and open httpd.conf.

Find the line below:

# Virtual hosts
# Include conf/extra/httpd-vhosts.conf

Now we’ll uncomment the line.

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

That’s all! Restart MAMP (or whatever LAMP stack you have) and enjoy your new domain!

Conclusion

Virtual hosting is a technique used to allow one web server to host more than one domain. The most used virtual hosting type is the name-based one.

In this shot, we learned how to create a virtual host on Windows. We successfully completed the following steps:

  • Create a new virtual host in httpd-vhosts.conf file.
  • Register it in the hosts file.
  • Enable virtual host in httpd.conf.

Note: To learn how to create a virtual host on GNU/Linux, visit this shot.