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

Published on February 19, 2020
How To Set Up Apache Virtual Hosts on Ubuntu 18.04 [Quickstart]


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’ll learn how to serve different content to different visitors depending on which domains they are requesting.

For a more detailed version of this tutorial, with more explanations of each step, please refer to How To Set Up Apache Virtual Hosts on Ubuntu 18.04.


In order 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’ll first make a directory structure that will hold the site data that we will be serving to visitors in our top-level Apache directory. We’ll be using example domain names, highlighted below. You should replace these with your actual domain names.

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

Step 2 — Grant Permissions

We should now change the permissions to our current non-root user to be able to modify the files.

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

Additionally, we’ll ensure that read access is permitted to the general web directory and all of the files and folders it contains so that 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 make a demonstration index.html page for each site. We can open up 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, like the following:

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

Save and close the file, then copy this file to use as the basis for our second site:

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

Open the file and modify the relevant pieces of information:

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

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’ll use as a template. We’ll copy it over 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:

  1. 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 for 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 combined

At this point, save and close the file.

Copy First Virtual Host and Customize for Second Domain

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

Start by copying it:

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

You now need to modify all of the pieces of information to reference your second domain. The final file should look something like this, 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 combined

Save and close the file when you are finished.

Step 5 — Enable the New Virtual Host Files

With our virtual host files created, we must enable them. We’ll be using the a2ensite tool to achieve this goal.

  1. sudo a2ensite example.com.conf
  2. sudo a2ensite test.com.conf

Next, disable the default site defined in 000-default.conf:

  1. sudo a2dissite 000-default.conf

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

  1. sudo systemctl restart apache2

Your server should now be set up to serve two websites.

Step 6 — Set Up Local Hosts File (Optional)

If you haven’t 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 machine, type the following:

  1. sudo nano /etc/hosts

For a local Windows machine, find instructions on altering your hosts file here.

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

/etc/hosts   localhost   guest-desktop
your_server_IP example.com
your_server_IP test.com

Save and close the file. This will direct any requests for example.com and test.com 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 your setup by going to the domains that you configured in your web browser:


You should see a page that looks like this:

Apache virtual host example

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


Apache virtual host test

If both of these sites work as expected, you’ve configured two virtual hosts on the same server.

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

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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Hi this helped me a lot to set the configuration and understand the Virtual config. Here is my query. I need to load all example.com directory for wild card URL like

  1. abc.example.com
  2. xyz.example.com
  3. test.example.com

How can I do it. I tried some links https://serverfault.com/questions/727055/redirect-all-subdomains-to-main-domain-inside-vhost unforntunatly this doesnt worked for me. I am on Ubuntu 20.04.1 LTS. Please help me… Thanks in advance

Thank you for your simple article , it helped me a lot

Doesn’t cover setting up SSL. The insecure web is deprecated, no tutorials should just cover port 80 anymore.

Hi Lisa!

Thank you for the article :)

I’m facing some problem here - I’m running Ubuntu 18.04 on Windows 10 (WSL2) for local development. I’ve followed your instructions in the article (I think I haven’t missed out on anything). I’ve created two websites - example.local and test.local

Now the problem is a. I can’t access example.local nor test.local (Chrome says the site can’t be reached) b. Accessing localhost shows me the page for example.local. When I changed the name of the example.local.conf file to zero.local.conf and restarted the server. That led to successfully serving the test.local page (when I try to access localhost).

Would you be able to help me with this?

Best, Saket

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel