VMware ESXi and vSphere Cluster Management

Apache Default Virtual Host Configuration on Ubuntu

Learn how Apache's Ubuntu default virtual host works, where its files are stored, what its directives mean, and how to publish and test a website.

Apache HTTP Server is web server software that accepts HTTP requests and returns web content. On Ubuntu and Debian-based systems, Apache commonly starts with a default virtual host: the fallback website configuration used when no more specific virtual host matches a request.

A virtual host is an Apache configuration block representing a website or endpoint. Multiple virtual hosts can share one server and even the same IP address. The default virtual host is useful for a machine hosting one simple website, and it is also a practical template for creating additional sites.

What the Default Virtual Host Does

When a browser requests a hostname and port, Apache compares the request with its enabled virtual-host configurations. If a matching named virtual host is found, Apache serves that site. If no more specific match applies, Apache uses the default virtual host for the relevant address and port.

You can usually leave the default site unchanged when the server hosts only one basic website. Place the site's files in its document root, check the configuration, and reload Apache after configuration changes. For a multi-site server, create separate virtual-host files rather than putting every website into one default file.

Ubuntu Apache Site Configuration Layout

Ubuntu's Apache packaging separates available site definitions from enabled sites:

PathRoleTypical contentsAdministrative action
/etc/apache2/sites-available/Stores site configuration files that may be activated.000-default.conf and custom site files.Create or edit a site definition here.
/etc/apache2/sites-enabled/Contains links to active site configuration files.Links created by a2ensite.Enable or disable sites with a2ensite and a2dissite.
/var/www/html/Usual document root for the Ubuntu default site.index.html and other public web files.Add or replace simple website content.
${APACHE_LOG_DIR}Variable referring to Apache's configured log directory.Access and error logs, commonly under /var/log/apache2/.Read logs when diagnosing requests or configuration problems.

The standard HTTP default-site file is:

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

The file in sites-available defines what a site would look like. A corresponding link in sites-enabled makes that definition active. The numeric prefix in 000-default.conf also helps place the default site early in Apache's configuration order.

The VirtualHost Container

The opening and closing VirtualHost directives surround the settings for one website:

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

*:80 means all local network addresses on port 80. Port 80 is the conventional TCP port for unencrypted HTTP traffic. The directives between the opening and closing tags apply to that virtual host.

DirectiveTypical default valuePurposeWhat to customize
<VirtualHost *:80>All addresses, HTTP port 80Starts the container for one HTTP virtual host.Usually change the address or port only for a specific networking design, such as HTTPS on port 443.
ServerAdminwebmaster@example.com or a package defaultSpecifies an administrative contact that Apache can expose in applicable generated error documents.Use an address monitored by the administrator.
DocumentRoot/var/www/htmlMaps the website's public root to a filesystem directory.Set the directory containing the site's public files.
ErrorLog${APACHE_LOG_DIR}/error.logRecords server and application-related errors for the site.Use a separate path when isolating logs for a site.
CustomLog${APACHE_LOG_DIR}/access.log combinedRecords HTTP requests using the selected log format.Choose a site-specific log path and an appropriate format.
</VirtualHost>NoneEnds the virtual-host container.Keep it present and correctly paired with the opening tag.

Understanding the Log Directives

ServerAdmin is not an authentication or login setting. It is contact information that Apache may include in certain generated error responses.

ErrorLog receives diagnostic entries such as configuration-related or request-processing errors. CustomLog receives request records. The combined format includes normal request information and adds referrer and user-agent fields. A referrer indicates the page that linked to the request, while a user agent identifies the browser or client software.

The Default Document Root

The usual document root for Ubuntu's default site is /var/www/html. Apache maps a request for the site root, such as http://localhost/, to that directory. If an index file such as index.html is present, Apache commonly returns it as the home page.

For a simple website, add or replace files in the document root:

sudo mkdir -p /var/www/html
printf '<h1>Apache is serving this site</h1>\n' | sudo tee /var/www/html/index.html

Apache's service account must be able to traverse the directories and read the files. Website files should normally be readable by Apache without making them unnecessarily writable. Ownership and permission choices depend on how the site is deployed, but a safe principle is to grant only the access required for serving content.

Reviewing and Testing the Default Site

Inspect the Configuration

Open the standard HTTP site file and review its virtual-host block:

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

You may set a suitable administrative contact address and verify that DocumentRoot, ErrorLog, and CustomLog point to the intended locations.

Check the Apache Service and Syntax

sudo systemctl status apache2 --no-pager
sudo apache2ctl configtest

The service-status command shows whether Apache is running. The syntax check validates the configuration without applying a reload. Do not reload Apache when the syntax check reports an error. Correct the reported directive, path, line number, or missing closing tag first.

Reload and Test Locally

After a successful syntax check, apply changes with a graceful reload:

sudo systemctl reload apache2
curl -I http://localhost/

