Cybersecurity Updates & Tools

Set Up Apache Virtual Hosts on Ubuntu 18.04: Complete Guide

Apache Virtual Hosts let you run multiple websites on a single server. Each site gets its own document root, its own error and access logs, and can have its own SSL certificate and separate security policy. This is the standard way to manage multi-domain hosting on an Apache server.

This guide shows you how to configure Apache Virtual Hosts on Ubuntu 18.04. The same steps work for Ubuntu 16.04 and any Ubuntu-based distribution. You can repeat this process for every additional domain you want to host.

Prerequisites:

  • A domain name pointing to your server’s public IP
  • Apache installed and running
  • Sudo access

Create the Document Root and Demo File

Apache serves files from a document root directory. Each virtual host needs its own. A clean convention is to store each site under /var/www/domain/public_html:

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

For testing, create a basic HTML file inside the document root. Save the following to /var/www/example.com/public_html/index.html:

html<!DOCTYPE html><html lang="en">  <head><title>Welcome to example.com</title></head>  <body><h1>Success! example.com home page!</h1></body></html>

When running commands as a sudo user, newly created files are owned by root. Change ownership to the Apache user so the web server can read and serve the files correctly:

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

Configure Apache Virtual Hosts on Ubuntu

Virtual host configuration files live in /etc/apache2/sites-available/. Create a config file for your domain:

bashsudo nano /etc/apache2/sites-available/example.com.conf

Paste the following configuration, replacing example.com with your actual domain name:

apache<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>

Here is what the key directives do:

  • ServerName and ServerAlias: the domain and www subdomain this virtual host answers for
  • DocumentRoot: the directory Apache serves files from for this domain
  • Options -Indexes: prevents Apache from showing a directory listing when no index file is present
  • AllowOverride All: lets .htaccess files override Apache config per directory, required by most CMS platforms like WordPress
  • ErrorLog and CustomLog: write logs to separate files for this domain, making debugging easier

Name the configuration file after your domain for easy management.

Enable, Test, and Restart Apache

Enable the new virtual host using the a2ensite helper:

bashsudo a2ensite example.com

This creates a symbolic link from sites-available to sites-enabled, which Apache reads on startup. You can also create the link manually if needed:

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

Before restarting, test the configuration for syntax errors:

bashsudo apachectl configtest
Syntax OK

If the output shows Syntax OK, restart Apache to apply the changes:

bashsudo systemctl restart apache2

Open http://example.com in your browser. You should see the test page you created earlier.

Your Apache Virtual Hosts on Ubuntu 18.04 are now active. Repeat the steps above for each additional domain you want to host on the same server. Leave a comment below if you run into any issues.