Read in 3 minutes

last updated 

How To Set Up Apache Virtual Hosts on Ubuntu 18.04

In this tutorial, we’ll provide a step by step instructions about how to set up Apache Virtual Hosts on Ubuntu 18.04.

Apache Virtual Hosts allows you to run more than one website on a single machine. With Virtual Hosts, you can specify the site document root (the directory which contains the website files), create a separate security policy for each site, use different SSL certificates and much more.

Although this tutorial is written for Ubuntu 18.04 Bionic Beaver the same steps can be used for Ubuntu 16.04 Xenial Xerus.


Make sure that you have met the following prerequisites before continuing with this tutorial:

Create the Directory Structure

Document root is the directory where the website files for a domain name are stored and served in response to requests. You can set the document root to any location you want but in this guide we will use the following directory structure:

│   └── public_html
│   └── public_html
│   └── public_html

Basically we will create a separate directory for each domain we want to host on our server inside the /var/www directory. Within each of these directories, we will create a public_html directory that will store the domain website files.

Let’s create the root directory for the domain:

sudo mkdir -p /var/www/

For testing purposes we will also create an index.html file inside the domain document root directory.

Open your editor and create the demo file:

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">
    <title>Welcome to</title>
    <h1>Success! home page!</h1>

In this guide, we are running the commands as sudo user and the newly created files and directories are owned by the root user.

To avoid any permission issues we can change the ownership of the domain document root directory to the apache user (www-data) :

sudo chown -R www-data: /var/www/

Create a Virtual Hosts

By default on Ubuntu systems, Apache Virtual Hosts configuration files are stored in /etc/apache2/sites-available directory and can be enabled by creating symbolic links to the /etc/apache2/sites-enabled/ directory.

Open your editor of choice and create the following basic Virtual Host configuration file:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/
    ErrorLog ${APACHE_LOG_DIR}/
    CustomLog ${APACHE_LOG_DIR}/ combined

You can name the configuration file as you like but the best practice is to use the domain name as the name of the virtual host configuration file.

To enable the new virtual host file we need to create a symbolic link from the virtual host file to the sites-enabled directory, which is read by apache2 during startup.

The easiest way to enable the virtual host is by using the a2ensite helper:

sudo a2ensite

The other option is to manually create a symlink as shown below:

sudo ln -s /etc/apache2/sites-available/ /etc/apache2/sites-enabled/

Once the configuration is enabled test if the syntax is correct with:

apachectl configtest

If there are no errors your will see the following output:

Syntax OK

In order for the changes to take effect, restart the apache2 service with:

sudo systemctl restart apache2

Finally to verify that everything is working as expected, open in your browser of choice, and you will see something like this:


You have learned how to create an apache virtual host configuration to host multiple domains on a single Ubuntu server. You can repeat the steps we outlined above and create additional virtual hosts for all your domains.

If you are facing any problem, feel free to leave a comment.

This post is a part of the how-to-install-lamp-stack-on-ubuntu-18-04 series.
Other posts in this series:

How To Set Up Apache Virtual Hosts on Ubuntu 18.04