Virtual hosting is a method for hosting multiple domain names on a computer using a single IP address. One widely used application is shared web hosting. Beside this, It is very usefull to developer while developing his site locally or in dedicated server. Let’s me explain –
1. Let i’m developing a site locally, if the project folder name is “myapp”, i need to browse this site by http://localhost/myapp/, i may want to browse my site by http://dev.myapp.com
2. I’ve a larger site, developing locally, will have different domains which share login and all domain locate same folder. That is
if anyone logged in –
later he visits
he doesn’t need to login again by verifying the security, how can you test it locally?
3. Let you have a live site already(let http://www.mydomain.com), you are re-developing this site on a new dedicated server(lets ip is 220.127.116.11), so you are browsing your developing site by – http://18.104.22.168. But if you want to access your developing site with http://www.mydomain.com from your desktop while keeping that the live site accessible to others by http://www.mydomain.com
Hence the virtual hosts. There may be some other reasons that i can’t remember at this moment. Here are the ways to how to configure virtual hosts in windows, linux and mac.
Configuring virtual hosts in windows
let you have mydomain folder in your xampp htdocs want to browse your application by mydomain.com, find out hosts file and open it in notepad. Host file location is –
Add the following line bottom of this file
First part is loopback address, second part is the domain name by which domain you want to access that folder. So if you hit on mydomain.com, it will hit on your local server instead to search on internet. Now add this domain name on your httpd configuration file. To do this, open “C:\Program Files\xampp\apache\conf\httpd.conf” file(in xampp, you will find it inside apache/conf in other package), add the following lines at the bottom of this file –
# Use name-based virtual hosting. NameVirtualHost *:80 <VirtualHost *:80> ServerName mydomain.com DocumentRoot "C:/Program Files/xampp/htdocs/mydomain" CustomLog logs/mydomain.com-error_log combined ErrorLog logs/mydomain.com-access_log </VirtualHost>
So for mydomain.com port 80 requests(for https it should be 443), it loads site from mydomain folder. Restart your apache and hit on http://mydomain.com, see what’s come. Exciting, huh?
Configuring virtual hosts in Linux
In linux, this is little different. Check this here
Configuring virtual hosts in Mac MAMP package
Open hosts file which is in following location
Now add the following line at the bottom of hosts file
Now add the following lines in httpd configuration file(File is located here – “/Applications/MAMP/conf/apache/httpd.conf”
NameVirtualHost * <VirtualHost *> DocumentRoot "/Applications/MAMP/htdocs/mydomain" ServerName mydomain.com ServerAlias www.mydomain.com </VirtualHost>
This is little more different than previous one, this handle http and https requests. Also one alias is added too. Don’t forget to restart apache to get the changes.