Hosting Multiple Sites on the Same Server Using Apache2

When I upgraded to my new server (Lumpy Gravy) and migrated my own site from my trusty HP DL380 to the new box, I took the opportunity to get better acquainted with one of the cornerstones on the internet, a piece of software you rarely ‘see’ but operates behind the scenes not just of this site but a full 59% of the entire internet – the Apache web server.

Apache2 comes as part of the ‘LAMP’ packages you are given the option to install when loading Ubuntu Server, and by forwarding port 80 to your machine you can start hosting web content quickly and easily.

But what if you wanted to host multiple websites from the same server? This is something I’ve had to do for the first time this week, and it’s all possible with a few simple configuration changes to Apache.

Apache (very) Basics

Before we start the tutorial, here are some of the commands, concepts and folders you need to be aware of:

  • ‘/etc/init.d/apache2’ – The path to the Apache2 web server
  • ‘/etc/apache2’ – The Apache2 configuration folder
  • ‘sites-available’ – Create your site configuration files in this folder
  • ‘sites-enabled’ – Folder containing symlinks to the currently enabled available sites
  • ‘a2ensite’ – Utility to enable sites
  • ‘a2dissite’ – Utility to disable sites
  • Virtual Hosts – These are Apache-speak for each of the sites you wish to run on a single server. Each site is a new ‘virtual host’.

In a virgin Apache configuration, your /etc/apache2/sites-available folder will contain one or two default configuration files to get you started. These will point to your operating system’s default web path, which will contain a basic ‘index.html’ file for you to test that the server is configured correctly. (On Debian distributions, the default path is /var/www/).

If you installed Apache with your OS, it will be configured to start automatically. On your router, you need to forward port 80 to your server – this is where Apache will be listening.

I’m also going to assume that you’ve bought more than one domain name (otherwise, why would you be reading this?!) and that you’ve pointed your domains to your external IP address via your domain registrar.

Preparing Apache

To begin with, we need to disable the default site. If you look in your ‘sites-enabled’ folder, you should see a symlink to ‘default’, or sometimes ‘000-default’. To disable it, we use ‘a2dissite’. You need root privileges for almost everything in this tutorial, so it might be worthwhile just switching to root with ‘sudo -s’ to save some typing. At the command prompt:

$ sudo a2dissite 000-default

Apache will prompt you to reload the configuration files to enact the change:

$ sudo /etc/init.d/apache2 reload

Apache will reload its configuration files, this time with no sites enabled. You can check this by listing the contents of the ‘sites-enabled’ directory with ‘ls‘.

We’re going to start by getting just one site up and running, and then adding more in turn when we know that our configuration is working. I spent hours troubleshooting when a single line of one config file knocked all sites offline! Checking your sites are working as you progress is a must.

Writing Apache Configuration Files

Each site hosted using Apache needs a configuration file. These files need to be created in the ‘sites-available’ folder and can be created using your favourite text editor (mine is, of course, the wonderful GNU nano). The only rule here is that you need to have root access to write files to this directory, and anything else in the /etc/ folder.

For the example, we’re going to pretend that we want to host ‘’.

Create a new configuration file in /etc/apache2/sites-available/ called ‘’. You can call the config file whatever you want, but do yourself a favour here and be organised.

$ sudo nano

Here’s our Apache configuration file for – let’s go through it line by line.

The # symbols are comments: those lines are just there for reference.

ServerAdmin: This is what Apache will report as the email address of the admin of your site.

ServerName: The name of the domain this website will respond to.

ServerAlias: The domain you list as an alias will also resolve to your website (useful if someone misses www., or if you want to add any subdomains)

DocumentRoot: Here, you need to write the path to the folder on your local machine that contains your website. In this case, it’s ‘mywebsite’ in Randy’s home directory.

Enabling Your New Site

Now it’s time to actually enable your site. At the command prompt:

$ sudo a2ensite

Apache will now configure itself to read from your new configuration file. This will create a symlink in ‘sites-enabled’ to the relevant config file which Apache can now read from. Now, we need to reload the configuration files, and for good measure we’re going to restart Apache as well. The program itself won’t tell you to do this, but this will rule out any problems you might have reloading the config files. At the prompt:

$ sudo /etc/init.d/apache2 reload

$ sudo /etc/init.d/apache2 restart

When the server restarts, fire up a web browser and check your handiwork. With a bit of luck and a fair wind you should now be able to see your site from any browser – you’re online!

Adding More Sites

Using our original site as a template, we can now add as many sites as we please. Let’s add a second site,, to check out how to do this.

First, make copy the original configuration file in the same folder but with the name of the new site:

# sudo cp

Now, edit the new file, updating the original entries with the appropriate domain and content path for the new site:

# sudo nano

Note the new ServerAdmin, ServerName and Alias information, as well as the local path where Stan’s website is kept.

Now, we can enable the site, reload the configuration files and restart the Apache server for good measure:

$ sudo a2ensite

$ sudo /etc/init.d/apache2 reload

$ sudo /etc/init.d/apache2 restart


Using these methods you can point as many domains to your solitary server as you please. All you need to remember is to bear in mind how much extra bandwidth and disk space you require for each site, as these requirements will mount up pretty quickly the more you choose to host on the same pipe and server.

There are more to come, but as of this week Lumpy Gravy plays host not only to but also to the design team of Warden & Ferris – check them out at

2 Responses to “Hosting Multiple Sites on the Same Server Using Apache2”

  1. […] my last post we covered how to host multiple websites from the same server. This is really useful if you have […]

  2. […] the more basic configurations detailed in previous tutorials, you may occasionally find odd results being returned to your clients. For example, if […]

Leave a Reply