Let’s see step by step how create virtual domains in windows. Mastering this simple apache configuration we can work on our projects locally before uploading them to the server. Furthermore, with the virtual hosting you will be able to access local sites with urls similar or identical to those you might have on your server.
Domains such as www.srcodigofuente.com or www.dominioxxx.local will be accessible from our browser, and redirected to our local site.
You can watch this article on video in its entirety or read it, whichever suits you best.
- Video tutorial to create Virtual Hosts
- What is Hosting or virtual domain?
- How to create virtual domains in Windows
- Modify the hosts file
- Activate the Apache virtual hosting service
- Add the domain to the Apache vHosts file
- Final notes on the use of Virtual Hosts
- Do you already have your virtual host created?
Video tutorial to create Virtual Hosts
What is Hosting or virtual domain?
According to the documentation of the official Apache website, the virtual hosting is:
The term Virtual Hosting refers to running more than one website (such as www.company1.com and www.company2.com) on a single machine
Although this definition is more aimed at explaining the use of this feature in Hosting, it is approximately what we will do in our team. Virtual Hosting is achieved by configuring several domains in the same IP, that is, it is a redirection of names (domains) to different directory routes using an HTTPD server such as Apache.
How to create virtual domains in Windows
Creating a virtual domain will be very fast once you have done it two or three times and have understood how each part of the procedure works. We will modify three files:
- The configuration file hosts Windows
- The Apache configuration file httpd.conf
- the file of virtual hosts httpd-vhosts.conf from Apache Server.
Modify the hosts file
We are going to modify a Windows configuration fileso, before modifying anything, it is advisable to know what we are playing.
The Hosts file (without extension) is a native Windows file. It is a file used by the operating system to associate domain names with IP addresses.. If this file exists in the path c:\system32\drivers\etc\, the operating system examines it before making a DNS query (to find out which ip corresponds to the domain by searching the internet). This means that, if we indicate a domain as www.amazon.es and we redirect it, the browser will no longer visit the normal address of this domain, but the ip indicated by us.
Now that we know what information we are manipulating, we are going to see how to add our new domain. In my case it will be srcodesource.local and I’m going to redirect it to localhost (127.0.0.1) to port 80. We open with administrator permission the file host alreadyAdd the following line, then save the changes:
Now that we have added our virtual domain on windows we can test it by starting Apache and, later, writing in our preferred browser the url of the virtual domain: https://srcodesource.local.
Remember that if you have changed or are going to change the Apache port, the VirtualHost configuration will not be the same for you. You must indicate your custom port instead of 80.
Activate the Apache virtual hosting service
Now that we have configured our redirection to domain localhost, we must enable apache service that allows us to use that virtual domain.
Specifically, we must activate the module vhosts_alias and direct them to the directory of our site. We will activate this feature from the Apache httpd.conf file.
vhosts module in Wamp
Wamp allows two types of activation of the Apache module: graphically or by directly modifying the file:
- Graphical vhosts module: If you use the Wamp GUI for Apache configurations, you can go to Apache > Apache Modules > vhosts_alias_module.
- vhosts module from file: in the route c:\wamp\bin\apache\apacheX.X.XX\conf\httpd.conf We must look for the line that indicates “Include conf/extra/httpd-vhosts.conf” and verify that it does not have a hash mark in front of it, if it did we must uncomment it.
vhosts module in xampp
In Xampp, the configuration goes through opening the file httpd.conf in the path “c:\xampp\apache\conf” and checking that the line with the inclusion of the module “Include conf/extra/httpd-vhosts.conf” is not commented , that is, it does not have a pad in front of it.
Add the domain to the Apache vHosts file
We already have activated the module that is in charge of treating the virtual domains directed to localhost. TOtime we need to configure Apache so that it knows how to interpret each local domain and redirect it to the folder where we have our project.
How to add the domain using Xampp
in the folder extra inside the path where the file was located httpd.conf find the configuration file virtual hosts httpd-vhosts.conf.
Inside the file we find commented configuration code (with hash marks at the beginning of the line) with configuration examples.
We can use one of the examples to set up our first virtual domain. A demo like the following:
In order to use my local domain srcodesource.local and redirect it to a unique folder (in my case I have called it with the same name) in the folder htdocs from Apache, you should leave this configuration directive like this:
The key is in indicate in the ServerName parameter the local domain created in the file host Windows and in DocumentRoot the base path of the projectrespecting the absence of the final slash of the path.
How to add the domain using Wamp
To configure our virtual host in Wamp we must open the file httpd-vhosts.conf from the path C:\wamp\bin\apache\apacheX.X.XX\conf\
ANDn inside the file we will add a directive VirtualHost with our domain configuration and the path to the project folder:
The key is in indicate in the ServerName parameter the local domain created in the file host Windows and in DocumentRoot and Directory the base path of the projectrespecting the use or absence of the final slash of the route.
Final notes on the use of Virtual Hosts
As final notes I would like to add several concepts about the correct use of Virtual Hosts for our local tests:
- To use our virtual domains we must write the route in the browser as follows: https://midodominolocal.es
- We can create as many virtual domains as we wantadding them in the Windows Hosts file and in Apache’s httpd-hosts.conf.
- We must take into account that If we indicate the domain with the same name as a REAL domain, we will not be able to access the real one from our browser.
Do you already have your virtual host created?
I hope this tutorial has helped you! If so and you have created your virtual domain, I would greatly appreciate it if you left a comment or shared the content.
Also, if you have any questions or ideas to add, leave them below! so others who want to learn can also learn it.
Thank you and see you in another article!