October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk10 min

How to Host Several Domains on One Apache Server with Virtual Hosts

A practical guide to serving several domains from one Apache server and IP, with DNS setup, virtual-host blocks, HTTPS certificates, verification commands, and troubleshooting.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

You can serve several domains from one Apache server and one IP address by using name-based virtual hosting. Point every domain’s DNS records at that server, then create a separate <VirtualHost> block for each independent website. Apache examines the request hostname and selects the matching ServerName or ServerAlias.

A single block can serve multiple names only when they are alternate names for the same site. Independent sites need separate blocks and document roots. HTTPS adds a matching *:443 configuration, certificate coverage, and TLS SNI considerations.

As an Amazon Associate I earn from qualifying purchases.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What “several domains on one virtual host” actually means

The usual goal is to host multiple domains on one Apache installation, server, or public IP. Apache calls each site definition a virtual host. You do not normally place unrelated websites in one block. Instead, define one name-based <VirtualHost> block per distinct site and let Apache choose among them by hostname.

For example, example.com and example.net can share an IP while serving different files. Conversely, www.example.com and example.com usually belong in one block through ServerAlias because they are alternate names for the same content.

The Apache Software Foundation describes name-based hosting as “usually simpler.” The method scales because the IP address identifies the server, while the hostname identifies the site.

How Apache chooses the site

  1. The client resolves the domain name through DNS and connects to an IP address and port.
  2. Apache groups configured virtual hosts by that address and port, such as *:80 for HTTP or *:443 for HTTPS.
  3. For HTTP, Apache reads the request’s Host header. For TLS connections, it can use the SNI hostname supplied during the TLS handshake, followed by the HTTP hostname.
  4. Apache compares that name with each block’s ServerName and ServerAlias.
  5. The matching block supplies the document root and other site settings.

Configuration order matters. For a particular IP-and-port group, the first listed virtual host becomes the fallback when no ServerName or ServerAlias matches. A typo, missing alias, or request sent to an unexpected hostname can therefore display the first site in that group.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

DNS must be correct before Apache can be tested

Apache configuration does not create DNS records. Add an A record for each IPv4 name and an AAAA record when the server is reachable over IPv6. Use the names that visitors will actually request, including any www alias you intend to serve.

DNS preparation checklist

  • List every hostname: apex names such as example.com, subdomains, and www names.
  • Point each name to the server’s intended IPv4 address with an A record and, where applicable, to its IPv6 address with an AAAA record.
  • Remove stale records that still point to an old server or service.
  • Check that public DNS returns the intended address before diagnosing Apache.
  • Remember that DNS propagation and resolver caching can make a change appear gradually rather than immediately.

On a Unix-like system, a query such as dig +short example.com shows the returned addresses. On Windows, nslookup example.com provides the equivalent basic check. The result should match the address on which Apache is listening.

Choose the right layout before writing configuration

Decide whether the names represent independent sites or merely alternate names. This choice determines document roots, redirects, logs, access rules, and certificates.

Situation Recommended layout Reason
Two unrelated websites Separate <VirtualHost> blocks and separate document roots Files, permissions, deployments, and failures remain isolated.
One site with several public names One block with one ServerName and additional ServerAlias values All names intentionally serve the same content and policy.
One preferred canonical name Serve the canonical name and redirect alternate names explicitly Visitors, search engines, cookies, and links converge on one hostname.
HTTP and HTTPS for a site Usually one *:80 block and one *:443 block Each listening port needs its own matching virtual-host definition.
Strict tenant or project separation Separate roots, logs, users or permissions, and access controls A mistake in one site is less likely to expose another site’s files.
Small set of closely related names Shared content and, where appropriate, shared logging Less configuration overhead, but incidents are harder to attribute to one name.

Do not use a shared document root merely because domains share a server. Shared content is a deliberate application decision; it is not required for name-based hosting.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Build the HTTP virtual hosts

