How to configure apache virtual host on ubuntu

In this tutorial, we will guide you to configure the apache virtual host or vhost on ubuntu 16.04. Using virtual host allow the administrator to use one server to host multiple domains or sites off of a single interface or IP by using a matching mechanism. Basically, you can you one IP address to server multiple domain names. Virtual host method is widely used in a shared hosting provider.

Prerequisites

Before you begin with this guide, you should at least have basic knowledge of linux, know basic shell command for Linux, root user or non-root user account with sudo privileges set up on your server. Also, you have been installed apache. If your server not installed apache you can follow How To Install Apache PHP on Ubuntu 16.04 Tutorial.

You must have point your domain name to your server IP Address. For the example in this tutorial we will create virtual host with these following details:

Domain name : test1.example.com
Document root : /var/www/example.com/

Step 1 – Create the Directory Structure

Document root is the top-level directory that Apache looks at to find content to serve. Usually using name public_html to make easy recognize. For instance, for our sites, we’re going to make our directories like this:

$ sudo mkdir -p /var/www/test1.example.com/public_html

Step 2 – Create Example Page for Virtual Host

We will create simple html file and put to virtual host document root

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

In this file, create a simple HTML document that indicates the site it is connected to. My file looks like this:

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

Step 3 – Create New Virtual Host Configuration Files

Apache comes with a default virtual host file called 000-default.conf used for default or to catch undefined virtual host. We will use as templates to create our virtual host. Create our virtual host configuration file based on 000-default.conf file using these following command:

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

Open new virtual host configuration file using nano

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

Edit that file become like this:

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName test1.example.com
    ServerAlias test1.example.com
    DocumentRoot /var/www/test1.example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Note: you can use ServerAlias to create virtual host with different name but have same document root, for example www.example.com and example.com . The configuration will be like these

ServerName example.com
ServerAlias www.example.com

Step 4 – Enable the New Virtual Host Configuration to Apache

Now that we have created our virtual host files, we must enable them. Apache includes some tools that allow us to do this. We use aen2site to enable configuration

$ sudo a2ensite test1.example.com.conf

You need to restart Apache to make these changes take effect:

$ sudo systemctl restart apache2

Step 5 – Test in Your Browser

Open your browser, in this example, we will open url http://test1.example.com to verify the results.

Related Post