How to set up Apache virtual hosts on Ubuntu 20.04

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 root directory of the site document (the directory containing the site files), create a separate security policy for each site, use different SSL certificates, and more.

This article describes how to set up Apache virtual hosts on Ubuntu 20.04.


Make sure you have completed the following requirements before continuing with the article:

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

Creating a directory structure

The document root is the directory where the website files for the 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

For each domain hosted on the server, the document root will be set to / var / www / / public_html.

Start by creating the root directory for the domain:

sudo mkdir -p /var/www/

We will also create an index.html file in the domain’s document root, which will be displayed when visiting the domain in a browser:


<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">
    <title>Добро пожаловать на</title>
    <h1>Успех! Главная страница!</h1>

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

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

Creating virtual hosts

On Ubuntu systems, the Apache Virtual Hosts configuration files are located in the / etc / apache2 / sites-available directory. They can be enabled by creating symlinks to the / etc / apache2 / sites-enabled directory that Apache reads on startup.

Open the text editor of your choice and create the following basic virtual host configuration file:


<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/

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

    ErrorLog ${APACHE_LOG_DIR}/
    CustomLog ${APACHE_LOG_DIR}/ combined

  • ServerName: The domain to match this virtual host configuration. This should be your domain name.
  • ServerAlias: All other domains or subdomains that must match this virtual host, for example the www subdomain.
  • DocumentRoot: Directory from which Apache will serve the domain files.
  • Options: This directive tells which server features are available in a specific directory.
    • -Indexes: Prevents directory listing.
    • FollowSymLinks: When this option is enabled, Apache will follow symbolic links.
  • AllowOverride: Specifies which directives declared in the .htaccess file can override configuration directives.
  • ErrorLog, CustomLog: Specifies the location for the log files.

You can name the configuration file whatever you like, but it is best 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

Another option is to manually create a symbolic link as shown below:

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

After that check the configuration for 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 make sure everything is working as expected, open in your browser and you will see the content of the index.html page:


You have learned how to create an Apache virtual host configuration to host multiple domains on a single Ubuntu server.

Repeat the steps above to create additional virtual hosts for all of your domains.

If you encounter any problem, feel free to leave comments.