Set Up Apache Virtual Hosts on Ubuntu 20.04

3 min read

A Virtual Host is an Apache configuration directive that allows you to run more than one website on a single server. With Virtual Hosts, you can specify the site document root (the directory containing the website files), create a separate security policy for each site, use different SSL certificates, and much more.

This article describes how to set up Apache Virtual Hosts on Ubuntu 20.04.


Ensure that you have met the following requirements before continuing with the guide:

  • One or more domain names pointing to your public server IP.
  • Apache installed on your Ubuntu system.
  • You are logged in as root or user with sudo privileges.

Creating the Directory Structure

The 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, in this example, we will use the following directory structure:

│   └── public_html
│   └── public_html

Each domain hosted on the server will have its document root set to /var/www/<domain_name>/public_html.

Start by creating the root directory for the domain:

sudo mkdir -p /var/www/

We’ll also create an index.html file inside the domain document root directory that will be shown when you visit the domain in your browser:/var/www/

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

Since the commands above are executed as a sudo user, the newly created files and directories are owned by root. To avoid any permission issues change the ownership of the domain document root directory and all files within the directory to the apache user (www-data) :

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

Creating a Virtual Hosts

On Ubuntu systems, Apache Virtual Hosts configuration files are located in /etc/apache2/sites-available directory. They can be enabled by creating symbolic links to the /etc/apache2/sites-enabled directory, which Apache read during the startup.

Open your text editor of choice and create the following basic Virtual Host configuration file:/etc/apache2/sites-available/

<VirtualHost *:80>
    DocumentRoot /var/www/

    <Directory /var/www/>
        Options -Indexes +FollowSymLinks
        AllowOverride All

    ErrorLog ${APACHE_LOG_DIR}/
    CustomLog ${APACHE_LOG_DIR}/ combined
  • ServerName: The domain that should match for this virtual host configuration. This should be your domain name.
  • ServerAlias: All other domains or subdomains that should match for this virtual host such as the www subdomain.
  • DocumentRoot: The directory from which Apache will serve the domain files.
  • OptionsThis directive controls which server features are available in a specific directory.
    • -Indexes: Prevents directory listings.
    • FollowSymLinks: When this option is enabled, Apache will follow the symbolic links.
  • AllowOverride: Specifies which directives declared in the .htaccess file can override the configuration directives.
  • ErrorLogCustomLog: Specifies the location for log files.

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, use the a2ensite helper script which creates a symbolic link from the virtual host file to the sites-enabled directory:

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 done, test the configuration for any syntax errors with:

sudo apachectl configtest

If there are no errors, you will see the following output:

Syntax OK

Restart the Apache service for the changes to take effect:

sudo systemctl restart apache2

Finally to verify that everything is working as expected, open in your browser, and you will see the content of the index.html page:

We want to hear your thoughts about this. Is there anything in particular that you love about it? What can we do to improve our services and experience? Leave a comment below or open a ticket on our helpdesk and we’ll personally review all suggestions and feedback. 

About Servercheap.NET

Since it was founded in 2015, Servercheap has always strived to provide its clients with enterprise-level performance at an unbeatable cost. Servercheap offers a wide range of customizable hybrid and virtual private server hosting services. All Servercheap clients enjoy a 99.9% uptime SLA and 24/7 rapid response support team.
At Servercheap, our core directive has always been to provide our clients with the best services and infrastructure possible, whether you’re hosting a game server, a high-intensity database, a development environment, or anything in-between.
For more information, visit

Leave a Reply

Your email address will not be published. Required fields are marked *