Web Server Virtual Host



Using Apache Virtual Host, you can run several websites on the same server.

For example, I can run both thegeekstuff.com and top5freeware.com on a single physical server that has one Apache webserver running on it.

Virtual hosting is a method for hosting multiple domain names (with separate handling of each name) on a single server (or pool of servers). This allows one server to share its resources, such as memory and processor cycles, without requiring all services provided to use the same host name. Virtual web hosting is a cheaper hosting option because you won't have to pay for a dedicated server to host just your website. Virtual web hosting is a good solution for small- to medium-sized (and even some larger) websites that aren't constantly being visited or have reasonable bandwidth needs. A Virtual Private Server (VPS) is the next step up from shared web hosting, offering the power and configurability of a dedicated server, but without any hardware issues to worry about. We have powerful physical servers partitioned into isolated virtual servers - each of them looks and behaves like a real networked server system.

Fig: Apache Virtual Host (Multiple websites, one Apache)


There are two types of Apache virtual host configurations: 1) IP-Based Virtual Host and 2) Name-based Virtual Host. Name-based virtual host is recommended for most scenarios.

IP-Based Virtual Host

In this configuration, when you are pointing two websites (with different ip-address) to the server that runs Apache, that physical server should have two different ip-address configured.

This means that the server should have two ethernet cards, each one of them configured to the ip-address of the corresponding website that Apache virtual host will be serving. So, this is not practical for most aspects, and you should not be using this.

In the following example, the server contains two NIC cards, one is configured with 192.168.101.1 ip-address for thegeekstuff.com, another is configured with 192.168.102.1 for top5freeware.com. Both these ip-address are served by a single Apache webserver running on that server using IP-Based virtual host.

Name-Based Virtual Host

In this configuration, when Apache webserver receives a request, it looks for the hostname in the HTTP header, and depending on the hostname, it servers different websites. This is very easy, as you need only one ip-address on that physical server; but, you update the DNS with multiple website names pointing to the same ip-address. For all practical purpose, you’ll be using only Name-based virtual host configuration.

In the following example, the server contains only one NIC card, which is configured with 192.168.101.1 ip-address. The DNS entry for both thegeekstuff.com and top5freeware.com website points to 192.168.101.1 ip-address. When Apache recives a request, it looks for the hostname entry in the HTTP header, and serves the corresponding website.

Fig: Apache Name-Based Virtual Host

1. Uncomment httpd-vhosts.conf in httpd.conf

If you’ve installed Apache 2 from source, by default, the following line will be commented in the httpd.conf file. Uncomment this line.

2. Setup virtual hosts

Modify the httpd-vhosts.conf as shown below to setup named-based virtual host setting for two hosts.

  • NameVirtualHost *:80 – Indicates that all the name-based virtual hosts will be listening on the default port 80
  • <VirtualHost *:80> </VirtualHost> – Enclose all the apache configuration parameters for each and every virtual host between these VirtualHost tags. Any apache directives can be used within the virtualhost container.
  • In the following example, we are setting up virtual host for thegeekstuff.com and top5freeware.com listening on the same port 80. So, there will be two <VirtualHost *:80> </VirtualHost>, one for each website.
  • When you go to thegeekstuff.com, the files under /usr/local/apache2/docs/thegeekstuff will be served by Apache; and the access_log and error_log for this site will go under /usr/local/apache2/logs/thegeekstuff

3. Check VirtualHost Configuration Syntax

Verify virtual configuration syntax using “httpd -S” as shown below. When everything is setup properly, it just displays “Syntax OK”.

When something is not configured properly, it will display warning message, including “directory does not exit” message as shown below.

4. Restart the Apache and test

Remote desktop for android to mac. Now, when you go to thegeekstuff.com (or www.thegeekstuff.com), the apache will serve the files from /usr/local/apache2/docs/thegeekstuff directory.

When you go to top5freeware.com (or www.top5freeware.com), the same apache running on the same server will serve the files from /usr/local/apache2/docs/top5freeware directory.

Using carbon copy cloner to make bootable drive. Use Carbon Copy Cloner To Make An Exact Copy Of Your Hard-Disk Though Time Machine is a very useful and simple to use backup utility, it is sound practice to not put all your digital eggs into one basket. Further, when you upgrade your hard drive, cloning.

Just to reiterate, for the name-based virtual host to work properly, the DNS entry for both these websites should be pointing to the same external ip-address of the physical server where the Apache webserver is running.