The minimal configuration requires an explicit ServerName and a DocumentRoot in every name-based block. Add ServerAlias only for names that should reach that same site.

1. Create a directory for each site

Use distinct paths and place a test file in each one so a hostname can be identified during setup:

mkdir -p /var/www/example /var/www/example-net
echo 'example.com site' > /var/www/example/index.html
echo 'example.net site' > /var/www/example-net/index.html

Those paths are examples; use directories whose ownership and permissions match your operating system and deployment process.

2. Define one block per independent domain

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

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

The first block serves requests for example.com and www.example.com. The second serves the two example.net names. A request for an unlisted hostname on port 80 goes to the first block in this address-and-port group, so do not rely on an accidental default.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

3. Put the configuration where your distribution loads it

Apache installations differ in how they include site files. Debian- and Ubuntu-family systems commonly keep individual files under /etc/apache2/sites-available/ and enable them with a2ensite; other systems may include files from a central configuration directory. Follow the include structure already used by your installation rather than copying a path blindly.

4. Check syntax and reload

Run a configuration test before reloading. A successful test means Apache can parse the files; it does not prove that DNS, permissions, or certificates are correct. Reload using the service mechanism provided by your operating system, then test each hostname.

Use aliases deliberately

ServerAlias is for an additional name that should enter the same virtual host. Typical examples are an apex name and its www form:

ServerName example.com
ServerAlias www.example.com shop.example.com

If an alias should not serve the same content, create another virtual host instead. If the alternate name should permanently lead to a preferred name, configure an HTTP redirect in that alias’s site rather than silently serving duplicate content. The alias declaration itself does not create a redirect; it only makes Apache map the name to the block.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Add HTTPS with matching port-443 hosts

HTTP and HTTPS are separate address-and-port groups. For every HTTPS name you intend to serve, create a corresponding <VirtualHost *:443> block and select a certificate strategy that covers the requested names.

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example
    SSLEngine on
    SSLCertificateFile /etc/ssl/example/fullchain.pem
    SSLCertificateKeyFile /etc/ssl/example/privkey.pem
</VirtualHost>

<VirtualHost *:443>
    ServerName example.net
    ServerAlias www.example.net
    DocumentRoot /var/www/example-net
    SSLEngine on
    SSLCertificateFile /etc/ssl/example-net/fullchain.pem
    SSLCertificateKeyFile /etc/ssl/example-net/privkey.pem
</VirtualHost>

The certificate paths above are illustrative; use the files issued and stored by your certificate-management process. Ensure that each certificate includes the exact DNS names clients will request, either as individual names or through an appropriate wildcard. Renewal is an operational task separate from virtual-host mapping.

Why SNI matters

Before encrypted HTTP headers are available, a TLS client can send the requested hostname as Server Name Indication (SNI). Apache uses that hostname to select among matching *:443 hosts and present the corresponding certificate. A missing or incorrect SNI name, an absent port-443 block, or a certificate that does not cover the name can produce a certificate warning or the wrong site even when the port-80 configuration is correct.

Decide how HTTP should behave

Some sites serve content on both ports; others use the port-80 block only to redirect to HTTPS. Whichever policy you choose, define it for every public name so that an unlisted alias does not fall through to an unrelated default host.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Separate logging and access controls when sites are independent

Virtual hosts can share Apache’s global settings, but independent sites usually benefit from their own access and error logs. Per-site logs make it possible to identify which hostname generated a request and to troubleshoot one application without mixing its traffic with another.

Keep filesystem permissions scoped to each document root. Review directory access rules, application configuration files, uploaded content, and symbolic links so that one site cannot read another site’s private data. If several names intentionally share an application, document that relationship and apply the same authentication, headers, and caching policy consistently.

Verify the parsed configuration, not just the files you edited

Use Apache’s virtual-host dump to see what the running configuration actually contains:

apachectl -S

On Windows installations, the equivalent command is:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
httpd.exe -S

