Creating Apache Virtual Hosts on Windows

Virtual hosts can be bound to a host name (for example, study.loc), to IP (for example, 127.0.0.3) and to a port (*:81). The second and the third way are good because we do not have to provide DNS resoling of virtual host name (this is usually achieved by changing the system C:\Windows\System32\drivers\etc\hosts file or using a personal DNS server with the corresponding records).

All three ways will be discussed. But let's start with a little preparation. Create a directory where we will host our virtual hosts. For example, C:\Server\data\htdocs\virthosts\. And in this directory we will create three new folders: host1, host2, host3. In each of them, create an index file (index.htm) with any content that would allow you to identify that you are in the right host. I will make the following entries in the corresponding host index files, for the first host:

You reached host1.

For the second:

You reached host2.

For the third:

You reached host3.

Now proceed to configure the virtual hosts.

In the C:\Server\bin\Apache24\conf\httpd.conf file, find and uncomment the following line:

Include conf/extra/httpd-vhosts.conf

In the C:\Server\bin\Apache24\conf\extra\httpd-vhosts.conf file, comment out (or delete) the lines that are listed by default.

IP Based Virtual hosts

For virtual hosts that are bind to IP, several IPs are required. However, on your local server, you can do this trick, because any address in the range from 127.0.0.0 to 127.255.255.255 reflects to 127.0.0.1.

Try typing 127.0.0.1 in the web-browser address bar, and then try 127.0.0.2, 127.0.0.3, 127.0.0.4, 127.0.0.5, and so on. As you can see, we have a lot of IPs, and you can attach virtual hosts to each of them.

Add the following lines to the C:\Server\bin\Apache24\conf\extra\httpd-vhosts.conf file:

<VirtualHost 127.0.0.2:80>
    ServerAdmin webmaster@www1.example.com
    DocumentRoot "C:/Server/data/htdocs/virthosts/host2/"
</VirtualHost>

Restart the web-server (do a restart after each modification of the configuration file):

c:\Server\bin\Apache24\bin\httpd.exe -k restart

And now open 127.0.0.2:

In this case, our IP settings are not affected other IP 127.0.0.*, as well as localhost.

Ports Based Virtual hosts (different sites on different ports)

Now add the following lines to the C:\Server\bin\Apache24\conf\extra\httpd-vhosts.conf file:

Listen 81
<VirtualHost *:81>
    ServerAdmin webmaster@www1.example.com
    DocumentRoot "C:/Server/data/htdocs/virthosts/host3/"
</VirtualHost>

The port can be replaced with any other one, not occupied by your system. You can also specify a specific IP instead of an asterisk. Restart web-server and open in the browser http://localhost:81:

Hostname Based Virtual hosts

The following method requires editing the C:\Windows\System32\drivers\etc\hosts file (or adding records in your own DNS server). Create any host name, preferably not the same as the real one. Examples are supersite, study.loc, video.ofme. I'll take as an example study.loc. Open the C:\Windows\System32\drivers\etc\hosts file hosts and add:

127.0.0.1 study.loc

And add the following lines to the file C:\Server\bin\Apache24\conf\extra\httpd-vhosts.conf:

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "C:/Server/data/htdocs/"
</VirtualHost>

<VirtualHost *:80>
    ServerName study.loc
    DocumentRoot "C:/Server/data/htdocs/virthosts/host1/"
</VirtualHost>

Restart the server and go to http://study.loc in the browser:

Similarly one can configure subdomains (dir1.study.loc, dir2.study.loc, dir3.localhost, etc.).

Automatic creation of virtual hosts / subdomains

In the C:\Server\bin\Apache24\conf\httpd.conf file, find and uncomment the following line:

LoadModule vhost_alias_module modules/mod_vhost_alias.so

And also, if you did not do this earlier, the line:

Include conf/extra/httpd-vhosts.conf

In the C:\Server\data\htdocs\virthosts\ directory, create the localhost folder (now it will be displayed when you type http://localhost in the browser).

In the file C:\Server\bin\Apache24\conf\extra\httpd-vhosts.conf, add the following line

VirtualDocumentRoot "C:/Server/data/htdocs/virthosts/%1/"

Restart the server.

Now any folder created in C:\Server\data\htdocs\virthosts\ will be available at the address of the form

http://folder_name.localhost

If you open http://host1.localhost, you will see the contents of C:\Server\data\htdocs\virthosts\host1\, when you open http://super.localhost, we will see the site in C:\Server\data\htdocs\virthosts\super\ etc.

Note the notation “%1” — it means the first part of the name of the requested virtual host. That is, for the requested host super.localhost, “%1” will mean "super".

Instead of “%1” you can specify “%0” — this means a fully qualified name, that is, for the requested host super.localhost, the characters “%0” will mean super.localhost and, therefore, the super.localhost folder will be searched, not super (as it does when using %1).

Remember, hostnames like *.localhost are automatically translated to local IP addresses. But if you want to use other host names, then specify them in the C:\Windows\System32\drivers\etc\hosts file so that the computer understands which name corresponds to which IP address.

If you add the following to the file C:\Server\bin\Apache24\conf\extra\httpd-vhosts.conf instead of the previous line with “%1”:

VirtualDocumentRoot "C:/Server/data/htdocs/virthosts/%0/"

then any folder created in C:\Server\data\htdocs\virthosts\ will be available at an address like http://folder_name

Note that .localhost is now ignored. In fact, you can use .localhost as well, simply by creating folders “host1.localhost”, “host2.localhost” and so on.

Recommended for you 'How to protect the Apache web server from hacking in Windows'.

Recommended for you:

2 Comments to Creating Apache Virtual Hosts on Windows

  1. Daniel Martinez says:

    Hi, i had followed the instructions on the page: https://miloserdov.org/?p=55 and now that a followed the eteps of "Automatic creation of virtual hosts / subdomains"  i can not see the phpmyadmin page…

    • Alex says:

      Hello! Please pay attention to the line:

      ‘In the C:\Server\data\htdocs\virthosts\ directory, create the localhost folder (now it will be displayed when you type http://localhost in the browser)’.

      So you must move the phpmyadmin folder in the C:\Server\data\htdocs\virthosts\localhost directory.

Leave a Reply

Your email address will not be published. Required fields are marked *