How to Set Up Apache Virtual Hosts on Debian 10

Published on

4 min read

Set Up Apache Virtual Hosts on Debian 10

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 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 a Debian 10 server.


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

Create the Directory Structure

The document root is the directory in which the website files for a domain name are stored and served in response to requests. The document root can be set to any location you want.

We will use the following directory structure:

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

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

Let’s start by creating the document root directory for the first 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.

Open your favorite text editor, create a new file and paste the following into it:

<!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/

Create a Virtual Hosts

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

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

<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 as well, usually the www subdomain.
  • DocumentRoot: The directory from which Apache will serve the domain files.
  • Options: This 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.
  • ErrorLog, CustomLog: Specifies the location for log files.

Edit the file according to your needs and save it.

You can name the configuration file as you like. 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 the configuration is enabled test if the syntax is correct:

sudo apachectl configtest

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

Syntax OK

Restart the apache2 service for the changes to take effect:

sudo systemctl restart apache2

To verify that everything works as expected, open in your favorite browser, and you will see something like this:


In this tutorial, we have shown you how to create an Apache virtual host configuration on Debian 10, Buster. You can repeat the steps we outlined above and create additional virtual hosts for all your domains.

If you want to secure your website with an SSL certificate, you can generate and install a free Letsencrypt SSL certificate .

Feel free to leave a comment if you have any questions.

This post is a part of the How to Install LAMP Stack on Debian 10 series.
Other posts in this series: