Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
VGSources
Blog

How to Create a Subdomain in Apache: DNS and Virtual Host Setup

A subdomain in Apache needs both a DNS record and a matching name-based virtual host. This guide covers the complete setup, validation, aliases, default-vhost behavior, and troubleshooting.
WorldApache: DNS and Virtual Host Setup Length9 min Posted Quest giverVGSources Team
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Creating blog.example.com in Apache requires two separate configurations: DNS must direct the hostname to the server, and Apache must select a name-based virtual host whose DocumentRoot contains the subdomain’s files. A virtual-host file cannot create a public DNS record by itself. Once both layers are configured, verify Apache’s parsed map with apachectl -S on Unix-like systems or httpd.exe -S on Windows, then test the hostname.

What a subdomain does—and what Apache does not do

A subdomain is a hostname under an existing domain, such as blog.example.com under example.com. The browser first resolves that hostname through DNS. It then opens a connection to the resulting server address and sends the hostname in the HTTP request. Apache examines that hostname and chooses the matching <VirtualHost> block.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Layer Responsibility What to verify
DNS Maps the subdomain to the server address. A lookup such as dig blog.example.com returns the intended address.
Apache Matches the request hostname to ServerName or ServerAlias and serves a directory. apachectl -S or httpd.exe -S shows the expected virtual host and configuration file.
Filesystem Stores the files Apache returns for that host. The configured document root exists, contains the expected index file, and is readable by Apache.

These jobs are independent. Adding a virtual host does not publish a DNS record, and publishing a DNS record does not tell Apache which directory to serve. A missing DNS record prevents the request from reaching the intended server; a missing or mismatched virtual host can send a valid request to another site on that server.

Decide the layout before changing Apache

You need control of the domain’s DNS, access to the Apache installation, and a directory dedicated to the new hostname. Confirm which address and port Apache listens on, because the address and port in the virtual-host declaration must match the listener that receives the request.

Most subdomains use name-based virtual hosting. Several hostnames share one IP address, and Apache distinguishes them by the hostname supplied in each request. IP-based hosting is a different design in which each virtual host needs its own IP-and-port combination.

Design How Apache distinguishes sites When it fits
Name-based hosting One address and port can serve several hostnames; Apache compares the request name with each block’s ServerName and ServerAlias. The usual choice when multiple subdomains share a server address.
IP-based hosting Each virtual host is associated with a distinct IP-and-port combination. Only when the environment requires separate addresses or ports.

Step 1: Create the DNS record

At the DNS provider, create an A record for the hostname, for example:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Name: blog (some providers display this as the complete name blog.example.com)
  • Type: A
  • Value: the server’s public IP address

Use the DNS record type appropriate to your environment if the server is addressed differently. The essential result is that blog.example.com resolves to the address where Apache is listening. DNS changes may not be visible immediately because resolvers retain earlier answers for their configured cache period, so check resolution rather than assuming the record is active.

From a machine that should reach the site, run:

dig blog.example.com

Use an equivalent DNS lookup utility when dig is unavailable. The answer should contain the intended server address. If it does not, fix DNS before changing Apache; no virtual-host directive can compensate for a hostname that resolves elsewhere.

For a test from one computer only, you can add the hostname to that computer’s hosts file and point it at the server address. This changes name resolution on that machine; it does not publish the subdomain to the Internet, so every other client still needs the real DNS record.

Step 2: Create a document directory and test file

Choose a directory used only by the subdomain, such as /var/www/blog, and place an index.html file in it. A simple test page can contain the hostname and a short message so that you can distinguish it from the main site.

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

The exact path and permission commands depend on the operating system and Apache package. Check that every directory component can be traversed by the Apache worker and that the index file is readable by it. A correct virtual host pointing at a nonexistent or unreadable directory cannot serve the intended page.

Step 3: Add a name-based virtual host

Create a virtual-host file using the configuration mechanism provided by your Apache installation. This minimal HTTP example gives the subdomain its own document root and separate logs:

