How to setup name-based virtual host on Ubuntu server.

I’m moving my site from fatcow.com to linode.com because I setup VPS on linode so I’ll have more control of it. The first step is to setup name-based virtual host so that I can link my sub-domains to my “www” sub-directories.

  1. Create DNS records for domains and sub-domains. The IP address of sub-domains can be the same as your main domain.

  2. Now the world know how to resolve your domain name to an IP address, next step is to create name-based virtual host on Ubuntu server so that we can host different sites on different sub-domains. I want to create 2 sites. One is my main domain which host my blog www.ycshao.com, another one is for testing purpose image.ycshao.com.

  3. Enable name-based virtual host on Ubuntu:

    1
    $ vim /etc/apache2/ports.conf

    and add following line to the end if it’s not there

    1
    NameVirtualHost *:80
  4. There are two folders in /etc/apache2, which are sites-available and sites-enabled. Usually sites-enabled folder will have soft link to files in sites-available folder. The folder names are pretty obvious, “available” means available sites and “enabled” means  sites which can be accessed. Let’s start from scratch.

    1
    2
    3
    $ cd /etc/apache2/sites-available # Go to directory
    $ cp default wordpress # Make a copy of default setting file
    $ vim wordpress

    and modify first few lines to

    1
    2
    3
    4
    5
    6
    7
    <VirtualHost *:80>
    ServerAdmin ycshao0402@gmail.com
    ServerName www.ycshao.com
    ServerAlias ycshao.com
    DocumentRoot /var/www/wordpress
    ...
    <virtualHost>

    The most important things are your “ServerName” corresponding to url you want to set and “DocumentRoot” corresponding to which directory this url points to. Then make a soft link to enable this site:

    1
    2
    $ ln -s /etc/apache2/sites-available/wordpress /etc/apache2/sites-enabled/wordpress
    $ service apache2 restart
  5. Now you should be able to access www.ycshao.com and it should go to my wordpress blog sitting in /var/www/wordpress.

  6. Now repeat step 4 for image.ycshao.com and other sites you want to configure.