VMware ESXi and vSphere Cluster Management

Create a New Apache Virtual Host

Learn to configure, enable, test, and troubleshoot a name-based Apache virtual host on Debian or Ubuntu.

What an Apache virtual host does

Apache HTTP Server is web server software that receives HTTP requests and serves website files. A virtual host is an Apache configuration block that defines how a particular hostname or website is served.

With name-based virtual hosting, one Apache server can serve multiple websites from one IP address. When a browser requests a URL, it sends the hostname in the HTTP Host header. Apache compares that hostname with each virtual host's ServerName and ServerAlias, then selects the matching configuration.

Request: Host: linux-ub

Apache server: one IP address
  ├── linux-ub       → /var/www/newWebsite
  └── another-site  → /var/www/anotherSite

ServerName is the primary hostname for a virtual host. ServerAlias adds other hostnames that should use the same site, such as www.example.com. If no configured hostname matches the request, Apache uses the default virtual host for that address and port. This is why browsing directly to an IP address often shows the default Apache site.

Prerequisites and Debian/Ubuntu layout

This lesson assumes Apache is installed and running on Debian or Ubuntu. You should also be comfortable with basic Linux commands, sudo, a terminal editor, file permissions, IP addresses, and DNS.

PathPurposeTypical contents
/etc/apache2/sites-availableStores site configuration files that can be enabled.Files such as 000-default.conf and newWebsite.conf
/etc/apache2/sites-enabledContains the enabled-site links Apache loads.Links created by a2ensite
/var/www/newWebsiteExample document root for the new site.index.html and other public website files
/var/log/apache2Stores Apache logs.Access and error logs for configured sites

/etc/apache2/sites-available/000-default.conf is a common HTTP default-site configuration template. It is not automatically the only available configuration, but it is a useful known-working starting point. The a2ensite command enables a configuration by creating the appropriate link in sites-enabled.

Prepare the website document root

DocumentRoot is the filesystem directory from which Apache serves a site's public files. Create the directory before referencing it in the virtual host configuration, then add a simple page with distinctive content.

sudo mkdir -p /var/www/newWebsite
sudo sh -c 'printf "%s\n" "<h1>New Website</h1>" > /var/www/newWebsite/index.html'

The following commands assign ownership to Apache's usual Debian/Ubuntu service account and apply common directory and file permissions.

sudo chown -R www-data:www-data /var/www/newWebsite
sudo find /var/www/newWebsite -type d -exec chmod 755 {} \;
sudo find /var/www/newWebsite -type f -exec chmod 644 {} \;

Apache needs execute permission on directories so it can traverse the path, and read permission on files so it can serve them. Ownership can instead be assigned to a deployment user or group when your hosting workflow requires it, provided Apache still has the necessary access.

Create the site configuration

Copy the default configuration to a descriptive filename in sites-available. Copying a known-working file gives you the correct general structure for an HTTP virtual host while allowing you to replace its site-specific values.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/newWebsite.conf
sudo editor /etc/apache2/sites-available/newWebsite.conf

Replace or adjust the contents so the site remains inside an HTTP <VirtualHost *:80> block:

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName linux-ub
    DocumentRoot /var/www/newWebsite

    ErrorLog ${APACHE_LOG_DIR}/newWebsite-error.log
    CustomLog ${APACHE_LOG_DIR}/newWebsite-access.log combined

    <Directory /var/www/newWebsite>
        Options -Indexes +FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

Change admin@example.com to a monitored administrator address and replace linux-ub with the hostname that will resolve to this server. The Directory block is optional when inherited configuration already provides the required behavior, but it is useful when you need to explicitly define access rules, directory options, or whether .htaccess overrides are allowed.

DirectivePurposeExample valueRequired or optional
ServerAdminAssociates an administrative contact with the site.admin@example.comCommonly included; optional for basic matching
ServerNamePrimary hostname Apache matches for this virtual host.linux-ubEssential for predictable name-based routing
ServerAliasAdds alternative hostnames handled by the same site.www.linux-ubOptional
DocumentRootSets the directory containing public website files./var/www/newWebsiteEssential
ErrorLogRecords errors for this virtual host.${APACHE_LOG_DIR}/newWebsite-error.logStrongly recommended
CustomLogRecords requests using a selected log format.${APACHE_LOG_DIR}/newWebsite-access.log combinedStrongly recommended

Enable, validate, and apply the site

Enable the configuration, test Apache's complete configuration, and only then apply the change.

