Apache Virtual Host Setup on Debian Buster

The Apache web server can use virtual hosts (similar to server blocks in Nginx) to encapsulate configuration details and host more than one domain from a single server.

Prerequisites

  • You must have a regular, non-root user with sudo privileges configured on the server.
  • A Fully Qualified Domain Name (FQDN) pointing to the server.
  • Running an instance of Debian 10.
  • A good Internet connection.

Host Directory

Create an example/public_html directory under www to hold the web root for example.com. Use the -p flag to also create the example parent directory at the same time:

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

Assign ownership of the public_html directory to the Apache www-data user:

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

Permissions should be correct if the unmask value is unmodified, but to make sure:

sudo chmod -R 755 /var/www/example/public_html

HTML Index File

Create a sample index.html file under the example/public_html directory:

nano /var/www/example/public_html/index.html

Add the following sample HTML to the file:

<html>
    <head>
        <title>Welcome!</title>
    </head>
    <body>
        <h1>Success! The example virtual host is working!</h1>
    </body>
</html>

Save and close the file when you are finished.

Configuration Files

Apache by default has one host with a single webpage file index.html found in its webroot directory /var/www/html/.

The default configuration file for all hosts is at /etc/apache2/apache2.conf.

A virtual host can be define within apache2.conf or by creating a new configuration file for the virtual host and linking it to the sites-available and sites-enabled Apache directories.

Create apache.conf

Create a new apache.conf configuration file for the virtual host, in the app root directory:

sudo nano /var/www/example/apache.conf

With this content:

<VirtualHost *:80>

    # Host definitions.
    ServerName example.com
    ServerAlias www.example.com
    ServerAlias example.com
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/example/html

    # Directives for any `.htaccess` files.
    <Directory /var/www/example/html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    # Custom logs.
    ErrorLog /var/log/apache2/example_error.log
    CustomLog /var/log/apache2/example_access.log combined

</VirtualHost>

NOTE: Require all granted is new and required as of Apache 2.4.3. To increase security, Apache changed the default configuration to block all requests by default. All hosts must now explicitly grant permissions. See Apache VirtualHost 403 Forbidden.

Symlinks

Make a symlink in /etc/apache2/sites-available called example.conf which points to the apache.conf file:

sudo ln -s /var/www/example/apache.conf /etc/apache2/sites-available/example.conf

Now create a symlink in /etc/apache2/sites-enabled called example.conf which points to the example.conf file under sites-available:

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

Restart Apache:

sudo systemctl restart apache2