Hosting Multiple Domains With Linode

I wanted to host a second website on my Linode recently. Rather than look up the process for doing so again when/if I decide to host a third website on the same Linode, I realized that I should just write up my own quick start guide for future reference. I am posting it here on my blog because I assume that it will be useful to someone else, too. (When in doubt, make it public.)

Note that although I am explicitly writing about how to add a website to a Linode running Ubuntu, you should be able to use most of these instructions on any Ubuntu server. In fact, I’m not even going to explain how to use Linode’s control panel here—I will only provide command-line instructions.

I essentially distilled the information in this blog post directly from the Configuring Name-based Virtual Hosts section of the Hosting a Website quick start guide in the Linode Library. The Linode documentation is fantastic, by the way—I highly recommend that you read it at least once before you start following the procedure that I’ve outlined below.

Getting Started

Your first step, if you haven’t done so already, is to read Linode’s Getting Started and Securing Your Server quick start guides. I’ve highlighted the key points from the aforementioned documentation below.

  1. Provision your Linode. You need to select a data center and deploy a Linux distribution (Ubuntu for our purposes).
  1. Boot your Linode.
  1. Connect to your Linode with an SSH client.
  1. Set your hostname.
  1. Set your timezone.
  1. Install software updates. I do this so often that I might as well post the commands I use here.

    $ sudo apt-get update
    $ sudo apt-get upgrade --show-upgraded
    
  1. Add a new user.
  1. Set up SSH key pair authentication.
  1. Disable SSH password authentication and root login.
  1. Set up a firewall.
  1. Set up Fail2Ban.
  1. Install and optimize Apache.

Preparing an Environment for Name-Based Virtual Hosts

You should only need to follow the steps in this section once. Skip to the next section if you have already installed your first name-based virtual host.

  1. Disable the default Apache virtual host.

    $ sudo a2dissite default
    
  2. Navigate to your home directory.

    $ cd ~
    
  3. Create a folder to hold your websites.

    $ mkdir public
    
  4. Set your home directory (but not all of the files in it!) to be readable and accessible to all users on the system.

    $ sudo chmod a+rx ~
    
  5. Set the public directory (and all of the files in it) to be readable and accessible to all users on the system.

    $ sudo chmod -R a+rx ~/public
    

Adding Website Data

Repeat the following steps for each name-based virtual host that has its own set of files and directories. Skip to the following section if you would simply like to add a new domain name as an alias for an existing website.

  1. Create a set of folders inside ~/public/ to store your website’s files, logs, and backups (replace example.com with your domain name).

    $ mkdir -p public/example.com/{public,log,backup}
    
  2. Upload or create files in the public subfolder that you just created (e.g. ~/public/example.com/public/).

Configuring Virtual Hosts

Repeat the following steps for each name-based virtual host.

  1. Create the virtual host file for your website (replace example.com with your domain name and vi with your editor of choice).

    $ sudo vi /etc/apache2/sites-available/example.com
    
  2. Create a configuration for your virtual host. Linode has created some basic settings (shown below) to get you started. You may copy and paste these settings in to the virtual host file you just created.

    Replace example_user with your username and example.com with your domain name.

    # domain: example.com
    # public: /home/example_user/public/example.com/
    
    <VirtualHost *:80>
      # Admin email, Server Name (domain name), and any aliases
      ServerAdmin webmaster@example.com
      ServerName  www.example.com
      ServerAlias example.com
    
      # Index file and Document Root (where the public files are located)
      DirectoryIndex index.html index.php
      DocumentRoot /home/example_user/public/example.com/public
    
      # Log file locations
      LogLevel warn
      ErrorLog  /home/example_user/public/example.com/log/error.log
      CustomLog /home/example_user/public/example.com/log/access.log combined
    </VirtualHost>
    
  3. Save the changes to the virtual host configuration file and exit the editor.

  4. Create a symbolic link to your new public directory (replace example.com with your domain name).

    $ sudo a2ensite example.com
    
  5. Gracefully restart Apache to save the changes.

    $ sudo service apache2 reload
    
  1. Add DNS records for your new domain, if you haven’t already.

Congratulations! You should now have a new name-based virtual host on your Linode.