How To Set Up Apache Virtual Hosts on Ubuntu 18.04 [Quickstart]

Introduction

This tutorial will guide you through setting up multiple domains and websites using Apache virtual hosts on an Ubuntu 18.04 server. During this process, you will learn how to offer different content to different visitors based on the domains they request.

For a more detailed version of this tutorial, with more explanations of each step, see How to Configure Apache Virtual Hosts in Ubuntu 18.04.

Prerequisites

To complete this tutorial, you will need access to the following on an Ubuntu 18.04 server: A

sudo user on

  • your server An
  • Apache2 web server, which you can install with sudo apt install apache2

Step 1 — Create the

directory structure

We will first make a directory structure that will contain the site data that we will serve to visitors in our top-level Apache directory. We’ll use example domain names, highlighted below. You need to replace them with 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

Now we must change the permissions to our current non-root user to be able to modify the files.

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

  1. sudo chown -R $USER:$USER /var/www
  2. /

  3. test
  1. .com/public_html

In addition, we will ensure that read access to the general web directory and all the files and folders it contains is allowed so that the pages can be served correctly.

  1. sudo chmod -R 755 /var/www

Step 3 — Create demo pages for each virtual host

Let’s create some content to serve, we’ll do a demo index.html page for each site. We can open an index.html file in a text editor for our first site, using nano for example.

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

Within this file, create a domain-specific HTML document, such as the following:

<html> <head> <title>Welcome to Example.com!</title> </head> <body> <h1>Success! The virtual host is example.com working!</h1> </body> </html> Save and close the file

, then copy this file to use as the basis for our second site

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

Open the file and modify the relevant information:

  1. nano /var/www/test.com/public_html/index.html

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

Save and close this file as well

.

Step 4 — Create new

virtual host files

Apache comes with a default virtual host file called 000-default.conf that we will use as a template. We’ll copy it to create a virtual host file for each of our domains.

Create the first

virtual host

file

Start by copying

the file for the first domain: sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

Open the new file in your editor (we’re using nano below) with root privileges:

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

We will customize this file for our own domain. Modify the highlighted text below according to your own circumstances.

<VirtualHost *:80> ServerAdmin admin@example.com 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 merged </VirtualHost>

At this point, save and close the file.

Copy the first virtual host and customize it for the second domain

Now that we have our first virtual host file established, we can create our second by copying that file and adjusting it as needed.

Start by copying it

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

Open the new file with root privileges in your editor:

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

Now you need to modify all pieces of information to reference your second domain. The final file should look similar to the following, with highlighted text corresponding to your own relevant domain information.

<VirtualHost *:80> ServerAdmin admin@test.com 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 merged </VirtualHost>

Save and close the file when you are finished

.

Step 5 — Enable the new

virtual host files

With our virtual host files created, we need to enable them. We will use the a2ensite tool to achieve this goal.

sudo a2ensite example.com.conf sudo a2ensite test.com.conf Next, disable the default

site defined in

000-default.conf: sudo

  1. a2dissite 000-default.conf

When you are finished, you must restart Apache for these changes to take effect and use systemctl status to verify the success of the restart

.

  1. sudo systemctl restart apache2

Your server should now be configured to serve two websites

  1. .

Step 6 — Configure

the local hosts file (optional) If you have not been using

actual domain names that you own to test this procedure and have been using some example domains instead, you can test your work by temporarily modifying the hosts file on your

local computer. On a local

Mac or Linux computer, type the following:

  1. sudo nano /etc/

hosts

For a local Windows machine, find instructions on how to modify your hosts file here

.

Using the domains used in this guide and replacing your server’s IP with the text your_server_IP, your file should look like this:

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

Save and close the file. This will direct any example.com and test.com requests on our computer and send them to our server.

Step 7 — Test Your Results

Now that you

have your virtual hosts configured, you can test the configuration by going to the domains you configured in your web browser

: http://example.com You should see a page that

looks like this:

You can also visit your second page and view the file you created for your second site.

http://test.com

If both sites work as expected, You have configured two virtual hosts on the same server.

If you adjusted the hosts file on your home computer, delete the lines you added.

Related Tutorials

Here are links to more additional guides related to this tutorial:

How to Configure Apache Virtual Hosts in Ubuntu 18.04 Domains

  • and DNS in DigitalOcean
  • How to Rewrite URL with mod_rewrite for Apache in Ubuntu 18.04

Contact US