Skip to content

How to Setup Virtual Hosts in Ubuntu

Last updated on February 20, 2018

In this post, I will walk you through, how to Setup Virtual Hosts in Ubuntu. Virtual Hosts are used to setting up more than one domain or websites using a single IP address. This is very useful if anybody wants to run multiple websites using a single IP address on single VPS.

Here are the simple steps to make virtual hosts on Apache in Ubuntu OS.

For example purpose, I am assuming that you are going to create a virtual host for “pravinh.com”.

Let’s began. The first thing that you need to create a directory to hold and serve project files.

   sudo mkdir -p /var/www/pravinh.com/html

Now give permissions to it. the above directory is owned by root user now. We should change the ownership of this directory to the regular user.

sudo chown -R $USER:$USER /var/www/pravinh.com/html/

The “$USER” variable indicates the currently logged in user. Set the read permissions to the web root (/var/www/pravinh.com/html) directory, so that everyone can read files from that directory.

sudo chmod -R 755 /var/www/pravih.com/html

Now clone the default Apache configuration and create pravinh.com.conf file.

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

Open the new configuration file and update the values for the ServerName, ServerAlias, and DocumentRoot. Where ServerName is the virtual host name and the ServerAlias is the www version. The DocumentRoot is the absolute path to the directory where your project is stored.


        ServerName pravinh.com
        ServerAlias www.pravinh.com

        ServerAdmin [email protected]
        DocumentRoot /var/www/pravinh.com/html

Next, enable the new virtual host configuration. If you want to disable virtual host configuration use a2dissite instead of a2ensite.

$ sudo a2ensite pravinh.com.conf

Lastly, add it to the hosts file.

$  sudo nano /etc/hosts

And add the virtual host domains one by one as shown below.

127.0.0.1 pravinh.com
127.0.0.1 www.pravinh.com

Last step but very important step restart Apache.

$ sudo service apache2 restart
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments