Grid Guides

Explore How System On Grid Can Work For You

How to Setup a Apache Virtual Hosts on Ubuntu 18.04


Table of Contents

Introduction

Virtual Hosts

Virtual host means the method of running more than one website on a single system. For example,host1.domain.com, host2.domain.com 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 ongrid.ml. So, there the site files can be stored.

$ sudo mkdir -p /var/www/html/ongrid.ml/           

Next, set the appropriate permissions.

$ sudo chmod -R 775 /var/www/html/ongrid.ml/
$ sudo chown -R www-data:www-data /var/www/html/ongrid.ml/

Create an index.html in the document root directory for testing purpose.

 $ sudo vim /var/www/html/ongrid.ml/index.html

Add the HTML code inside

<html>
    <head>
        <title>Welcome to ongrid.ml!</title>
    </head>
    <body>
        <h1>The ongrid.ml virtual host is working!</h1>
    </body>
</html>

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/ongrid.ml.conf

Paste the following in the directive updated with the new directory and domain name.

<VirtualHost *:80>
    ServerName ongrid.ml
    ServerAlias www.ongrid.ml
    ServerAdmin [email protected]
    DocumentRoot /var/www/html/ongrid.ml/
    ErrorLog ${APACHE_LOG_DIR}/ongrid.ml_error.log
    CustomLog ${APACHE_LOG_DIR}/ongrid.ml_access.log combined
</VirtualHost>

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 ongrid.ml.conf

Test apache configuration if there is no error you are going to see ‘syntax ok’. Restart, apache2 for changes.

$ sudo apache2ctl configtest

apacheconfigtest

$ sudo systemctl restart apache2

The domain name ongrid.ml 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.

216.200.116.101 ongrid.ml

Open a browser enter the URL as shown,

http://ongrid.ml

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

setupapachevirtualhosts

Conclusion

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