<VirtualHost *:80>
    ServerName blog.example.com
    DocumentRoot /var/www/blog
    ErrorLog ${APACHE_LOG_DIR}/blog-error.log
    CustomLog ${APACHE_LOG_DIR}/blog-access.log combined
</VirtualHost>

How each directive controls the result

  • <VirtualHost *:80> limits this block to requests received on port 80 for any local address represented by the asterisk. Use the address and port that match your listener.
  • ServerName blog.example.com is the primary hostname Apache compares with the request hostname. Set it explicitly on every virtual host.
  • DocumentRoot /var/www/blog identifies the directory from which Apache serves the subdomain’s files.
  • ErrorLog records errors for this host, while CustomLog records requests using the shown combined format. The APACHE_LOG_DIR variable is commonly supplied by packaged installations; follow your package’s logging conventions if that variable is not defined.

The block needs, at minimum, a ServerName and a DocumentRoot. Add a ServerAlias only when another hostname is intentionally supposed to serve the same content:

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

Do not add aliases merely to make an unplanned name work. Every alias a user enters must also resolve through DNS (or through a deliberate hosts-file entry during local testing). Apache can match wildcard aliases, but a wildcard in the configuration does not create wildcard DNS records.

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

Step 4: Put the file where this Apache package loads it

Apache installations do not all use the same directory layout. Debian-family packages commonly separate files into sites-available and sites-enabled; other packages include virtual-host files from a different directory or from the main configuration. Identify the include path used by the installed package and place the file where that package reads it.

There is no universal enable command that is safe to copy across operating systems. Use the package’s normal mechanism to include or enable the file, then verify that Apache actually reads it. A file sitting in an unused directory has no effect, even if its syntax is perfect.

Step 5: Inspect Apache’s parsed virtual-host map

Before reloading, ask Apache to show how it interpreted the configuration:

apachectl -S

On Windows, use:

httpd.exe -S

The output is more useful than looking at filenames alone. Confirm that it lists blog.example.com, the expected address and port, and the file containing the block. It also reveals which virtual host is first for each address-and-port pair. If your new host is absent, the file was not included, the name is misspelled, or Apache rejected the configuration before building its map.

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

Step 6: Validate and reload Apache

Run the configuration-test command supplied by your operating system’s Apache package. Fix every reported syntax or path error before applying the change. Then use that package’s service-management procedure to reload Apache so the running process adopts the validated configuration.

The exact test and reload commands vary by distribution, installation method, and Windows service setup. Do not substitute a command documented for a Debian package into an unrelated installation. If the reload fails, keep the previous working configuration in place, read the reported error, correct the indicated file, test again, and reload only after the test succeeds.

Step 7: Request the hostname and verify the result

Open http://blog.example.com/ in a browser or make an equivalent HTTP request. The response should be the index.html from /var/www/blog, not the page from another virtual host. Check the subdomain’s access log for the request and its error log for filesystem or configuration problems.

If the wrong site appears, do not immediately move files around. Work through the routing chain in order:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Resolve blog.example.com and confirm that it returns the intended server address.
  2. Check the spelling of the browser’s hostname against ServerName and every ServerAlias.
  3. Confirm that the request’s address and port match the <VirtualHost> declaration.
  4. Run apachectl -S or httpd.exe -S and verify that the file is loaded.
  5. Confirm that the document root exists, contains the expected file, and is readable by Apache.
  6. Review virtual-host order if no configured name matches.

Why Apache may show the wrong website

For a given address and port, Apache compares the request hostname with the ServerName and ServerAlias values in the matching virtual-host set. When no name matches, Apache uses the first virtual host listed for that address and port. That first block is therefore the default for unknown, misspelled, or missing hostnames.

Make the default intentional by placing the desired fallback first, and define ServerName on every virtual host. This does not make an unknown hostname valid; it simply determines which site receives requests that do not match a configured name.

