Virtual hosting is a method for hosting multiple domain names on a single server. The are two types of virtual hosting: “Name-based virtual hosting” and “IP-based virtual hosting.” With the name-based virtual hosting, you can host multiple websites on a single machine with a single IP address.

Here, we are going to host two websites – namely “www.virtualhost1.com” and “www.virtualhost2.com” – on a single IP “192.168.1.227” on the Apache web serve, the most popular web server in the world.

Note: while Ubuntu 14.04 server is used for this tutorial, the instructions will be similar for most Linux distros.

Getting Started: Installing Apache

First, you need to update your system and then install Apache.

To do this, run the following command:

To verify whether the Web server is working or not, open the web browser and navigate to the URL http://192.168.1.227/ (assuming that your server IP address is 192.168.1.227).

You should see that the Apache web server is working.

Create Virtual Directories

First, you need to make a directory structure that will hold the website data that we will be serving to clients.

Create two directories for the websites “www.virtualhost1.com” and “www.virtualhost2.com.”

Note: the default folder for hosting your files is “/var/www/html.” For those who are using an alternative file path, you will have to change the above code accordingly.

Now you need to create an “index.html” file for both websites. This will be served by the Apache web server.

Create an index.html file for “www.virtualhost1.com” virtual host:

Add the following content:

Save and close the file.

Similarly, create an index.html file for “www.virtualhost2.com” virtual host:

Setting Up Ownership and Permissions

By default, Apache service runs as a “www-data” user in Ubuntu. Both virtual directories that we created earlier are owned by root. You must change the ownership of these two virtual directories to “www-data,” so that Apache can read and write data.

To do this, run

Also, you need to make the Apache web root (/var/www/html) directory world readable so that everyone can read files from that directory.

Create Virtual Host Files:

By default, Apache comes with a default virtual host file called “000-default.conf.” You need to disable this virtualhost file first.

Now, create a virtual host file “www.virtualhost1.com.conf” for the virtual host “www.virtualhost1.com.”

Similarly, create a virtual host file “www.virtualhost2.com.conf” for the virtual host “www.virtualhost2.com.”

After creating the virtual host files, you need to enable a new virtual host.

You can do this by running

Finally, restart the Apache service.

Testing Virtual Hosts:

You need to add a host entry on each and every remote or local system to resolve the website by name.

You can do this by editing the “/etc/hosts” file.

Add the following lines:

Open your web browser and navigate to the URLs “http://www.virtualhost1.com” and “http://www.virtualhost2.com.”

You should see the sample demo pages which we created earlier.

www.virtualhost1.com demo page:

www.virtualhost2.com demo page:

Conclusion

I hope this post will help you to set up virtual host in Ubuntu. There is no limit on the number of virtualhost Apache can handle, so you can create and host as many websites you want.

Our latest tutorials delivered straight to your inbox