Aug 10, 2012

How to serve multiple domains


How to serve multiple domains
Most people serve more than one domain on their Slice(s).
Whether for different domain names or different subdomains of the same domain, the procedure is the same.

Question
I am often asked by people how to use their Slice to serve multiple domains.
The question often surprises me as they may have setup their Slice, installed a web server (Apache, Nginx, etc) and even created a virtual host to serve their main domain.
Don't get me wrong, the question itself is good (I like questions as it makes me feel useful!), but the answer is always so simple:
Create another vhost.
Outline
There may not be a great deal I can add to the answer but let me outline the process of setting up a Slice and creating a virtual host (I won't go into any details of the installation and creation process - please see the relevant articles for detailed help).
When your Slice is first created it is a minimal Linux install (it doesn't matter what OS you choose).
You SSH into the Slice and update and secure it.
Then you install your preferred web server (Apache, Nginx, Litespeed, etc).
Then the detailed stuff begins. It doesn't matter if your site is PHP based or Rails based or something else entirely. You install the language and framework basics (say Ruby and rubygems or mod_php and so on).
Once that is all done, you come to the part that allows you to server your site: creating virtual hosts.
Procedure
Greatly simplified the procedure for serving a website is as follows:
A browser send a request to your Slice IP asking for the contents of 'domain.com' (your domain name).
Your web server jumps into action and says 'yes! I have something for you'. The web server does its 'thing' and serves up an http representation of your site which is sent to the browser.
The browser then translates the http and parses it to a human form of the web site (something like this one).
All jolly good but how does your web server know what to send?
Virtual Hosts
This is where name based virtual hosts come in.
One of the first lines in any virtual host contains the domain name that is related to the vhost.
Something like this for Apache:

  ServerName  domain1.com
  ServerAlias www.domain1.com
and something like this for Nginx:
server {

  server_name  www.domain1.com;
  rewrite ^/(.*) http://domain1.com/$1 permanent;
Each one starts slightly differently but the same principle applies - that particular virtual host will respond to queries for 'domain1.com' and 'www.domain1.com'.
Multiple domains
So, to serve different content for different domains is as simple as adding another virtual host.
Let's say you have a subdomain called 'blog.domain1.com' serving a blog (I know, shocking originality!).
The basic creation process would be to create a folder in your public_html folder with the relevant files (let's say a Wordpress install).
A virtual host would be created with the server_name or ServerName as 'blog.domain1.com' which would be configured to point to the blog files and folders in your public_html folder.
Done.
Language and frameworks
It doesn't matter what language or framework your domain uses.
To serve multiple Rails applications for example requires the same setup for each application.
Of course, there would be some differences such as port numbers for the mongrel or thin instances. Virtual hosts can't share ports.
For example, you may create a mongrel cluster to run from port 8000 - 8002 for one domain and another running from 8010 - 8012 for your blog and so on.