sudo a2ensite newWebsite.conf
sudo apache2ctl configtest
sudo systemctl reload apache2

A successful configuration test normally reports Syntax OK. The test checks Apache syntax and configuration consistency without applying a broken change. If it reports an error, correct the referenced file and line before reloading.

A graceful reload asks Apache to reread its configuration while allowing existing requests to finish. It is normally the preferred choice for a configuration-only change. A restart stops and starts the service, which can interrupt active connections, but may be useful after certain service-level changes or when a reload cannot complete.

sudo systemctl restart apache2
CommandPurposeWhen to use it
a2ensite newWebsite.confEnables a site configuration by creating its enabled-site link.When adding or re-enabling a site
a2dissite newWebsite.confDisables a site configuration.When a site is no longer needed or must be taken out of service
apache2ctl configtestValidates Apache syntax and configuration.Before every reload or restart
apache2ctl -SLists Apache's recognized virtual hosts and matching order.When verifying host selection or diagnosing conflicts
systemctl reload apache2Gracefully applies configuration changes.After a successful configuration test
systemctl restart apache2Stops and starts Apache.When a full restart is required; expect possible interruption

Make the hostname resolve to the server

The configured ServerName must resolve to the server's IP address. This can use public DNS, private DNS, or a temporary entry in a client's hosts file. Apache configuration alone does not make a domain resolve.

Before public DNS is available, add a mapping to the hosts file on the machine that will make the test request. On Linux and macOS this is usually /etc/hosts; on Windows it is commonly C:\Windows\System32\drivers\etc\hosts. Use the real server address, not the documentation address shown here:

192.0.2.10 linux-ub

After saving the temporary mapping, the hostname should resolve locally. Remove it when authoritative DNS is configured and working normally. For production, create the appropriate DNS record so the domain points to the web server's address.

Verify name-based routing

Use a browser

Visit the configured hostname, for example http://linux-ub/, and confirm that the page displays New Website. If you browse to http://server-ip/ instead, Apache may select the default virtual host because the request hostname does not match ServerName.

Use curl

You can test routing locally without DNS by sending an explicit Host header:

curl -H 'Host: linux-ub' http://127.0.0.1/

Once the hostname resolves through DNS or a hosts-file entry, test it directly:

curl http://linux-ub/

List Apache's virtual hosts

sudo apache2ctl -S

Check that the desired hostname is listed for port 80 and points to newWebsite.conf. The output also helps reveal which virtual host is the default and whether another configuration has a conflicting hostname.

Troubleshooting

Apache reports a syntax error or will not reload

  • Run sudo apache2ctl configtest.
  • Read the referenced filename and line number.
  • Check for misspelled directives, invalid paths, and unclosed VirtualHost or Directory blocks.
  • Correct the configuration and run the test again before reloading.

The default Apache site appears

  • Run sudo apache2ctl -S and confirm the new site is enabled and listed.
  • Confirm the requested hostname exactly matches ServerName or a ServerAlias.
  • Check that DNS or the hosts file points the hostname to the expected server.
  • Enable the site with sudo a2ensite newWebsite.conf and reload Apache if necessary.
  • Remember that direct IP requests commonly select the default host.

The site returns 403 Forbidden

  • Check directory and file permissions along the entire path to the document root.
  • Confirm the Directory block permits access with Require all granted where appropriate.
  • Inspect the site's error log for the exact permission or access denial.

The site returns 404 Not Found

  • Confirm that DocumentRoot points to /var/www/newWebsite.
  • Verify that /var/www/newWebsite/index.html exists.
  • Check that the requested filename and path are correct.
  • Review the access and error logs for the requested filesystem path.

The domain cannot be reached by name

  • Check whether a DNS record exists and whether it returns the web server's IP address.
  • Consider DNS caching or propagation delays after a record change.
  • Use a temporary hosts-file entry or the curl Host-header test to separate DNS problems from Apache problems.
  • Confirm that port 80 is reachable and that firewall or network rules allow HTTP traffic.

Site lifecycle and HTTPS next steps

Disable a site without deleting its configuration by running:

sudo a2dissite newWebsite.conf
sudo apache2ctl configtest
sudo systemctl reload apache2

For a production domain, configure HTTPS with TLS certificates. An HTTP virtual host listens on port 80, while HTTPS uses a separate virtual host on port 443, usually with certificate and private-key directives. A common production design redirects the port 80 virtual host to the HTTPS virtual host after TLS is configured.

For related guidance, continue with Apache virtual host configuration.