Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How to Setup a Apache Virtual Hosts on Ubuntu 18.04

Table Of Contents

    ## Table of Contents

    – [Introduction](#introduction
    – [Prerequisites](#prerequisites
    – [Creating a directory](#creating-a-directory
    – [Create a virtual host](#create-a-virtual-host
    – [Conclusion](#conclusion

    ## Introduction
    ### Virtual Hosts
    Virtual host means the method of running more than one website on a single system. For example,, etc., Virtual hosting is very useful in running multiple websites in a single system.

    ### Types of Virtual Hosts in Apache

    There are two types of virtual hosts in Apache:

    #### IP based virtual hosting
    IP based virtual hosting can run multiple websites/domains on a single system. where every domain or website has each IP address different.

    #### name- based virtual hosting
    name-based virtual hosting can run multiple domains/websites on a system and every domain/website has the same IP address.

    ## Prerequisites

    You must have a domain name which is pointed to the public server IP address.
    Get Apache installed on your system.
    You must have a non-root user account with sudo privileges. Set up this non-root user account by following the guide, [initial server setup with Ubuntu 18.04](

    ## Creating a Directory

    Website files of the particular domain name are stored in a document root directory. So, that they can be available to us when we request for any file in response.
    Document root can be set to any location where ever you want it.

    Create a specific directory for each domain we want to host inside the server /var/www/ directory. This directory is created for storing the data of the virtual hosts. we will create a directory public_html to store the domain website files.

    Create a document root directory for your domain as So, there the site files can be stored.
    $ sudo mkdir -p /var/www/html/
    Next, set the appropriate permissions.
    $ sudo chmod -R 775 /var/www/html/
    $ sudo chown -R www-data:www-data /var/www/html/

    Create an index.html in the document root directory for testing purpose.
    $ sudo vim /var/www/html/
    Add the HTML code inside

    Welcome to!

    The virtual host is working!

    Next, save and close the file.

    ## Create a virtual host
    Apache Virtual Hosts files are stored in/etc/apache2/sites-available directory, enabled by creating links to the /etc/apache2/sites-enabled directory.

    Create a virtual host configuration file.
    $ sudo vim /etc/apache2/sites-available/
    Paste the following in the directive updated with the new directory and domain name.

    DocumentRoot /var/www/html/
    ErrorLog ${APACHE_LOG_DIR}/ongrid.ml_error.log
    CustomLog ${APACHE_LOG_DIR}/ongrid.ml_access.log combined

    Save and close the file.

    Servername: The domain name should match the virtual host configuration.
    Serveralias: www domain should not match with the virtual host.
    Documentroot: Apache servers the domain files from the directory.
    Options: It shows which features are available in a directory.
    Index: Stop directory listings.
    Followlinks: It tells to follow the symbolic links to the web server.
    Allowoverride: Identifies directives which are to be placed in the .htaccess file.
    Errorlog: Identifies the location to log files.
    Customlog: specifies the location to the log files.

    Create a symbolic link from the virtual host file to the sites-enabled directory, to enable the new virtual host apache2 reads it during startup.

    Enable the virtual host by using the a2ensite helper.
    $ sudo a2ensite
    Test apache configuration if there is no error you are going to see ‘syntax ok’. Restart, apache2 for changes.
    $ sudo apache2ctl configtest
    $ sudo systemctl restart apache2
    The domain name is a dummy domain. You need to set up DNS by adding it to the /etc/hosts file.
    $ sudo vim /etc/hosts
    Add the following line at the end of the file, replace the server IP address and domain name with your server IP address and domain name.

    Open a browser enter the URL as shown,

    You will get the following output as shown in the screenshot.


    ## Conclusion

    Creating an apache virtual host configuration to host multiple websites on a single Ubuntu server.