The term “Virtual Hosting” refers to the hosting of many domains on a single server. In Linux-based systems such as Ubuntu 22.04, a Virtual Host is a configuration directive in Apache that permits you to operate several websites on a single server.

 

What is a virtual host?

A virtual host allows you to run more than one website on a single web server by using separate configuration files. This means you can configure each site with its own domain name, IP address, and other settings. You can also make changes to the same configuration file for multiple sites at once.

Prerequisites

Before you begin this tutorial, you will need:

  1. 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.
  2. 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.

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.

If you are using DigitalOcean, you can learn how to set up domains by following our product documentation, How to Add Domains.

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 the Directory Structure

The first step is to create a directory structure that will hold the site data that you will be serving to visitors.

Your document root, the top-level directory that Apache looks at to find content to serve, will be set to individual directories under the /var/www directory. You will create a directory here for each of the virtual hosts.

Within each of these directories, you will create a public_html directory. The public_html directory contains the content that will be served to your visitors. The parent directories, named here as your_domain_1 and your_domain_2, will hold the scripts and application code to support the web content.

Use these commands, with your own domain names, to create your directories:

sudo mkdir -p /var/www/your_domain_1/public_html

sudo mkdir -p /var/www/your_domain_2/public_html

Be sure to replace your_domain_1 and your_domain_2 with your own respective domains. For example, if one of your domains was example.com you would create this directory structure:

/var/www/example.com/public_html.

Step 2 — Granting Permissions

You’ve created the directory structure for your files, but they are owned by the root user. If you want your regular user to be able to modify files in these web directories, you can change the ownership with these commands:

sudo chown -R $USER:$USER /var/www/your_domain_1/public_html

sudo chown -R $USER:$USER /var/www/your_domain_2/public_html

The $USER variable will take the value of the user you are currently logged in as when you press ENTER. By doing this, the regular user now owns the public_html subdirectories where you will be storing your content.

You should also modify your permissions to ensure that read access is permitted to the general web directory and all of the files and folders it contains so that the pages can be served correctly:

$ sudo chmod -R 755 /var/www

Your web server now has the permissions it needs to serve content, and your user should be able to create content within the necessary folders. The next step is to create content for your virtual host sites.

Step 3 — Creating Default Pages for Each Virtual Host

We’ll also create an index.html file inside the domain document root directory that will be shown when you visit the domain in your browser

:/var/www/domain1.com/public_html/index.html

Since the commands above are executed as a sudo user, the newly created files and directories are owned by root. To avoid any permission issues change the ownership of the domain document root directory and all files within the directory to the apache user (www-data) :

Step 4 — Creating a Virtual Hosts

On Ubuntu systems, Apache Virtual Hosts configuration files are located in /etc/apache2/sites-available directory. They can be enabled by creating symbolic links to the /etc/apache2/sites-enabled directory, which Apache read during the startup.

On Ubuntu systems, Apache Virtual Hosts configuration files are located in /etc/apache2/sites-available directory. They can be enabled by creating symbolic links to the /etc/apache2/sites-enabled directory, which Apache read during the startup.

Open your text editor of choice and create the following basic Virtual Host configuration file:

/etc/apache2/sites-available/domain1.com.conf

<VirtualHost *:80>

    ServerName domain1.com

    ServerAlias www.domain1.com

    ServerAdmin webmaster@domain1.com

    DocumentRoot /var/www/domain1.com/public_html

    <Directory /var/www/domain1.com/public_html>

        Options -Indexes +FollowSymLinks

        AllowOverride All

    </Directory>

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

    CustomLog ${

ServerName: The domain that should match for this virtual host configuration. This should be your domain name.

ServerAlias: All other domains or subdomains that should match for this virtual host such as the www subdomain.

DocumentRoot: The directory from which Apache will serve the domain files.

Options: This directive controls which server features are available in a specific directory.

-Indexes: Prevents directory listings.

FollowSymLinks: When this option is enabled, Apache will follow the symbolic links.

AllowOverride: Specifies which directives declared in the .htaccess file can override the configuration directives.

ErrorLog, CustomLog: Specifies the location for log files.

You can name the configuration file as you like, but the best practice is to use the domain name as the name of the virtual host configuration file. To enable the new virtual host file, use the a2ensite helper script which creates a symbolic link from the virtual host file to the sites-enabled directory:

sudo a2ensite domain1.com

Once done, test the configuration for any syntax errors with:

sudo apachectl configtest

Step 6 — (Optional) Setting Up Local Hosts File

If you haven’t been using actual domain names that you own to test this procedure, and have been using example domains instead, you can still test the functionality of your virtual host sites by temporarily modifying the hosts file on your local computer. This will intercept any requests for the domains that you configured and point them to your Virtual Private Server (VPS), just as the DNS system would do if you were using registered domains. This will only work from your local computer and is only for testing purposes.

Make sure you are operating on your local computer for these steps and not your VPS server. You will need to know the computer’s administrative password or otherwise be a member of the administrative group.

If you are on a Mac or Linux computer, edit your local file with administrative privileges by typing:

sudo nano /etc/hosts

If you are on a Windows machine, open the Command Prompt and type:

notepad %windir%\system32\drivers\etc\hosts

The details that you need to add are the public IP address of your server, followed by the domain you want to use to reach that server. Using the domains used in this guide, and replacing your server IP for the your_server_IP text, your file should look like this:

127.0.0.1   localhost

127.0.1.1   guest-desktop

your_server_IP your_domain_1

your_server_IP your_domain_2

This will direct any requests for your two domains on your computer and send them to your server at the designated IP address.

Save and close the file.

If there are no errors, you will see the following output:

Syntax OK

Restart the Apache service for the changes to take effect:

sudo systemctl restart apache2

Finally to verify that everything is working as expected, open http://domain1.com in your browser, and you will see the content of the index.html page:

Conclusion

You have learned how to create an apache virtual host configuration to host multiple domains on a single Ubuntu server.

Repeat the steps we outlined above to create additional virtual hosts for all your domains.

Leave a Reply

Your email address will not be published. Required fields are marked *