How To Set Up Apache Virtual Hosts on Debian 9

Updated on

4 min read

Set Up Apache Virtual Hosts on Debian 9

In this tutorial, we will walk you through on how to set up Apache Virtual Hosts on Debian 9.

Apache Virtual Hosts allows you to host more than one domain on a single machine. When using virtual hosts, you can specify a different document root (the directory which contains the website files) for each domain or subdomain, create a separate security policy, use different SSL certificates and much more.

Although this tutorial is written for Debian 9 the same steps apply for all Debian based distributions.

Prerequisites

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

Create the Directory Structure

The document root is a 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 guide we’ll use the following directory structure:

/var/www/
├── domain1.com
│   └── public_html
├── domain2.com
│   └── public_html
├── domain3.com
│   └── public_html

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

Let’s start by creating the document root directory for our first domain, example.com:

sudo mkdir -p /var/www/example.com/public_html

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:

/var/www/example.com/public_html/index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to example.com</title>
  </head>
  <body>
    <h1>Success! example.com home page!</h1>
  </body>
</html>

We are running the commands as a sudo user and the newly created files and directories are owned by the root user.

To avoid any permission issues we’ll change the ownership of the domain document root directory and all files within that directory to the apache user (www-data) :

sudo chown -R www-data: /var/www/example.com

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.

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

/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
  • 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.

You can name the Virtual Host configuration file as you want but it is recommended to use the domain name as the name of the configuration file.

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

In Debian systems you can enable the virtual host by using a helper script named a2ensite:

sudo a2ensite example.com

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

sudo ln -s /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-enabled/

Once the configuration is enabled test if the syntax is correct by typing:

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 http://example.com in your favorite browser, and you will see something like this:

Conclusion

In this tutorial, you learned how to create an Apache Virtual Host configuration to host multiple domains on a single Debian server. You can repeat the same steps to create additional virtual hosts for your other domains.

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

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