How To Set Up Apache Virtual Hosts on Ubuntu 20.04

Introduction

The Apache HTTP server is a popular open-source web server that offers flexibility, power, and widespread support for developers. Apache server configuration does not take place in a single monolithic file, but instead happens through a modular design where new files can be added and modified as needed. Within this modular design, you can create an individual site or domain called a virtual host.

Using virtual hosts, one Apache instance can serve multiple websites. Each domain or individual site that is configured using Apache will direct the visitor to a specific directory holding that site’s information. This is done without indicating to the visitor that the same server is also responsible for other sites. This scheme is expandable without any software limit as long as your server can handle the load.

In this guide, you will set up Apache virtual hosts on an Ubuntu 20.04 server. During this process, you’ll learn how to serve different content to different visitors depending on which domains they are requesting by creating two virtual host sites.

Prerequisites

Before you begin this tutorial, you will need:

  • An Ubuntu 20.04 server with a non-root user with sudo privileges. You can use our Initial Server Setup with Ubuntu 20.04 guide to set this up.
  • Apache installed on the server. You can learn how by completing steps 1-3 on our How To Install the Apache Web Server on Ubuntu 20.04 tutorial.

In order to successfully complete this tutorial, you will need two domains with:

  • In order to successfully complete this tutorial, you will need two domains with
  • An A record with your_domain pointing to your server’s public IP address.
  • An A record with www.your_domain pointing to your server’s public IP address. 

For other providers, please refer to their relevant product documentation.

Note: If you do not have domains available at this time, you can use test values locally on your computer. Step 6 of this tutorial will show you how to test and configure your test values. This will allow you to validate your configuration even though your content won’t be available to other visitors through the domain name.

Step 1 — Creating Default Pages for Each Virtual Host

With your directory structure in place, you can start focusing on each individual virtual host site and the content within that site. Start by creating an index.html page for your first site your_domain_1.

Open and create the index.html file with your preferred text editor. This example uses nano:

  1. nano /var/www/your_domain_1/public_html/index.html

Within this file, create an HTML file that indicates to visitors which site they are connected to:

/var/www/your_domain_1/public_html/index.html
<html>
  <head>
    <title>Welcome to your_domain_1!</title>
  </head>
  <body>
    <h1>Success! The your_domain_1 virtual host is working!</h1>
  </body>
</html>

To save and close the file in nano, start by pressing CTRL+X. Press Y when prompted to save the file, then press ENTER when you are finished to exit.

Next, copy this file to use as the base for your second site by typing:

  1. cp /var/www/your_domain_1/public_html/index.html /var/www/your_domain_2/public_html/index.html

Then open this new file and modify the relevant pieces of information using your text editor like before:

  1. nano /var/www/your_domain_2/public_html/index.html
/var/www/your_domain_2/public_html/index.html
<html>
  <head>
    <title>Welcome to your_domain_2!</title>
  </head>
  <body> <h1>Success! The your_domain_2 virtual host is working!</h1>
  </body>
</html>

Save and close this file. You now have one page for each site that you can use to test the virtual host configuration.

Step 2 — Creating New Virtual Host Files

Virtual host files are the files that specify the actual configuration of your virtual hosts and dictates how the Apache web server will respond to various domain requests.

Apache comes with a default virtual host file called 000-default.conf. You can copy this file to create virtual host files for each of your domains.

Copy the default configuration file over to the first domain:

  1. sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/your_domain_1.conf

Be aware that the default Ubuntu configuration requires that each virtual host file end in .conf.

Open the new file in your preferred text editor with root privileges:

  1. sudo nano /etc/apache2/sites-available/your_domain_1.conf

With comments removed, the file will be similar to this:

/etc/apache2/sites-available/your_domain_1.conf
<VirtualHost *:80>
  ...
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
   ...
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Within this file, customize the items for your first domain and add some additional directives. This virtual host section matches any requests that are made on port 80, the default HTTP port.

