Sunday, 17 February 2013

Apache Virtual Hosts

I've been setting up a webserver recently and getting to the bottom of a few new Linux commands. I thought I'd use this blog to capture some of these commands to help myself in the future as well as others who may be trying similar things.

The first thing I have been doing is getting the most out of my VPS by setting up apache VirtualHosts which allows you to host multiple domains from one IP address.

To do this you need to modify the httpd_conf file. In this example I am going to imagine that I own three domains www.domain1.com, www.domain2.com and www.domain3.com. I am also going to use NamedHosts


  • sudo vi /etc/httpd/conf/httpd.conf
  • Go to the bottom of the file and add the following...


<VirtualHost*:80>  
   ServerName domain1.com  
   ServerAlias *.domain1.com  
   DocumentRoot /var/www/domain1/html  
   ErrorLog logs/domain1.com-error_log  
   CustomLog logs/domain1.com-access_log common  
 <VirtualHost>  






Lets break down exactly what is going on here

The opening line is saying grab any traffic on port 80 and check against this virtual host.

The next statement gives the server name that we want to route, in this case it is domain1.com. If the HTTP get request has a Host of domain1.com then we want to route the traffic. The ServerAlias field is acting as a sort of catch-all for subdomains, also routing these to the same location as domain1.com

    ServerName domain1.com
    ServerAlias *.domain1.com

The DocumentRoot directive indicates where the request should be routed to in order to find the site contents. In this case a directory within /var/www/domain1/. This is where you will create your website files such as index.html

    DocumentRoot /var/www/domain1/html

Finally we have a couple of directives to separate logs out for this particular domain into there own log files.

To configure domain2.com and domain3.com we just add extra vitual host definitions. The finished directives look as follows. One important point here is the ordering of the domains. The domain listed first becomes the default domain. If any unrecognised host traffic arrives at this apache server it will be routed to the defaul domain, in this example domain1. That is to say if I owned domain4.com and routed it to this server but I have yet to add a Virtual Host declaration, when someone tries to visit domain4.com it will be unmatched and revert to the default domain serving the contents of domain1.com.

 <VirtualHost*:80>  
   ServerName domain1.com  
   ServerAlias *.domain1.com  
   DocumentRoot /var/www/domain1/html  
   ErrorLog logs/domain1.com-error_log  
   CustomLog logs/domain1.com-access_log common  
 <VirtualHost>  
 <VirtualHost*:80>  
   ServerName domain2.com  
   ServerAlias *.domain2.com  
   DocumentRoot /var/www/domain2/html  
   ErrorLog logs/domain2.com-error_log  
   CustomLog logs/domain2.com-access_log common  
 <VirtualHost>  
 <VirtualHost*:80>  
   ServerName domain3.com  
   ServerAlias *.domain3.com  
   DocumentRoot /var/www/domain1/html  
   ErrorLog logs/domain3.com-error_log  
   CustomLog logs/domain3.com-access_log common  
 <VirtualHost>  

The next steps is to enable NameVirtualHost which is another parameter within the httpd.conf file. To do this simply find the following line in the file and uncomment the directive like so.

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

Once we are done we need to save the file and restart apache. First we will test that the configuration is valid by issuing this command

/usr/sbin/httpd -S

Then issue the following command in your terminal. 

sudo /etc/init.d/httpd -restart

Apache is configured so the final steps will be to update your DNS records for your domains. This will be specific to your domain provider but essentially you will need to add the following for each domains DNS settings.

  • An A record redirecting * to your Apache servers IP address.
  • A CNAME entry redirecting www.domain1.com to domain1.com (I needed to do this you may not.)
After all is said and done you will be hosting 3 separate websites from one VPS using one IP Address.