How to set up virtual host in xampp
Before we discuss on how to set up a virtual host we need to know why do we need to set up a virtual host.
Why do we need to set up a virtual host?
As a developer we need to work on multiple projects and a web developer we do deal with web servers. We can setup multiple virtual host as per our need for each project, so that each local application or website can be accessible using specific URL on different ports.
For example , let consider your application is available in “xampp/htdocs/app1” and you will access this app1 in browser as “localhost/app1”
If we adopt the traditional method, to access our application available in our local system the issue can come.
For example, if we want to load or fetch any file form our project directory, our URL will be “locahost/app1/site/public/images/demo.jpg”. Due to an invalid path , files becomes inaccessible and as a result they wont get loaded in the page.
Using virtual host, you can load from the root of your document.
In this article lets discuss step-by-step process to setup virtual host in windows with xampp.
Step 1 – Open httpd.conf file available in “C:\xampp\apache\conf”
1.Search for “#Include conf/extra/httpd-vhosts.conf” and remove “#” (uncomment).
2.Search “Listen” keyword and add your new port number(for ex. 8000, 8001 etc) and save file.
Step 2 – Open “httpd-vhosts.conf” file from “C:\xampp\apache\conf\extra”
Add following lines
ServerName abc.dev.com ServerAlias abc.dev.com DocumentRoot C:\xampp\htdocs\abc\site\public Options +Indexes +Includes +FollowSymLinks +MultiViews AllowOverride All Require local
1.First open VirtualHost tag and write alias name for your application with a port.
2.ServerName: Hostname and port that the server uses to identify itself.
3.ServerAlias: Alternate names for a host used when matching request to name-virtual hosts
4.DocumentRoot: Complete execution path of your directory where your application is present.
5.Directory Tag: Inside directory tag we have to write path of document or folder where all files are available
6. Options +Indexes +Includes +FollowSymLinks +MultiViews
Step 3 – Open “hosts” file from “C:\Windows\System32\drivers\etc”
Add your virtual host name
ex. 127. 0. 0. 1 abc.dev.com
Step 4- Output
Step 5 – Restart your apache server and check your virtual host. Thats it.