Comparisons & Alternatives

Virtual Host: What It Is, Types and How It Works in Apache

An Apache virtual host is a configuration that lets a single web server serve multiple sites, each with its own domain, directory, and independent settings.

Hands collaborate on a business plan with a laptop in a modern office setting.

An Apache virtual host (or vhost) is a configuration block that tells the web server how to respond when it receives a request for a specific domain. Thanks to virtual hosts, a single physical server can host dozens — or hundreds — of independent websites, each with its own domain, document root, and configuration.

If you've ever wondered how shared hosting works under the hood, or how to set up multiple sites on a VPS, this guide explains it from the ground up.

What Are Virtual Hosts Used For?

Without virtual hosts, a web server could only serve one site. With them, Apache inspects the domain of each HTTP request and routes it to the correct directory and configuration.

Common use cases include:

  • Shared hosting: the provider creates one vhost per customer account.
  • Local development: a developer runs myproject.local and client2.local on the same machine without conflicts.
  • Internal microservices: different applications on the same VPS respond to separate subdomains.
  • Staging and production on one server: staging.example.com and example.com with different settings.

Types of Apache Virtual Hosts

Apache supports two main types of virtual hosts. Understanding the difference is key to understanding how your hosting works.

1. Name-Based Virtual Hosts

This is the most common type. Apache distinguishes sites by the Host header that the browser sends with each HTTP request. All domains share the same server IP address.

Advantage: No extra IPs required. A server with a single IP can host hundreds of domains.

Simplified configuration example:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example
</VirtualHost>

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

This is the model used by all modern shared hosting. When you add an additional domain to your plan, the host creates a new VirtualHost block in Apache's configuration.

2. IP-Based Virtual Hosts

Apache distinguishes sites by the destination IP address. Each domain requires its own IP assigned to the server.

Advantage: Works even with very old HTTP/1.0 clients that don't send a Host header. Also required for SSL on older versions of Apache (before SNI support).

When it's used today: Rarely in shared hosting, since dedicated IPs cost extra. It's used in corporate environments where each application requires its own IP for network segmentation reasons.

Quick Comparison

Feature Name-Based IP-Based
IPs required One IP for all domains One IP per domain
SSL compatibility Requires SNI (all modern browsers support it) Works without SNI
Typical use Shared hosting, VPS, local development Corporate environments, legacy systems
Cost No extra IP cost Cost per additional IP

How Apache Decides Which Virtual Host to Use

The process is straightforward but worth understanding for troubleshooting:

  1. The browser sends an HTTP request to the server's IP, including the Host: example.com header.
  2. Apache reviews its list of VirtualHost blocks in order.
  3. It looks for a block whose ServerName or ServerAlias matches the value of the Host header.
  4. If there's a match, Apache serves files from that block's DocumentRoot.
  5. If there's no match, Apache falls back to the default virtual host — typically the first one in the list.

This default vhost behavior explains why a misconfigured domain sometimes shows the wrong site: Apache falls back to the first available virtual host when no match is found.

Virtual Hosts and SSL/HTTPS

When HTTPS is needed, each domain requires its own SSL certificate. Apache handles this via SNI (Server Name Indication), a TLS extension that lets the client announce the domain before Apache sends back a certificate.

An SSL virtual host block looks like this:

<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/example
    SSLEngine on
    SSLCertificateFile /etc/ssl/example.com.crt
    SSLCertificateKeyFile /etc/ssl/example.com.key
</VirtualHost>

In shared hosting, the control panel (cPanel, Plesk, etc.) generates and manages these blocks automatically each time you install an SSL certificate for a domain.

To explore hosting options that support multiple domains with SSL, check the hosting provider comparisons.

Virtual Hosts in the Context of Shared Hosting

When you buy a shared hosting plan, the provider creates an Apache virtual host for each domain you add to your account. You don't see or edit those files directly, but all the magic happens there.

Providers using cPanel generate VirtualHost blocks automatically in paths like /etc/apache2/conf/userdata/ or via their own templates. Editing those files directly isn't available on shared hosting — for that you need root access, which is only available on VPS or dedicated servers.

If your project requires advanced Apache configurations — custom modules, complex rewrite rules, server-level security headers — working with a specialized hosting agency can save you hours of configuration work and hard-to-debug errors.

Key Takeaways

  • An Apache virtual host is the configuration block that lets a single server serve multiple domains independently.
  • Name-based virtual hosts are the current standard: they share one IP and distinguish domains using the HTTP Host header.
  • IP-based virtual hosts require one IP per domain and are used in specific legacy or corporate environments.
  • Apache uses the first virtual host as the default when no block matches the requested domain.
  • HTTPS on virtual hosts works thanks to SNI, supported by all modern browsers.
  • In shared hosting, the control panel manages virtual hosts automatically; on a VPS you need root access to edit them.

Need to configure multiple domains or unsure which plan gives you the control you need? The specialists at elenlace.com can guide you to the right hosting type for your project.

FAQ

How many virtual hosts can Apache handle?

Apache imposes no technical limit on the number of virtual hosts. The real limit is the server's RAM and its ability to handle simultaneous connections. In practice, shared hosting servers routinely manage hundreds or thousands of virtual hosts without issue.

Is the .htaccess file part of the virtual host?

Not directly. The virtual host defines whether Apache is allowed to read .htaccess files within its DocumentRoot (via the AllowOverride directive). If enabled, .htaccess extends or overrides the vhost configuration at the directory level, without needing to edit the main configuration file.

Why does my domain show the wrong site after I set up a virtual host?

Almost always one of three reasons: the domain's DNS records don't yet point to the server's IP, the ServerName doesn't exactly match the domain (including or excluding www.), or Apache hasn't reloaded its configuration after the change. Check all three in that order.

Do I need to restart Apache every time I add a virtual host?

A full restart isn't necessary. A configuration reload with apachectl graceful or systemctl reload apache2 is enough — it applies changes without interrupting active connections.

Further reading

Other providers and guides worth comparing:

← All