You know we developers work on the local server, after completing the work we migrate the work on the local server. After setting up a virtual host, we can make multiple virtual hosts on the same server.
Example
Suppose if we are working on 2 projects in the local environment, we can make 2 domain URLs for both projects. Each website can be accessed by a specific name.
In this article, we are going to set up 2 virtual hosts on the local server. The domain names we will use here are test1.com and test2.com
Configure Virtual Hosts
Step 1: Go to the C:\xampp\apache\conf directory and open the httpd.conf file. Find the Virtual hosts text using ctrl +f and remove the # sign from there.
# Virtual hosts
# Include conf/extra/httpd-vhosts.conf
To
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
Step 2: Navigate the directory C:\xampp\apache\conf\extra and open the httpd.vhosts.conf file. See the below screenshot.
Now we need to paste the following code in the httpd.vhosts.conf file.
<VirtualHost *:80>
DocumentRoot "C:\XAMPP\htdocs\test1"
ServerName test1.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:\XAMPP\htdocs\test2"
ServerName test2.com
</VirtualHost>
Note: Here I have added code for 2 virtual hosts
You need to replace the project path name in the root directory and server name according to your requirement and save the file.
Now the httpd.vhosts.conf file will look like something like this:
Step 3: Now we need to open the host file.
Search Run on the window search bar, click on the run application and paste the following code:
C:\Windows\System32\drivers\etc\hosts
Open the file, and add your virtual host.
127.0.0.1 test1.com
127.0.0.1 test2.com
Note: I have added two virtual hosts. You need to change the hosts name according to your projects.
Now hosts file will look like this:
Virtual host configuration is done. Now restart the Apache server from the Xampp control panel.
For testing open the browser and browse the domains.
http://test1.com/
http://test2.com/
We have successfully configured two virtual hosts.
Please refer to the below article if you are not able to modify the changes in the Hosts file.
Not able to modify the Hosts file
Thanks for reading, feel free to reach out to me for any comments and suggestions. I hope you found the article helpful and you were able to setup the virtual hosts on your localhost xampp.