Setting Up Name-Based Virtualhost Apache

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.

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.

Apache-test-page

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:

Add the following content:

Save and close the file.

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.

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

To do this, run the following command:

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

Add the following content:

Save and close the file.

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

Add the following content:

Save and close the file.

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.

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:

Save and close the file.

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:

Apache-name-virtualhost1

www.virtualhost2.com demo page:

Apache-name-virtualhost2

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.