Hellow there! How’s it going? Today I thought of concentrating my blog on Apache Virtual Host. There you have the URL to the documentation page. It’s for the Apache HTTP Server Version 2.2. Have you got time to read all that? I mean who’s got time to read all those docs? That’s why I’m here for. I’ll make this blog post short and sweet for you(happy-face).

What’s Apache Virtual Host?

Apache Virtual Hosts A.K.A Virtual Host(Vhost) are used to run more than one web site(domain) using a single IP address. In other words you can have multiple web sites(domains) but a single server. Different sites will be shown depending on the user’s requested URL. Best part is you can have any number of virtual hosts in a single server. It simply means you can have any number of web sites(domains) in a single server.

As you see in the above picture, requests from each domain will be mapped into respective document root. If you don’t know what document root means it is where all the files of the website are located(could be public_html). I know you know what document root means, I’m just messing around(tongue-out-face). Let’s get serious again. There are

  1. Name-Based Virtual Host
  2. IP-Based Virtual Host

In most scenarios Name-based virtual host is used. So today we are going to talk about Name-Based Virtual Host and it’s configurations. I don’t want to leave you in the dark. So I’ll go through basics of IP-Based Virtual Host as well.

1. IP-Based Virtual Host

In this scenario the physical server should have two ip addresses, in other words the physical server should have two ethernet cards, each one of them are configured to the particular ip-address of the corresponding website. Don’t get confused. There is only one physical server running Apache but two IPs.

  • www.abc.com has the 192.168.100.1 IP address and a virtual host in Apache which points to www.abc.com’s document root.
  • www.xyz.com has the 192.168.100.2 IP address and a virtual host in Apache which points to www.xyz.com’s document root.

2. Name-Based Virtual Host

Most of the time you will be using name-based virtual host configuration. Let me explain what happens then we’ll move on to how to get to this done. When a request is made to the Apache web server, it looks for the hostname in the HTTP header in the given request. Depending on the hostname, requested will be served. Compared to previous scenario here the server has got only one ip-address but multiple web sites(domains) will be pointing to the server. Here abc.com and xyz.com both sites points to the same Apache web server which has got the ip address of 192.168.100.1. In this scenario we need to have two virtual hosts, one for xyz.com and one for abc.com. Are you worried about how to setup your virtual hosst? That’s what we are going to talk about next.

Web Server Virtual Host

How To Use Apache Virtual Host?

Here we are going to focus on the name-based virtual hosting. To set up a virtual host you should have the following. What we call “prerequisites”.

  • Apache web server
  • Directory which keeps the website’s files(/var/www/dasunhegoda.com/public_html)
  • Correct permission, 755 for folders and 644 for files

If you have all the above mentioned prerequisites you are good to proceed. Let’s get techie(happy-face).

In the below code snippets dasunhegoda.com should be replaced with your site name.

Mtg for mac. 1. New Virtual Host File

Here we get a copy of the default file.

2. Open The New Virtual Host File & Edit

Above command will open up the newly created file. If you don’t have vim editor, you can use vi editor or nano editor for this. Paste the code below.

Let me explain. Virtual hosts, is defined by <VirtualHost> sections. Keep in mind that having logs in your vhost is not mandatory but makes the life easier to figure out what’s going on.

  • VirtualHost *:80 – Virtual hosts will be listening on the default port 80(could 443 if you are using https)
  • ServerAdmin – Server Admin’s email
  • DocumentRoot – Path where web site files are located
  • ServerName – Server name
  • ServerAlias – Alternate names
  • ErrorLog – File contains any errors that it encounters in processing requests
  • CustomLog – All requests processed by the server. Access log file

Note that you don’t have to have the all the directive in your virtual host. Even having DocumentRoot and ServerName would do the job. See more example here.

3. Activate the host

Activate the virtual host using above command. a2ensite is a built in apache command.

4. Restart Apache

Above command will restart the Apache server. Remember whenever you make configuration changes you should restart Apache server.

*. Disable the virtual host

Web Server Virtual Host

At any point if you want to disable the virtual host you can use above command to disable. a2dissite is a built in apache command.

Tips & Tricks

Apache Web Server Virtual Hosting

Here I’m going to tell you a dirty way to get the job done. But below method is not recommend.

Rather than going through all above steps you can right way edit the apache2.conf file which contains all the Apache server configurations. Just copy and paste the VirtualHost code snippet and restart the server. Disadvantage is you can n’t use a2ensite or a2dissite to enable and disable the sites.

Virtual Machine Host Server

That’s all about Apache virtual hosts. If you have any questions let me know in the comments below. Your feedback is highly appreciated.





Comments are closed.