Configuring Apache 2.0.X - Virtual Hosting
February 5th, 2008
General
What is virtual hosting? Well, as we discussed in setting up dns and how dns works, you might be wondering that if every domain and subdomain has it’s own ip address then, wouldn’t we run out of ips? Yes. Of course! But, the cool thing about virtual hosting is that you can place multiple domains on the same ip. The only exception to this is SSL which requires it’s own ip address for each certificate. So, how do you setup virtual hosting? First thing, make sure all your domains or subdomains point to the same ip address in your dns settings.
So for example if your ip was 127.0.0.1 then: tech.mydomain.com , www.mydomain.com and blog.mydomain.com should all point 127.0.0.1 .
Next, let’s go to your apache configuration (httpd.conf) file.
In httpd.conf let’s go to the bottom of the file an add the following,
NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster@mydomain.com
DocumentRoot c:/home/webpub/tech.mydomain.com/
ServerName tech.mydomain.com
ErrorLog c:/home/logs/tech.mydomain.com-error.log
CustomLog c:/home/logs/tech.mydomain.com-access.log common
</VirtualHost><VirtualHost *>
ServerAdmin webmaster@mydomain.com
DocumentRoot c:/home/webpub/blog.mydomain.com/
ServerName blog.mydomain.com
ErrorLog c:/home/logs/blog.mydomain.com-error.log
CustomLog c:/home/logs/blog.mydomain.com-access.log common
</VirtualHost><VirtualHost *>
ServerAdmin webmaster@mydomain.com
DocumentRoot c:/home/webpub/www.mydomain.com/
ServerName mydomain.com
ServerAlias *.mydomain.com
ErrorLog c:/home/logs/www.mydomain.com-error.log
CustomLog c:/home/logs/www.mydomain.com-access.log common
</VirtualHost>
Let’s review over what we did.
NameVirtualHost *
This tells us we will run virtual hosts on all ips and ports.
<VirtualHost *>
ServerAdmin webmaster@mydomain.com
DocumentRoot c:/home/webpub/tech.mydomain.com/
ServerName tech.mydomain.com
ErrorLog c:/home/logs/tech.mydomain.com-error.log
CustomLog c:/home/logs/tech.mydomain.com-access.log common
</VirtualHost>
This snippet tells apache to setup a virtual host that answers to the name tech.mydomain.com with the path for the documents being c:/home/webpub/tech.mydomain.com/. If you look at the end of the configuration changes you will see an additional parameter for the virtual host mydomain.com.
The parameter ServerAlias allows you to assign domain aliases so that multiple domains or subdomainscan point to the same virtual host setup. So, for example, by placing this snippet last:
<VirtualHost *>
ServerAdmin webmaster@mydomain.com
DocumentRoot c:/home/webpub/www.mydomain.com/
ServerName mydomain.com
ServerAlias *.mydomain.com
ErrorLog c:/home/logs/www.mydomain.com-error.log
CustomLog c:/home/logs/www.mydomain.com-access.log common
</VirtualHost>
This will tell apache that after directing people to blog.mydomain.com or tech.mydomain.com, everything else should be directed to www.mydomain.com. In this case the * is a wildcard. You could also setup ServerAlias to be ServerAlias *.mydomain.com *.myotherdomain.com which would allow anything from either domain to use the same document root c:/home/webpub/www.mydomain.com/ while tech and blog use a different one.
As you can see, this is a very powerful tool for setting up large numbers of websites, each with their own path on your system.
