These days setting up a website is easy. We can simply buy a domain, get a web host or cloud service, put up a WordPress or other platforms, done. There are tons of tutorials on the internet, and I just happen to have one as well.

Often, after that initial setup, you might realize that you are not utilizing all the computing power of the cloud and the capabilities of your domain. You are not getting that many traffics on your website. In fact, for most of the time your server just sits there, not serving anyone. On the other hand, other than www.example.com, you are not using any of the unlimited subdomains like whatever.example.com that happens to come with your purchase of the example.com.

Now that you realize the potentials of what you already own, you can utilize them. One way to do that is setup another personal blog for yourself or a photo gallery for you and your love ones (There is only 1 love.example.com!). However, doing such thing often requires the use of virtual host or vhost for short. You don’t know how? I am here to help!

What is Virtual Host

host helps you to host website, or several websites on the same computer. I use it to host johnsonsu.com and blog.johnsonsu.com on the same AWS EC2 instance.

Apache vhost Setup for Ubuntu 14.04

Apache comes with vhost by default. In fact, apache is using vhost to host your current website. In Ubuntu 14.04, apache’s vhost related files are located in:

    /etc/apache2/site-available/

Inside this directory, you should see a 000-default.conf file. This is the default configuration file for apache vhost. The content should look something like this (with out the comments):

    <VirtualHost *:80>

        ServerAdmin [email protected]
        DocumentRoot /var/www

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

    </VirtualHost>

What this does is when someone connects to your server on port 80 (default port for HTTP), apache will handle that request with the things in side /var/www, which is usually your website’s root folder.

Now, we want to host more website on the server, say we a www.example.com and a blog.example.com. To save the hassle, we can simply duplicate the default configuration. We can run the command:

    sudo cp /etc/apache2/site-availabel/000-default.conf www.example.conf     #copy the default config file to a new file called www.example.conf
    sudo cp /etc/apache2/site-available/000-default.conf blog.example.conf     #copy the default config file to another new file called blog.example.conf

Now we have 2 new config file, let’s edit them. We want the www.example.conf to look something like this:

    <VirtualHost *:80>

        ServerAdmin [email protected]
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/wordpress  #You should change this to your main site's root folder

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

    </VirtualHost>

Whereas blog.example.conf should look like this:

    <VirtualHost *:80>

        ServerAdmin [email protected]
        ServerName blog.johnsonsu.com
        DocumentRoot /var/www/blog  #You should change this to your main site's root folder

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

    </VirtualHost>

Notice they are very similar. The only thing you need to make sure is the ServerName and DocumentRoot are correct.

Now you can go to your domain’s DNS manager. For example, if you purchased domain from godaddy, then go to godaddy.com, login, and edit your DNS. You would want to add a new CNAME record with name blog as an alias of example.com and point to the same server IP address. After that save the changes and wait for DNS to update, this can take a while.

Now if you go to blog.example.com, you should see whatever you put in /var/www/blog. Congratulations, you now have another website on your server, without paying any extra!

You can easily create a bunch of WordPress website for your hobby(guitar.example.com), your lover(love.example.com), your family members(family.example.com), even for your dog(you got it!).