Atlantic.Net Blog

How to Set Up Apache Virtual Hosts on Ubuntu 20.04

Verified and Tested 03/10/21

Introduction

In this article, we will go over how to add additional virtual hosts to a Linux cloud server with Apache installed. Each virtual host handles a specific website or domain that will be hosted on the server, including sub-domains. This is referred to as named-based hosting because it allows multiple websites to utilize one set of resources, such as a single IP.

Prerequisites

This article assumes that you have already installed Apache and performed the basic configuration of it. If you have not done so, follow our how-to on this here.

Setting up Apache Virtual Hosts on Ubuntu 20.04

All sites/hosts are located in the following directory:

/etc/apache2/sites-available/

If you have just installed Apache, these are the only things you should see:

Directory: /etc/apache2/sites-available/

Create a virtual host configuration file using your preferred text editor. In this example, nano is used.

nano /etc/apache2/sites-available/virtualhost.example.conf

Add a basic configuration to this file and save:

Sample Virtual Host configuration

Some of the sections you may consider changing are:

<VirtualHost *:80>

This is the port that this virtual host will be accessible from. If we were to change 80 to 8080 then we would type the following URL to access the host: http:// virtualhost.example:8080

ServerName virtualhost.example
ServerAlias www.virtualhost.example

These values will indicate the domain that this virtual host will be representing and the alternate name for the host.

ServerAdmin [email protected]

Update this with your applicable server admin email address.

DocumentRoot /var/www/virtualhost.example/public_html

This will be the location of the site files for the specified domain.

Next enable the virtual host with the following command:

a2ensite virtualhost.example.conf

Your output should look like this:

a2ensite sample output

Lastly, just restart Apache as indicated by the prompt

service apache2 reload

Congratulations! You have now successfully Set Up Apache Virtual Hosts on Ubuntu 20.04. Thank you for following along and feel free to check back with us for further updates.

More About Atlantic.Net

Atlantic.Net offers world-class hosting solutions, including VPS hosting services.

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year