How To Create Virtual Host In Ubuntu

It is a very common scenario for web-developers to host more than one website on single machine for development purpose or for deployment purpose. The term virtual host is very common in such situations.

Those virtual hosts are “IP-based”, or “name based”. IP-based virtual host means to have multiple IP addresses for your multiple sites, where one or more IP address may associated with one website. Where as name-based virtual host means to have multiple names associated with each IP address. The beauty of virtual host is that, the fact that they are running on the same physical server is not apparent to the end-user.

Most web-developers are working with very known and widely used Apache web-server. Apache was one of the first servers to support IP-based virtual hosts.

The same way Ubuntu is the choice of development platform for most developers. In Ubuntu with Apache running, it is very easy to create virtual hosts.

In my experience, I have found virtual host very handy when you are dealing with the sites developed using WordPress, Joomla! and even using some frameworks with lots of URL dependency, whether on site root or on some third-party services. It helps to deploy your websites locally in your regular work space but with a proper identification (virtual domain/IP) just like you have with your live site.

Let see, how to create virtual host for local.example.com in Ubuntu for Apache server.

$ sudo nano /etc/hosts

Edit the host file and add following line just at the end of IPV4 entries.

127.0.0.1    local.example.com

Save changes and exit the file.

$ cd /etc/apache2/sites-enabled
$ sudo nano local.example.com

Last command will create a temp file on path at we are.

<VirtualHost *:80>
    ServerName    local.example.com
    DocumentRoot  (/var/www/example) # root directory of your project.
</VirtualHost>

Save file and exit. Restart Apache to apply changes.

$ sudo /etc/init.d/apache2 restart 

Leave a comment

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