How To Set Up Apache Virtual Hosts on Ubuntu

June 5, 2023 0 Comments
Introduction

This tutorial will walk you through setting up several domains and websites on an Ubuntu 18.04 server using Apache virtual hosts. You will gain knowledge on how to deliver various visitors with various types of material according to the domains they are asking during this procedure.

Please refer to How To Set Up Apache Virtual Hosts on Ubuntu 18.04 for a more thorough explanation of each step in this article.

Prerequisites

You must have access to the following on an Ubuntu 18.04 server in order to finish this tutorial:

On your server, a sudo user
Installing an Apache2 web server with sudo apt install apache2

Step 1 — Create the Directory Structure

In order to serve users with the site data, we need to establish a directory structure in our top-level Apache directory. The highlighted domain names below will be used as examples. These should be swapped out for your actual domain names.

sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/test.com/public_html

Step 2 — Grant Permissions

To allow our current non-root user to edit the files, we should now update the permissions.

sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/test.com/public_html

In order for pages to be served correctly, we’ll also make sure that read access is allowed to the general web directory and all of the files and directories it contains.

sudo chmod -R 755 /var/www

Step 3 — Create Demo Pages for Each Virtual Host

Now that we have some material to serve, let’s develop an example index.html page for each site. We can use nano as a text editor to open the index.html file for our first website.

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

Create a domain-specific HTML document in this file that looks something like this:

<html>
<head>
<title>Welcome to example.com!</title>
</head>
<body>
<h1>Success! The example.com virtual host is working!</h1>
</body>
</html>

Copy this file, save it, and then close it to serve as the foundation for our second website:

cp /var/www/example.com/public_html/index.html /var/www/test.com/public_html/index.html

<html>
<head>
<title>Welcome to Test.com!</title>
</head>
<body> <h1>Success! The test.com virtual host is working!</h1>
</body>
</html>

Additionally, save and shut this file.

Step 4 — Create New Virtual Host Files

We’ll take inspiration from Apache’s default virtual host file, 000-default.conf. To make a virtual host file for each of our domains, we’ll copy it across.

Create the First Virtual Host File

Copy the file for the first domain first:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

Open the new file in your editor of choice (we’ll be using nano below) as root:

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

This file will be modified for use with our domain. Change the underlined text below to suit your needs.

<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save the document now, then shut it.

Copy First Virtual Host and Customize for Second Domain

Now that we’ve established our first virtual host file, we can make our second one by duplicating it and making the necessary adjustments.

To begin, copy it:

sudo cp /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-available/test.com.conf

Open your editor and the new file with root privileges:

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

All of the information must now be changed to relate to your second domain. The completed document should like this, with the highlighted text being the information from your own pertinent site.

<VirtualHost *:80>
ServerAdmin [email protected]
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

When you’re done, save and shut the file.

Step 5 — Enable the New Virtual Host Files

We now need to enable our virtual host files, which we established before. To accomplish this, we’ll use the a2ensite tool.

sudo a2ensite example.com.conf
sudo a2ensite test.com.conf

The default site specified in 000-default.conf should then be disabled:

sudo a2dissite 000-default.conf

After you’re done, restart Apache for the modifications to take effect, and run systemctl status to ensure the restart was successful.

sudo systemctl restart apache2

You should now have two websites being served by your server.

Step 6 — Set Up Local Hosts File (Optional)

If you haven’t been testing this process with real domain names that you own, but rather with some example domains, you can check your progress by momentarily changing the hosts file on your local computer.

Type the next on a local Mac or Linux computer:

sudo nano /etc/hosts

You can find instructions on changing your hosts file for a local Windows PC here.

Your file should appear as follows using the domains mentioned in this article and your server IP in place of the text your_server_IP:

127.0.0.1 localhost
127.0.1.1 guest-desktop
your_server_IP example.com
your_server_IP test.com

Save the document, then exit. Any requests made to example.com and test.com on our PC will now be sent to our server.

Step 7 — Test your Results

Now that your virtual hosts are set up, you may test your configuration by visiting the domains you set up in your web browser:

http://example.com

You should see a page similar to this one:

You may view the file you made for your second site by visiting your second page.

http://test.com

Test.com

You’ve set up two virtual hosts on the same server if both of these websites function as planned.

Delete the lines you inserted if you modified the hosts file on your home computer.

Leave A Comment

To Top