Apache HTTP Server course

Apache sites-enabled Directory: Enable and Disable Virtual Hosts

Learn how Debian and Ubuntu Apache use /etc/apache2/sites-enabled, a2ensite, and a2dissite to activate, disable, verify, and troubleshoot virtual hosts.

On Debian- and Ubuntu-based systems, /etc/apache2/sites-enabled/ is the directory containing the Apache site configurations that are currently enabled. These configurations usually define virtual hosts: Apache blocks that serve a particular website, hostname, address, or port.

A configuration file existing in /etc/apache2/sites-available/ does not make the site active by itself. It must first be enabled, normally with a2ensite. After the change, Apache must be reloaded or restarted before it uses the updated configuration.

What is the sites-enabled directory?

The /etc/apache2/sites-enabled/ directory contains the site definitions that Debian-family Apache installations include in the active configuration. The main Apache configuration uses an IncludeOptional-style directive to load entries from this directory.

Most entries are symbolic links. A symbolic link is a filesystem reference that points to another file. For example:

/etc/apache2/sites-enabled/newWebsite.conf -> /etc/apache2/sites-available/newWebsite.conf

Apache reads the enabled entry, follows the link to the source file, and obtains the virtual host definition from the file in sites-available.

Relationship between sites-available and sites-enabled

/etc/apache2/sites-available/ stores site configuration files that are available to be enabled. /etc/apache2/sites-enabled/ identifies which of those site definitions are active.

LocationContentsWhether Apache loads it as an enabled siteTypical administration action
/etc/apache2/sites-availableOriginal site configuration filesNot normally, merely because the files exist thereCreate, edit, or keep a site configuration for later use
/etc/apache2/sites-enabledUsually symbolic links to available site filesYes, when the directory is included by the Apache configurationEnable or disable site definitions

This separation lets an administrator keep a configuration on disk without making it active. Disabling a site normally removes only its symbolic link; the source file remains in sites-available and can be enabled again later.

How Apache activates a site

  1. Create or place a valid virtual host file in /etc/apache2/sites-available/.
  2. Enable that file, normally with a2ensite. This creates the corresponding symbolic link in sites-enabled.
  3. Check the complete Apache configuration with apache2ctl configtest.
  4. Reload Apache so it reads the changed set of enabled sites.

A typical virtual host might contain:

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

ServerName identifies the primary hostname. ServerAlias lists additional hostnames that should use the same virtual host. The file becomes active only when its site configuration is represented in sites-enabled and Apache has loaded the change.

Enable a site with a2ensite

a2ensite is the standard Debian/Ubuntu helper utility for enabling an Apache site configuration. Assume that /etc/apache2/sites-available/newWebsite.conf already contains a valid virtual host.

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

The first command creates the appropriate symbolic link. It is preferable to manually creating links because it uses the expected Debian-family Apache layout and reports common problems such as a missing site file.

A successful configuration test normally prints:

Syntax OK

Always test before reloading. Enabling a file changes what Apache will load, but it does not apply the change to a running process until a reload or restart occurs.

Disable a site with a2dissite

a2dissite disables an enabled site. It normally removes the active symbolic link while preserving the original configuration in sites-available.

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

Because the source file remains available, you can enable it again later:

sudo a2ensite newWebsite.conf
GoalCommandResultFollow-up action
Enable a sitesudo a2ensite newWebsite.confCreates a link in sites-enabledRun apache2ctl configtest, then reload
Disable a sitesudo a2dissite newWebsite.confRemoves the active linkRun apache2ctl configtest, then reload
Apply a valid changesudo systemctl reload apache2Apache rereads configuration without a full stop and startVerify the virtual host and website response

Safe verification workflow

Use these commands to inspect the enabled site set, validate syntax, and understand Apache's virtual host selection:

CommandWhat it verifiesExpected result
ls -l /etc/apache2/sites-enabled/Lists enabled entries and their link targetsEntries commonly show -> followed by a file in sites-available
sudo apache2ctl configtestChecks Apache configuration syntaxSyntax OK when the configuration is valid
sudo apache2ctl -SShows recognized virtual hosts, name mappings, ports, and defaultsA report identifying the loaded virtual hosts and default host
sudo systemctl reload apache2Applies validated configuration changesApache continues serving while rereading its configuration

