How To Create Apache Virtual Hosts On Ubuntu 18.04

Getting Started

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

Install Apache2, PHP, Linux, Mysql (LAMP) 

What is a virtual host?

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.

we'll create two test domain.

  1. example1.local
  2. example2.local

To create directory enter the following command:

#/var/www/html/
mkdir example1 example2

Step 2. Setting up permissions

Next, change the folder permission to the current user. currently, these folders are owned by root.

sudo chown -R $USER:$USER /var/www/html/example1
sudo chown -R $USER:$USER /var/www/html/example2

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

Next, 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 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 time to create a virtual host for each domain.

On Fresh installation, default virtual host config file ships with apache2, we'll copy and edit. 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