Inspect the output for each address-and-port group, the order of the hosts, every ServerName and ServerAlias, and the host Apache identifies as the default. This catches a file that was never included, a duplicate name, a typo, or a block loaded earlier than expected.

Test the hostname and the content together

After DNS resolves to the server, request each name and check that the response identifies the intended document root. A simple HTTP test is:

curl -i http://example.com/
curl -i http://example.net/

For a temporary test before DNS has propagated, you can send an explicit Host header to the server’s address:

curl -i -H 'Host: example.com' http://203.0.113.10/

Replace the example address with the server’s actual IPv4 address. This test exercises Apache’s hostname matching while bypassing the local resolver; it does not validate the public DNS record.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Test HTTPS separately

Request each HTTPS name and inspect the certificate and returned content. When testing an address directly, preserve the hostname through SNI with a hostname-aware client option rather than connecting only by numeric address. A valid certificate for one name does not prove that every other name has a matching certificate and virtual host.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why Apache shows the wrong site

The domain resolves to another server

If DNS returns an old address, Apache on the intended machine never receives the request. Compare the public A and AAAA answers with the server’s addresses, and check both records when clients may prefer IPv6.

The name is missing from Apache

Confirm that the requested spelling appears exactly in ServerName or ServerAlias. A hostname omitted from the block is unmatched and can land on the first host for that port.

The edited file is not loaded

Run apachectl -S or httpd.exe -S. If the block does not appear, correct the include or site-enabling step and reload Apache after a successful syntax test.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The request uses a different port

An HTTP request reaches the *:80 group; an HTTPS request reaches *:443. Adding a correct port-80 block does not configure HTTPS. Create and verify the corresponding port-443 host.

The TLS certificate belongs to another name

For HTTPS, check the SNI name sent by the client, the order and contents of the *:443 blocks, and the certificate’s name coverage. A certificate mismatch can make a correctly routed site appear broken.

A default host is masking the mistake

Because the first host in an address-and-port group is the fallback, a missing alias may consistently display the same unrelated site. Treat that page as evidence of an unmatched hostname, not as proof that DNS is correct.

Common configuration mistakes to avoid

  • Expecting Apache to create DNS: virtual-host files do not publish records.
  • Putting two unrelated roots in one block: one DocumentRoot serves that block; use separate blocks for separate sites.
  • Using an alias as a redirect: ServerAlias maps names; it does not issue an HTTP redirect.
  • Configuring only port 80: HTTPS requires its own port-443 mapping and certificate setup.
  • Ignoring configuration order: an unmatched request uses the first host in its group.
  • Testing only by IP address: an IP-only request does not represent the hostname-based selection visitors use.
  • Mixing certificates and names: every HTTPS hostname must be covered by the certificate presented for its selected host.
  • Sharing permissions casually: a common server does not require common filesystem access.

A repeatable deployment checklist

  1. Write down every public hostname and decide whether it is an independent site, an alias, or a redirect target.
  2. Create DNS A and, where needed, AAAA records that point to the Apache server.
  3. Create and permission a document root for each independent site.
  4. Add one *:80 block per site with an explicit ServerName and DocumentRoot.
  5. Add only intentional ServerAlias values.
  6. Define the HTTPS policy, then add matching *:443 blocks and certificates for every HTTPS name.
  7. Run the configuration syntax check and reload Apache.
  8. Run apachectl -S (or httpd.exe -S) and verify names, ports, file order, and fallback hosts.
  9. Check DNS resolution, then test each hostname over HTTP and HTTPS with a hostname-aware client.
  10. Review logs, permissions, redirects, and certificate renewal for each independent site.

When one IP is no longer the right constraint

Name-based virtual hosting is appropriate when several domains can reach the same Apache listener and can share the server’s operational boundaries. Separate IP addresses may still be required by a legacy application, a network policy, or a service that cannot use hostname-based selection. Those cases are exceptions to the name-based pattern, not prerequisites for ordinary multi-domain hosting.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.