To inspect a link manually, run:

ls -l /etc/apache2/sites-enabled/

Confirm that the target file exists in sites-available and contains the expected VirtualHost, ServerName, ServerAlias, and DocumentRoot settings.

Reload versus restart

A reload asks the running Apache service to reread its configuration while avoiding a full service stop and start. Existing connections are generally handled more smoothly, so reload is the normal choice after enabling or disabling a site.

A restart stops and starts the service. It may be used when a reload is insufficient, when troubleshooting service state, or when an installation specifically requires a complete process restart. Test the configuration first in either case.

Default virtual host considerations

With name-based virtual hosting, Apache compares the requested hostname with the ServerName and ServerAlias values of enabled virtual hosts. If no enabled host matches, Apache uses the default virtual host for the requested address and port.

Enabled configuration ordering can affect which virtual host becomes the default. Numbered filenames such as 000-default.conf are commonly placed early so that the intended default is clear. Review the names and ordering of files in sites-enabled, and use apache2ctl -S to see Apache's interpretation rather than relying only on filenames.

If the wrong website appears, check all of the following:

  • The intended configuration is present in sites-enabled.
  • The request hostname exactly matches a ServerName or ServerAlias.
  • DNS or local host resolution points to the expected server.
  • The default virtual host and enabled-file ordering are what you expect.

Practical example: enabling a newly created virtual host

Suppose /etc/apache2/sites-available/newWebsite.conf contains a valid definition for example.com. Enable and verify it as follows:

sudo a2ensite newWebsite.conf
ls -l /etc/apache2/sites-enabled/
sudo apache2ctl configtest
sudo apache2ctl -S
sudo systemctl reload apache2

The listing should show a relationship similar to:

/etc/apache2/sites-enabled/newWebsite.conf -> /etc/apache2/sites-available/newWebsite.conf

The -S output should include the virtual host and its hostname mapping. The reload then applies the validated configuration.

Practical example: disabling an old website

To stop serving an old site without deleting its configuration:

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

The enabled link is removed, but /etc/apache2/sites-available/oldWebsite.conf normally remains. You can restore the site later with a2ensite oldWebsite.conf.

Troubleshooting

The file exists in sites-available but the website is not active

  • Run sudo a2ensite filename.conf using the exact filename.
  • Confirm that the link appears with ls -l /etc/apache2/sites-enabled/.
  • Run sudo apache2ctl configtest.
  • Reload Apache after the test reports success.

a2ensite cannot find the site configuration

The supplied name must match a file in /etc/apache2/sites-available. List the directory and check spelling, capitalization, and the file extension:

ls -l /etc/apache2/sites-available/

Apache fails to reload

Run:

sudo apache2ctl configtest

Correct the reported file and line. Common causes include syntax errors, invalid directives, missing modules, conflicting ports, and invalid file paths. If more detail is needed, inspect service status and Apache error logs. Do not repeatedly reload until the configuration test succeeds.

A symlink in sites-enabled is broken

A broken link usually means that its source file was renamed, moved, or deleted. Inspect the target with ls -l. Restore the expected source file, or disable the old entry and enable the correctly named configuration.

The request reaches the default site

  • Use sudo apache2ctl -S to inspect defaults and hostname mappings.
  • Verify the intended site is enabled.
  • Check ServerName and ServerAlias for spelling and matching hostnames.
  • Confirm DNS or local host resolution points to this Apache server.
  • Review the order of enabled virtual host filenames, including files such as 000-default.conf.

Distribution and installation differences

sites-available, sites-enabled, a2ensite, and a2dissite are Debian/Ubuntu conventions, not universal Apache features. Other operating systems and installation methods may load virtual hosts from different directories or directly from the main Apache configuration.

Before applying these commands on another platform, identify the installed Apache layout and inspect its main configuration for the relevant Include or IncludeOptional directives.

Related Apache topics