First, change the ServerAdmin directive to an email that the site administrator can receive emails through:

/etc/apache2/sites-available/your_domain_1.conf
ServerAdmin admin@your_domain_1

After this, add two additional directives. The first, called ServerName, establishes the base domain for the virtual host definition. The second, called ServerAlias, defines further names that should match as if they were the base name. This is useful for matching additional hosts you defined. For instance, if you set the ServerName directive to example.com you could define a ServerAlias to www.example.com, and both will point to this server’s IP address.

Add these two directives to your configuration file after the ServerAdmin line:

/etc/apache2/sites-available/your_domain_1.conf
<VirtualHost *:80>
  ...
    ServerAdmin admin@your_domain_1
    ServerName your_domain_1
    ServerAlias www.your_domain_1
    DocumentRoot /var/www/html
    ...
</VirtualHost>

Next, change your virtual host file location for the document root for this domain. Edit the DocumentRoot directive to point to the directory you created for this host:

/etc/apache2/sites-available/your_domain_1.conf
DocumentRoot /var/www/your_domain_1/public_html

Here is an example of the virtual host file with all of the adjustments made above:

/etc/apache2/sites-available/your_domain_1.conf
<VirtualHost *:80>
  ...
    ServerAdmin admin@your_domain_1
    ServerName your_domain_1
    ServerAlias www.your_domain_1
    DocumentRoot /var/www/your_domain_1/public_html
    ...
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    ...
</VirtualHost>

Save and close the file.

Create your second configuration file by copying over the file from your first virtual host site:

  1. sudo cp /etc/apache2/sites-available/your_domain_1.conf /etc/apache2/sites-available/your_domain_2.conf

Open the new file in your preferred editor:

  1. sudo nano /etc/apache2/sites-available/your_domain_2.conf

You now need to modify all of the pieces of information to reference your second domain. When you are finished, it should look like this:

/etc/apache2/sites-available/your_domain_2.conf
<VirtualHost *:80>
  ...
    ServerAdmin admin@your_domain_2
    ServerName your_domain_2
    ServerAlias www.your_domain_2
    DocumentRoot /var/www/your_domain_2/public_html
    ...
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    ...
</VirtualHost>

Save and close the file when you are finished.

Step 3 — Enabling the New Virtual Host Files

Now that you have created your virtual host files, you must enable them. Apache includes some tools that allow you to do this.

You’ll be using the a2ensite tool to enable each of your sites. If you would like to read more about this script, you can refer to the a2ensite documentation.

Use the following commands to enable your virtual host sites:

  1. sudo a2ensite your_domain_1.conf
  2. sudo a2ensite your_domain_2.conf

There will be output for both sites, similar to the example below, reminding you to reload your Apache server:

Output
Enabling site example.com.
To activate the new configuration, you need to run:
  systemctl reload apache2

Before reloading your server, disable the default site defined in 000-default.conf by using the a2dissite command:

  1. sudo a2dissite 000-default.conf
Output
Site 000-default disabled.
To activate the new configuration, you need to run:
  systemctl reload apache2

Next, test for configuration errors:

  1. sudo apache2ctl configtest

The should receive the following output:

Output
. . .
Syntax OK

When you are finished, restart Apache to make these changes take effect.

  1. sudo systemctl restart apache2
<pre class="prefixed command language-bash" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; font-family: monospace, mono
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

12 Useful “df” Commands to Check Disk Space in Linux

Useful df Command Examples This article explain a way to get the full information of Linux disk...

How to Disable SELinux

How to Disable SELinux You’ve setup a new system, or installed something new on your Linux...

How to set a Static IP in Ubuntu Linux

How to set a Static IP in Ubuntu LinuxOpen the terminal command line application or ssh into the...

Linux Check Disk Space Command To View System Disk Usage

Linux Check Disk Space Command To View System Disk Usage Linux command to check disk space...