How to create virtualhost on ubuntu

Hello All,

In this blog, I am going to document how I do most of my co-hostings using apache2 virtualhost. I will be using virtualbox and ubuntu 14.04 guest.

Preparatory work

Usually I create a virtual machine with 2 network interfaces.

eth0 <--- NAT adapter IP: 10.0.2.255
eth1 <--- HostOnly adpater IP: 192.168.56.105

NAT adapter is for making the guest use the internet connection of the host but unfortunately with that adapter, the host has no way of connecting to the guest. So no ssh would work. The HostOnly adapter is the trick to still have the guest reachable to the host while it uses the host internet connection.

Domain Registration

Normally to have apache running on a certain box serve a domain name, that domain name has to be registered with ICANN through a registrar like dynadot, 1and1 or godaddy.In our local environment we will use how own registrar like /etc/hosts in order to let my host associate our chosen domain with the guest.

# /etc/hosts file
127.0.0.1       localhost
192.168.56.105  node5.localhost

Let’s verify whether our entry is working using ping method.

[email protected]:~$ ping node5.localhost
PING node5.localhost (192.168.56.105) 56(84) bytes of data.
64 bytes from node5.localhost (192.168.56.105): icmp_seq=1 ttl=64 time=0.785 ms
64 bytes from node5.localhost (192.168.56.105): icmp_seq=2 ttl=64 time=0.648 ms
64 bytes from node5.localhost (192.168.56.105): icmp_seq=3 ttl=64 time=1.23 ms
64 bytes from node5.localhost (192.168.56.105): icmp_seq=4 ttl=64 time=0.374 ms

Connection to the guest

Let’s configure our virtualhost. In order to do that we need to connect to the guest .
Our guest has been created with the username Pakkun.

ssh [email protected]
The authenticity of host '192.168.56.105 (192.168.56.105)' can't be established.
ECDSA key fingerprint is dc:08:33:9e:15:9c:2e:73:a2:3f:ba:2e:b2:70:17:5a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.56.105' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Sun Apr 12 16:23:05 GMT 2015

  System load:  0.07              Processes:           68
  Usage of /:   14.8% of 7.26GB   Users logged in:     0
  Memory usage: 10%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

164 packages can be updated.
78 updates are security updates.

Last login: Sun Apr 12 16:23:05 2015
[email protected]:~$ 

Installation of required packages

Now that we have logged in let’s install required packages, apache2, php5

$sudo apt-get install apache2 php5 php5-cli

Let’s verify our installation by visiting 192.168.56.105 in a browser, it should be looking like the image below.

apache on guest machine

Creation of the virtualhost

This installation is based on ubuntu so debian family file system would be used. This means :

  • we can create a vhost document root under /var/www/html
  • we can create a vhost file under /etc/apache2/sites-available
  • we can activate or deactivate the virtualhost using a2ensite or a2dissite

let’s create our document root where our files would be kept

sudo mkdir -p /var/www/html/vhosts/node5.localhost/htdocs

Let’s create the virtualhost file for node5.localhost. Our file would be created in the folder /etc/apache2/sites-available.

sudo nano /etc/apache2/sites-available/node5.localhost.conf

Let’s make sure it looks like shown below

#/etc/apache2/sites-available/node5.localhost.conf
<VirtualHost *:80>

        ServerName node5.localhost

        ServerAdmin [email protected]
        DocumentRoot /var/www/html/vhosts/node5.localhost/htdocs

        LogLevel warn

        ErrorLog ${APACHE_LOG_DIR}/node5_error.log
        CustomLog ${APACHE_LOG_DIR}/node5_access.log combined

</VirtualHost>

Let’s create a php file to run in our virtual host.let’s create our index.php shown below

  sudo nano /var/www/html/vhosts/node5.localhost/htdocs/index.php
<?php

echo "welcome to node5.localhost"

?>

Let’s activate our virtual host with the command a2ensite. What that command does is to actually create a symbolic link in the /etc/apache2/sites-enabled folder.

[email protected]:~$ sudo a2ensite node5.localhost.conf
Enabling site node5.localhost.
To activate the new configuration, you need to run:
  service apache2 reload

After that a reload of apache configuration would make the virtualhost available.

sudo service apache reload

Now let’s visit our site at http://node5.localhost, it should be looking like beow

node 5 virtualhost

Just in case you are hosting php frameworks like joomla, etc you might want to add a directory tag to override default apache configuration.

#/etc/apache2/sites-available/node5.localhost.conf
<VirtualHost *:80>

        ServerName node5.localhost

        ServerAdmin [email protected]
        DocumentRoot /var/www/html/vhosts/node5.localhost/htdocs

        <Directory /var/www/html/vhosts/node5.localhost/htdocs>
                  Options Indexes FollowSymLinks
                  AllowOverride All
                  Require all granted
        </Directory>

        LogLevel warn

        ErrorLog ${APACHE_LOG_DIR}/node5_error.log
        CustomLog ${APACHE_LOG_DIR}/node5_access.log combined

</VirtualHost>

Leave a Comment

Your email address will not be published. Required fields are marked *

captcha * Time limit is exhausted. Please reload the CAPTCHA.