Virtual Host is a configuration in Apache that allows multiple domains to run on one server. There is another concept mentioned in Nginx that has the same function as Virtual Host called Server Block. In this article, I just want to share with you strange about Virtual Host how to configure a Virtual Host in Windows, here I use XAMPP as a development environment.Configure Virtual Host in XAMPP
You can download XAMPP from https://www.apachefriends.org
After the installation is complete, open: /apache/conf/httpd.conf
Find the line: Include conf/extra/httpd-vhosts.conf, If you see a # at the beginning of the line, remove it. This line is only for loading config from another file into the httpd.conf.
With Xampp, this file is included by default. But I still want to update this step so that if you use other software to create Web Server such as Vertrigo, WAMP, App Server, etc, you can still use Virtual Host configuration. Especially, you can also install a file with a different name (this is not important because as long as you include the Virtual Host configuration in httpd.conf is okay).
Now open the file /apache/conf/extra/httpd-vhosts.conf, copy the code below and paste into the httpd-vhosts.conf file.
CustomLog "logs/yourdomain.com-access.log" common
Require all granted
You restart Apache by restarting the XAMPP software.
Ok, so you’ve finished configuring the Apache part, now you just need to register DNS for your domain and everything is done.
You open the file %windir%/system32/drivers/etc/hosts with notepad or any editor. Add to the end of the file and save it. If you cannot save the hosts file, be sure to turn off Readonly and try running again in Administrator mode.
When you type yourdomain.com address, the machine will prioritize to find the IP address of that domain in the hosts file of the operating system first. If not, it will find the IP address of this domain through DNS servers on the Internet. Because you have declared in the hosts file, it will use the IP address declared in it. Here the IP address – 127.0.0.1 points to the device it is accessing (which is your own computer – if configured on the server, it is the server itself). You can use the IP range from 127.0.0.1 -> 127.255.225.254, this is the IP range pointing to your device (Full range, not just 127.0.0.1).
Now you open your browser and type in the address bar to replace the ugly localhost with your own domain.
Using Virtual Host has several benefits:You can access any code directory without having to copy to htdocs folder in XAMPP.If you use the code folder in another partition, you don’t need to worry about backing up the code folder every time you reinstall Windows.Looks more dangerous. :v
You can also configure Vhost with WAMP, APPServer, USBServer, etc, the configuration is not much different because all these software uses Apache, Mysql, and PHP. Virtual host is a feature of Apache, not XAMPP, so any software using Apache can configure Virtual host in the same way, the only difference is the location of the httpd.conf file.