×
Additional Image

What is Virtual Hosting and How Does It Work?

Updated: April 7th, 2024 6 min read

When you purchase through links on this site, I may earn an affiliate commission. Here’s how it works.

Virtual hosting allows a single web server to host multiple websites or applications, serving each over a single IP address. The key benefits are cost-efficiency and easier management for hosting providers and developers.

I’ll explain how virtual hosting works, the main types like name-based and IP-based, along with key concepts like domains, DNS, Apache, and more. You’ll also learn steps to set up virtual hosts yourself.

Key Takeaways

  • Virtual hosting enables one server to host multiple sites over a single IP
  • Main types are name-based (uses hostnames) and IP-based (unique IP per site)
  • Name-based relies on DNS to map multiple domain names to the shared server IP
  • IP-based assigns each site a dedicated IP address to isolate at the IP level
  • Benefits include lower costs, simplicity for developers, and leveraging provider infrastructure

Name-Based vs IP-Based Virtual Hosting

The two primary approaches to virtual hosting are:

Name-based: Maps multiple domain names to one server IP. The web server checks the Host HTTP header to determine which website to display. This is the most common method.

IP-based: Assigns each website its own unique IP address on the server. The server identifies sites based solely on destination IP, without needing virtual host names.

Name-based virtual hosting is generally easier to implement. IP-based allows isolating sites, but requires procuring dedicated IPs which gets expensive.

Now let’s explore exactly how these technologies function…

Understanding Name-Based Virtual Hosting

Name-based virtual hosting relies on mapping multiple domain names to a shared web server and IP address.

For example, hosting provider ServerCo hosts these client sites:

  • site1.com
  • site2.biz
  • site3.org

Instead of a dedicated server per site, all are hosted on one shared machine using name-based virtualization.

When a request comes in for site1.com, the domain is checked by the web server against its virtual host configs to determine which site to display.

This allows hosting multiple domains on one IP without conflict. The HTTP Host header defines which hostname is being accessed.

Name-based virtual hosting depends on DNS to resolve all domains to the shared server IP. Traffic reaches the same physical machine regardless of which site visitors type into their browser.

The web server software (like Apache) then selects the correct virtual host configuration to use based on matching the Host header to registered virtual host names.

How Does IP-Based Virtual Hosting Work?

With IP-based virtual hosting, each website receives its own dedicated IP address on the server.

For example, ServerCo’s client sites could be assigned IPs like:

  • site1.com – 172.20.30.1
  • site2.biz – 172.20.30.2
  • site3.org – 172.20.30.3

When a request comes in, the web server checks which IP it was received on to determine which virtual host to match:

  • Traffic to 172.20.30.1 displays site1.com
  • Traffic to 172.20.30.2 displays site2.biz

And so on – no need to inspect domain names, just the destination IP.

This approach isolates sites at the IP level for extra separation between virtual hosts. But dedicated IPs require more networking resources as sites scale up.

Key Advantages of Virtual Hosting

Virtual hosting allows hosting multiple sites on fewer physical servers compared to one dedicated machine per site. The main benefits are:

Cost Efficiency

  • Hosting providers reduce hardware, electricity, cooling, rack space, and management costs per site. These savings get passed down to customers.

Performance & Reliability

  • Sites reside on high traffic data center servers with excess capacity to absorb spikes. Providers maintain connectivity, caching, security updates, and uptime monitoring.

Developer Convenience

  • No need to manage bare metal hardware or virtual machines. Developers simply deploy code to isolated virtual host containers maintained by the hosting company. Updates and scaling are handled for you.

Next I’ll walk through configuring virtual hosts yourself…

Setting Up Name-Based Apache Virtual Hosts on Ubuntu

I’ll demonstrate an example configuring name-based Apache virtual hosts on Ubuntu 20.04. The same ideas apply across web servers and operating systems.

1. Install Apache and Enable Modules

First install Apache if you don’t already have it:

sudo apt install apache2

Enable the required modules:

sudo a2enmod rewrite
sudo a2enmod headers

The rewrite module allows clean URLs. The headers module passes custom HTTP headers like Host.

2. Set Up Directory Structure

Under /var/www/html, make folders for each virtual host site:

sudo mkdir /var/www/html/site1.com  
sudo mkdir /var/www/html/site2.org

Add index pages to test:

<!-- /site1.com/index.html -->
<h1>Site 1</h1> 

This will display “Site 1” when accessing this virtual host.

3. Define Virtual Hosts

Open the default site config file:

sudo vim /etc/apache2/sites-available/000-default.conf

Add virtual host container blocks for each site:

<VirtualHost *:80>
    ServerName site1.com
    DocumentRoot /var/www/html/site1.com 
</VirtualHost>

<VirtualHost *:80>
    ServerName site2.org 
    DocumentRoot /var/www/html/site2.org
</VirtualHost>

This maps hostnames to their document root directories.

4. Enable .htaccess Overrides

Web servers host many sites, so they allow certain directives to be overridden via distributed config files like .htaccess:

<Directory /var/www/>
    AllowOverride All
</Directory> 

Then reload Apache for changes to apply:

sudo systemctl reload apache2

The virtual hosts are now ready!

5. Test the Virtual Host Configuration

With Apache running, test your setup in a browser:

  • http://server_IP
  • http://server_IP

Then edit the URLs to add your virtual host names:

  • http://site1.com
  • http://site2.org

You should see the index pages load proving successful virtual host configuration!

The same IP now displays different content based on the hostname.

Alternative Virtual Hosting Methods

There are also some less common ways to structure virtual hosting:

Port-Based Hosting

Uses different ports instead of hostnames to isolate sites. Rarely used due to lack of consistent browser support for port numbers in URLs.

OS-Level Virtualization

Containers like Docker and Jails allow extreme isolation between virtual hosts by packing them into separate user space instances on the host. Adds security but complexity.

Custom Server Software

Specialty programs like Zeus Web Server and Linux Virtual Server (LVS) have built-in virtual hosting capabilities beyond the basics provided by Apache and Nginx.

Conclusion

To wrap up, be sure you grasp these supporting concepts related to virtual hosting:

  • Domain registration – Buying your website’s domain to assign to hosting server IPs. Usually via domain registrars like Namecheap and GoDaddy.
  • DNS configuration – Setting up DNS records to point from your domain to hosting provider web/mail server IPs. Typically configured through domain registrars.
  • Reverse proxy servers – NGINX and Varnish Cache are popular open source proxies used in large deployments to route traffic to backend web servers running virtual hosts. Adds security, caching, load balancing and more.

Virtualization crucially powers today’s web infrastructure. With shared name-based Apache hosting on Linux, you now have a solid starting point to build, experiment, and scale out your applications.

author
Neil Beckett
Neil is an accomplished web, designer and developer with over 15 years of experience in creating and optimizing websites for small businesses and online entrepreneurs. Read full bio

You might also like...

author

Neil is an accomplished web, designer and developer with over 15 years of experience in creating and optimizing websites for small businesses and online entrepreneurs.

Todays Best
Hosting Deals 🚀