How to set up Apache Virtual Hosts on Debian 9.4 and Ubuntu 16.04

Created by Jordy Leffers at 06-12-2017 11:05:35 +0100

Apache's Virtual Hosts allow us to host several websites on the same server, saving us the trouble of acquiring a whole new server for a second website while there's enough disc space anyway. This way your domain names will direct to the same IP address, after which the virtual host seamlessly redirects the web browser to the correct web page.

Before we begin this guide it's important to note that before installing the virtual host, we must first have Apache installed and configured as we have in How to setup Linux, Apache, MySQL & PHP (LAMP) on Debian 9.4. You don't need to go through all steps in that tutorial, just the Apache installation.

This tutorial is based on cloud containers created on the page. This means that by default you are the root user, so all of the command below don't make use of sudo. If however, you're not the root user on your system, you'll have to add "sudo" in front of the commands found in this guide below.

For this tutorial, we're going to need  at least two domains for us to configure the virtual hosts to host.

Creating our directory structure

The first step to setting up several virtual hosts on our server, is to create our directory structure in which we will place the files belonging to our different domain names. We made these directories using the following commands, the domain names should be substituted for your own of course. You can add as many as you'd like.

mkdir -p /var/www/

mkdir -p /var/www/

mkdir -p /var/www/

mkdir -p /var/www/

The next step is to modify the directories' permissions to make sure that the general web directory has read acces so the pages can be served correctly.

chmod -R 755 /var/www

Adding default pages to each virtual host

In order for us to quickly be able to check whether every domain directs us to the correct page, we can create a simple html file in each of the domain directories that prints a string with the related domain name in it:

nano /var/www/

In this file, place some html to create a default web page:

Repeat this process for your other domains, and move on to the next step.

Create the virtual host files

Apache uses virtual host files to determine to which web page the webbrowser will be directed in response to different domain requests.

We can find these files in /etc/apache2/sites-available. Apache comes with a default virtual host file (000-default.conf), we can use this as a template to create virtual host files for our own domains by copying and renaming it.

cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/

Now that a copy of the template is created with the right name, edit it:

nano /etc/apache2/sites-available/

The file will look something like this:

First we're going to want to change the ServerAdmin to the site administrator's e-mail address. 


Next we'll add ServerName and ServerAlias, these establish the base domain and further domain names that direct to the same web page directory, provided that both these domain names point to the server's IP.



Also we will want to change the location of DocumentRoot, to the directory we created for this web page.

DocumentRoot /var/www/

Now the file should contain your equivalent of this information:

Repeat this process for all your domain names, before moving on to the next step: Enabling the virtual host files.

Enabling the virtual host files

Now we have configured our virtual host files, all that's left to to is to enable them. For this we can use an Apache tool (a2ensite):

sudo a2ensite (Do this for all your domains)

The output should look like this:

Enabling site

To activate the new configuration, you need to run:

systemctl reload apache2

Disable the default enabled site:

sudo a2dissite 000-default.conf

Finally, restart Apache:

sudo systemctl restart apache2

Congratulations, you've now configured multiple sites on one server!


Comments are turned off.