Introduction

A Virtual Host allows you to run multiple websites from a single physical server or Virtual Private Server. There are two types of virtual hosts on Apache:

  • IP-Based Virtual Hosting – every individual website on the Apache Server uses a different, unique IP address.
  • Name-Based Virtual Hosts – enables you to add multiple domains using a single IP address.

This tutorial will show you how to set up Apache Virtual Hosts on an Ubuntu 18.04 system.

Set up Apache Virtual Hosts on Ubuntu Introductory Image

Prerequisites

How to Set Up a Name-Based Virtual Host

Name-based virtual hosting allows the client to report the hostname to the server, as an element of the HTTP header. This feature means that one machine can host multiple websites sharing the same IP address.

Visual of name based virtual hosting

Step 1: Create a Directory Structure

Each virtual host needs to have a directory for storing virtual host data. Create directories and a directory structure at the following location /var/www. In our example, we’ve created the phxnap.com directory, which is an example domain name.

Enter the following command and replace the example domain with your domain name:

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

Within the phxnap.com directory, we also created public_html. This directory is going to store website files for that domain.

Next, create a sample index.html page using nano or your favorite text editor:

nano /var/www/phxnap.com/public_html/index.html

For example, we used the following sample HTML:

<html>

<head>

<title>Welcome to phoenixNAP!</title>

</head>

<body>

<h1>Well done! Everything seems to be working!</h1>

</body>

</html>

Example of web page created in nano

Save and exit the file.

To prevent any permission issues modify the ownership of your document’s root directory to www-data. The chown command is useful in this instance, for example:

sudo chown –R www-data:www-data /var/www/phxnap.com

You have now successfully changed the directory ownership to the Apache User.

Step 2: Create a Virtual Host Configuration File

Apache Virtual Host configuration files are stored in the /etc/apache2/sites-available directory.

To create a basic configuration file for your Virtual Host. configuration file, enter the domain information in the following command:

sudo nano /etc/apache2/sites-available/phxnap.com.conf

Add the following configuration block to create a basic configuration file. This example uses the phxnap.com test domain. Make sure to enter the correct domain for your website:

<VirtualHost *:80>

ServerAdmin webmaster@phxnap.com

ServerName phxnap.com

ServerAlias www.phxnap.com

DocumentRoot /var/www/phxnap.com/public_html

ErrorLog ${APACHE_LOG_DIR}/phxnap.com-error.log

CustomLog ${APACHE_LOG_DIR}/phxnap.com-access.log combined

</VirtualHost>

Apache congif file showing server settings

  • ServerName – represents the domain
  • ServerAlias – represents all other domains such as subdomains
  • DocumentRoot – the directory used by Apache to serve domain files
  • ErrorLog, CustomLog – designates the log files location

There is no established format. However, naming your configuration files based on the domain name is a “best practice”.

Step 3: Enable Virtual Host Configuration Files

To enable the virtual host file, create a symbolic link from the virtual host file to the sites-enabled directory. Apache2 reads this file when staring.

Use the a2ensite helper to enable the virtual host file with the command:

sudo a2ensite phxnap.com

The output will appear as:

a2ensite helper to enable virtual host

Next, verify the configuration file syntax is correct using the command:

sudo apachectl configtest

The message in the terminal will confirm that the syntax is correct: “Syntax OK”

Restart Apache2 for the changes to be applied:

sudo systemctl restart apache2

The only thing left to do is to go to a web browser and access your website. Based on the index.html file we created earlier, the following message should appear:

Web page displaying virtual host sucessfully created

Step 4: Configure Firewall (Optional)

Modify your firewall settings to improve security by creating a rule to enable SSH on Ubuntu:

sudo ufw allow OpenSSH

Add the rules to allow access to Apache.

sudo ufw allow in “Apache Full”

Next, enable the firewall.

sudo ufw enable

If you see a message saying “Command may disrupt existing SSH connections.”  Press y. If the firewall is working properly, then you should see “Firewall is active and enabled on system startup.”

Conclusion

By following this tutorial, you have successfully created and configured an Apache Virtual Host on Ubuntu. You are now able to create multiple name-based virtual hosts, as well as run/host a large number of domains from a single server by using one IP address.


Next you should also read