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.
- Provision your Linode. You need to select a data center and deploy a Linux distribution (Ubuntu for our purposes).
- Connect to your Linode with an SSH client.
-
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
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.
-
Disable the default Apache virtual host.
$ sudo a2dissite default
-
Navigate to your home directory.
$ cd ~
-
Create a folder to hold your websites.
$ mkdir public
-
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 ~
-
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.
-
Create a set of folders inside
~/public/
to store your website’s files, logs, and backups (replaceexample.com
with your domain name).$ mkdir -p public/example.com/{public,log,backup}
-
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.
-
Create the virtual host file for your website (replace
example.com
with your domain name andvi
with your editor of choice).$ sudo vi /etc/apache2/sites-available/example.com
-
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 andexample.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>
-
Save the changes to the virtual host configuration file and exit the editor.
-
Create a symbolic link to your new public directory (replace
example.com
with your domain name).$ sudo a2ensite example.com
-
Gracefully restart Apache to save the changes.
$ sudo service apache2 reload
- 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.