Home Apache How To Configure Apache Virtual Hosts In Ubuntu 16.04-18.04

How To Configure Apache Virtual Hosts In Ubuntu 16.04-18.04

4
How To Configure Apache Virtual Hosts In Ubuntu 18.04 LTS
How To Configure Apache Virtual Hosts In Ubuntu 18.04 LTS

How To Configure Apache Virtual Hosts In Ubuntu 16.04-18.04 LTS – Apache Virtual hosting is a method for hosting multiple domain names on a single server. This allows one server to share its resources, such as memory and processor cycles.

How To Configure Apache Virtual Hosts In Ubuntu 16.04-18.04 LTS

My test server IP address is 192.168.100.100 and host name is apachevirtual.

First, we will see how to configure name-based virtual hosts in web server.

Step 1: Install Web Server

Make sure you have installed web server. To install it on Ubuntu, run:

$ sudo apt-get install apache2

To test the setup, open your browser and type your server IP address or host name. If you can see below landing page you have installed HTTP server correctly.

Apache Landing Page

Good! web server is up and running!!

Step 2: Create web directory for each host

I am going to create two virtual hosts, namely example.com and example.org.

Let us create a directory for first virtual host example.com. This directory is required for storing the data of our virtual hosts.

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

Likewise, create a directory for second virtual host example.org as shown below.

$ sudo mkdir -p /var/www/html/example.org/public_html

The above two directories are owned by root user. We need to change the ownership to the web server group (www-data).

$ sudo chown -R $USER:www-data /var/www/html/example.com/public_html
$ sudo chown -R $USER:www-data /var/www/html/example.org/public_html

Set read permissions to the Apache root directory

$ cd /var/www/html/example.com/public_html

$ find . -type f -exec chmod 664 {} \;

$ find . -type d -exec chmod 775 {} \;

$ cd /var/www/html/example.org/public_html

$ find . -type f -exec chmod 664 {} \;

$ find . -type d -exec chmod 775 {} \;

Step 3: Create Sample Web Pages for Each Host

Let us create a sample page for example.com site. To do so, run:

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

Add the following lines in it:

<!DOCTYPE html>
<html>
<head>
<title>www.example.com</title>
</head>
<body>

<h1>www.example.com</h1>
<p>This is a paragraph.</p>

</body>
</html>

Save and close the file. Create a sample page for example.org site too:

<!DOCTYPE html>
<html>
<head>
<title>www.example.org</title>
</head>
<body>

<h1>www.example.org</h1>
<p>This is a paragraph.</p>

</body>
</html>

Step 4: Create Configuration File for Each Hosts

We need to create configuration files for each virtual host. Copy the default virtual host file called 000-default.conf contents to the new virtual host files like below.

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

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

Please be make sure that you must save all configuration files with .conf extension

Let’s modify the configuration files to match with our virtual hosts.

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

Edit or modify ServerAdmin, ServerName, ServerAlias and DocumentRoot values matches to virtual host.

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName example.com
        ServerAlias www.example.com

        ServerAdmin webmaster@example.com
        DocumentRoot /var/www/html/example.com/public_html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

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

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Go to next one

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

Edit or modify ServerAdmin, ServerName, ServerAlias and DocumentRoot values matches to virtual host.

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName example.org
        ServerAlias www.example.org

        ServerAdmin webmaster@example.org
        DocumentRoot /var/www/html/example.org/public_html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

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

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Step 5: Enable and Disable Virtual Hosts Configuration Files

$ sudo a2dissite 000-default.conf

$ sudo a2ensite example.com.conf

$ sudo a2ensite example.org.conf

$ sudo systemctl restart apache2

Test Virtual hosts, please add below lines to virtual host

[...]
192.168.100.100   example.com
192.168.100.100   example.org
[...]

Open up your web browser and point it to http://example.com and http://example.org hooray you are configured virtual host correctly.

As you noticed, we have used the same IP address (i.e 192.168.100.100) for host two different websites (http://example.com and http://example.org). This is what we call name-based virtual hosting. Hope this helps.

Please refer Apache Virtual Host Guide

4 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version