A client that omits the Host header, sends an unknown name, or reaches the wrong address can consequently receive the primary site. Testing by IP address alone is not a valid test of a name-based subdomain, because the IP address does not identify which hostname you intend to serve.

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

Aliases, wildcards, and shared content

Use aliases deliberately

ServerAlias is appropriate when two or more names should return the same files, such as a canonical name and an alternate spelling. It is not a substitute for a second document root. If two hostnames need different content, give them separate virtual-host blocks and separate directories.

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.

Wildcards still require DNS

Apache supports wildcard names in aliases, which can reduce repetitive configuration when many names intentionally share content. DNS remains a separate requirement: each requested name must resolve to the server, whether through individual records or a DNS wildcard supported by your provider. An Apache wildcard cannot answer a name that never reaches the server.

Troubleshooting by symptom

Symptom Likely layer Checks and corrective action
The hostname does not resolve. DNS Run dig blog.example.com. Correct the A record or the environment-appropriate DNS record and allow for resolver caching.
The hostname resolves to another server. DNS or hosting topology Compare the lookup result with the address where Apache listens. Correct the record or the intended server assignment before editing Apache.
The main site appears instead of the subdomain. Apache name matching Check the exact hostname, ServerName, aliases, address and port, loaded-file status, and the order shown by apachectl -S or httpd.exe -S.
The new host is absent from the parsed map. Apache loading Place the file in the package’s included directory or enable it through that package’s normal mechanism, then rerun the parsed-configuration command.
The expected page is not in the response. Filesystem or document root Confirm that DocumentRoot points to the intended directory, that index.html exists there, and that Apache can read the path.
Apache refuses to reload. Configuration syntax or paths Run the installation’s configuration-test command, correct each reported error, and retry the reload only after validation passes.
A local test works but another network does not. Local name override or DNS Check whether the successful machine uses a hosts-file entry. That entry affects only that machine and does not publish the subdomain.
An unknown hostname receives a valid site. Default virtual host Expected behavior when no name matches: Apache serves the first vhost for the address and port. Make that fallback intentional and correct the requested hostname or DNS.

A repeatable verification checklist

  1. The DNS record for the subdomain points to the address serving Apache.
  2. A DNS lookup from the intended client returns that address.
  3. The dedicated document directory exists and contains the expected index file.
  4. Apache can traverse the directory and read its files.
  5. The virtual-host block has the correct address and port.
  6. ServerName exactly matches the hostname in the request.
  7. Any ServerAlias values are intentional and have corresponding DNS resolution.
  8. The file is included by the installed Apache package.
  9. apachectl -S or httpd.exe -S shows the expected name, file, address and port.
  10. The package’s configuration test succeeds.
  11. Apache has been reloaded using the package’s normal service procedure.
  12. The browser request returns the subdomain’s test file and appears in the expected logs.

Scope of this example

The configuration shown is an HTTP virtual host on port 80. It establishes DNS routing, name-based selection, a document root, and logging. HTTPS requires its own listener and certificate-related configuration, which should be designed and validated separately rather than assumed to be enabled by this HTTP block.

Once DNS resolves the name, Apache loads the virtual-host file, and the parsed map shows the intended match, the subdomain is functioning as a distinct Apache site. Future changes should preserve that separation: change DNS when the destination address changes, change the virtual host when hostname routing or content location changes, and use the logs and parsed configuration to identify which layer produced an unexpected result.

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.

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

Leave a Reply

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

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.

More quests from Patch Notes

  1. How to Set Up a RedM (RDR2) Server in 2026: License Key, txAdmin and server.cfg, Step by StepBlog11min
  2. Best RedM (RDR2) Server Hosting in 2026: Comparing Four Hosts on Slots, Memory and PriceBlog10min
  3. How to Host a Mindustry Server in 2026: server-release.jar, Port 6567 and the Commands That MatterBlog7min
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

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.