How To Create Apache Virtual Hosts On Ubuntu 18.04

In this tutorial, we will learn how to create a virtual host from scratch on ubuntu 16.04 and 18.04

If you haven't install Apache2, PHP, Mysql (LAMP) you can follow below

Install Apache2, PHP, Linux, Mysql (LAMP)

A virtual host is a way, method to run multiple websites on a single machine. for example webiste1.com, website2.com, etc. There are two types of virtual host:

  • IP-based virtual hosts.
  • Name-based virtual hosts.

In this tutorial, we are going to learn about how to create a virtual host in your local machine.

Note: This tutorial is only for Ubuntu users.

So, let's get started. 

Step 1. Create a directory for each domain.

In this post, we'll take an example to create virtual host for two domains. Below is the domain we are going to create virtual host

  1. example1.local
  2. example2.local

Before we get started to create a virtual host we will need to create a separate directory for each domain. To do so enter the following command:

#/var/www/html/
mkdir example1 example2

The above command will create two directories example1 and example2 in /var/www/html path

Step 2. Setting up permissions

Next, we will give the folder permission to apache2. Enter the following command

sudo chown -R www-data:www-data /var/www/html/example1
sudo chown -R www-data:www-data /var/www/html/example2

Step 3. Create a demo page for each virtual host.

We will create a demo page for example1

#/var/www/html/example1/index.html

<html lang="en">
<head>
  <meta charset="utf-8">

  <title>Demo title for domain example1</title>
  <meta name="author" content="DesertEBS">

</head>

<body>
  <h1>This is demo page for example1 domain.
</body>
</html>

Now we have created a demo page for domain example1. we don't have to create each time a new file. we can copy this file to another domain and edit. To do so, Enter the following command.

cp /var/www/html/example1/index.html /var/www/html/example2/index.html

Edit and change the content.

#/var/www/html/example2/index.html

<html lang="en">
<head>
  <meta charset="utf-8">

  <title>Demo title for domain example2</title>
  <meta name="author" content="DesertEBS">

</head>

<body>
  <h1>This is demo page for example2 domain.
</body>
</html>
 

Step 4. Create a virtual host file for each host.

Now we will create a virtual host for each domain.

we'll copy and edit the existing 000-default.conf. To do so enter the following command for example1 domain.

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

Now edit the copied file in your code editor.

#/etc/apache2/sites-available/example1.conf
<VirtualHost *:80>
        ServerName example1.local
        ServerAlias www.example1.local
        DocumentRoot /var/www/html/example1

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Same process for domain 2.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example2.conf
#/etc/apache2/sites-available/example2.conf
<VirtualHost *:80>
        ServerName example2.local
        ServerAlias www.example2.local
        DocumentRoot /var/www/html/example1

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 5. Enable virtual host (demo site)

Next, enable created, virtual hosts.

To enable domain example1. enter the following command.

sudo a2ensite example1.conf

Output:

Enabling site example1.local
To activate the new configuration, you need to run:
  service apache2 reload

Same for the domain 2.

sudo a2ensite example2.conf

Output:

Enabling site example2.local
To activate the new configuration, you need to run:
  service apache2 reload

Step 6. Restart the apache2 server

Now restart the server.

sudo service apache2 reload