You can also open http://localhost/ in a browser. If an index file exists in the active document root, the response should return that page. The curl -I command requests response headers, so a successful basic setup commonly shows an HTTP success status such as 200 OK.

Enabling, Disabling, and Applying Sites

Editing a file does not immediately change Apache's running configuration. Apache must be reloaded or restarted. A graceful reload is normally preferred because it applies the new configuration while allowing existing connections to finish.

Use Ubuntu's helper commands to activate or deactivate site definitions:

sudo a2ensite example-site.conf
sudo a2dissite 000-default.conf
sudo apache2ctl configtest
sudo systemctl reload apache2

a2ensite enables a configuration from sites-available by creating the appropriate link in sites-enabled. a2dissite disables a site by removing that active link. Enabling another site can change which configuration responds, especially when several sites listen on the same address and port.

Using the Default Configuration for Another Website

Separate configuration files allow separate websites to have their own document roots, hostnames, and logs. Name-based virtual hosting uses ServerName and ServerAlias:

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

ServerName is the primary hostname for the site. ServerAlias lists additional hostnames. When a client sends an HTTP request, its Host header is compared with these names. The matching virtual host handles the request.

If no enabled virtual host matches the requested hostname, Apache can use the first applicable virtual host for that address and port as the fallback. Consequently, enabled-file ordering matters. Choose the fallback deliberately rather than assuming an unmatched hostname will fail.

Example: A Separate Site

  1. Create a directory and a test page.
  2. Create a new file such as /etc/apache2/sites-available/example-site.conf.
  3. Set a unique ServerName and the new DocumentRoot.
  4. Ensure Apache can traverse the directory and read its files.
  5. Enable the site, validate the configuration, and reload Apache.
  6. Test with a hostname that resolves to the server. For local testing, configure local hostname resolution and send a request using that hostname.
sudo mkdir -p /var/www/example
printf '<h1>Example site</h1>\n' | sudo tee /var/www/example/index.html
sudo nano /etc/apache2/sites-available/example-site.conf
sudo a2ensite example-site.conf
sudo apache2ctl configtest
sudo systemctl reload apache2

Inspecting Access and Error Logs

Logs connect an HTTP request to Apache's processing result. Make a request, inspect the access log, then request a page that does not exist:

curl -I http://localhost/
sudo tail -n 50 /var/log/apache2/access.log
curl -I http://localhost/page-that-does-not-exist
sudo tail -n 50 /var/log/apache2/error.log

The access log should contain the request and its status code. A missing page commonly produces a 404 response and may generate a related diagnostic entry in the error log. If a site uses custom log paths, read the files specified by that site's ErrorLog and CustomLog directives.

Troubleshooting

Cannot Connect to Localhost on Port 80

  • Check whether Apache is running with systemctl status apache2.
  • Check listening sockets with ss -ltnp and confirm that something is listening on port 80.
  • Review the Apache error log.
  • Check firewall rules or another service that may be blocking or occupying the port.

Start or repair Apache, resolve any port conflict, and permit HTTP traffic where firewall rules require it.

Apache Refuses to Reload

  • Run sudo apache2ctl configtest.
  • Read the reported error and line number.
  • Check for malformed directives, invalid paths, unsupported directives, or a missing </VirtualHost> tag.

Fix the configuration and rerun the syntax check. Reload only after validation reports success.

The Old Default Page Still Appears

  • Compare the configured DocumentRoot with the location of the new index file.
  • Confirm that Apache was reloaded after changing the document root.
  • Check whether the request is selecting another virtual host.
  • Use curl to distinguish a server response from browser caching or presentation issues.

Response Is 403 Forbidden

  • Check directory traversal and file read permissions.
  • Review applicable Directory access-control directives.
  • Confirm that an acceptable index file exists if directory listing is disabled.
  • Read the error log for the specific denial reason.

Provide safe read and traversal permissions, and avoid making the website files unnecessarily writable.

An Unknown Hostname Shows an Unexpected Site

  • Confirm that the requested hostname matches a configured ServerName or ServerAlias.
  • Confirm that the intended site is enabled.
  • Inspect Apache's parsed virtual-host mapping and the enabled-site ordering.
  • Verify the Host header sent by the client.

Configure the intended hostname explicitly, enable the correct site, and deliberately choose which virtual host should be the fallback.

Exam-Relevant Notes

  • sites-available stores definitions; sites-enabled contains active links.
  • 000-default.conf is the usual Ubuntu HTTP default-site file.
  • DocumentRoot identifies the directory from which public files are served.
  • ErrorLog records errors; CustomLog records requests.
  • apache2ctl configtest should succeed before a reload.
  • a2ensite enables a site and a2dissite disables one.
  • Name-based selection depends on the request's Host header and the configured ServerName or ServerAlias.
  • The first applicable virtual host can handle unmatched hostnames, so ordering can determine the fallback.

For a related reference, see Apache default virtual host configuration.