Beginner Fundamentals

Virtual Hosts

Virtual hosts let a single Apache server host many websites, each with its own domain name and content. The most common type is name-based, where Apache picks the site by the requested hostname.

A Basic Virtual Host

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example

    <Directory /var/www/example>
        Require all granted
    </Directory>
</VirtualHost>

The ServerName is the main domain; ServerAlias adds extra names that map to the same site.

Hosting a Second Site

Just add another block with a different name and root:

<VirtualHost *:80>
    ServerName blog.example.com
    DocumentRoot /var/www/blog
</VirtualHost>

Enable the Site (Debian)

Save the file in sites-available/ and enable it:

sudo a2ensite example.conf
sudo systemctl reload apache2

Test Locally

Add an entry to /etc/hosts so the name resolves to your server while testing:

127.0.0.1 example.com blog.example.com

Apache reads the Host header on each request and serves the matching virtual host.