Apache Configuration Files on Ubuntu
Learn where Apache configuration files are located on Ubuntu, including apache2.conf, modules, listeners, snippets, Virtual Hosts, and enabled directories.
Apache HTTP Server is a web server whose behavior is controlled by text-based configuration files. These files contain directives: instructions that set an option, enable a feature, define a listener, or describe a website.
Ubuntu and other Debian-based systems organize Apache into several coordinated files and directories instead of putting every setting into one large file. This layout makes it easier to enable, disable, and maintain individual modules, snippets, and websites.
The Ubuntu Apache Configuration Directory
The standard Apache configuration directory on Ubuntu is /etc/apache2. It normally contains the primary configuration file, port and startup settings, MIME detection data, and the directories used for modules, shared snippets, and Virtual Hosts.
ls -la /etc/apache2
find /etc/apache2 -maxdepth 2 -type l -ls
| Path or name | Category | Purpose | How it becomes active |
|---|---|---|---|
apache2.conf | Main server configuration | Primary server-wide settings and configuration coordination | Read as Apache's main configuration |
conf-available | Generic snippets | Optional shared configuration fragments | A selected file is linked into conf-enabled |
conf-enabled | Generic snippets | Active shared configuration fragments | Apache reads the enabled symbolic links |
envvars | Startup environment | Environment variables used when Apache starts | Used by the service startup process |
magic | File type detection | Signatures for identifying types from file contents | Used when content-based detection is configured |
mods-available | Modules | Available module loading and configuration files | A module is selected with the Ubuntu module-management mechanism |
mods-enabled | Modules | Active module configuration links | Apache reads the enabled symbolic links |
ports.conf | Listeners | TCP addresses and ports on which Apache listens | Apache processes its Listen directives |
sites-available | Virtual Hosts | Defined but potentially inactive website configurations | A site is linked into sites-enabled |
sites-enabled | Virtual Hosts | Active website configuration links | Apache reads the enabled symbolic links |
How Apache Configuration Is Organized
| Configuration area | Typical scope | Examples of managed behavior |
|---|---|---|
| Main server configuration | Server-wide | Global defaults, access rules, and included configuration files |
| Generic snippets | Reusable server-wide or shared behavior | Security headers, character-set settings, logging defaults, and custom behavior |
| Modules | Features supplied by Apache modules | URL rewriting, proxying, authentication, and scripting integration |
| Listeners | Network entry points | HTTP, HTTPS, and additional TCP ports |
| Virtual Hosts | Individual sites or hostnames | Server names, document roots, logs, and site-specific rules |
| Startup environment | Apache process startup | Environment variables affecting startup and runtime behavior |
| File type detection | Content identification | Inferring a file type from its initial bytes |
apache2.conf: The Main Configuration File
/etc/apache2/apache2.conf is Ubuntu's primary Apache configuration file. It contains server-wide settings and coordinates the rest of the layout by including configuration components from the appropriate enabled directories.
sudo less /etc/apache2/apache2.conf
Apache installations on other platforms commonly use the filename httpd.conf for the main configuration. The name is not a different configuration language; it is a distribution or installation convention. On Ubuntu, start by examining apache2.conf.
Keep changes to the main file deliberate. Reusable settings generally belong in an appropriate file under conf-available, while site-specific settings belong in a Virtual Host file under sites-available.
Available and Enabled Directories
Ubuntu uses paired directories. An *-available directory stores configurations that are installed or prepared but not necessarily active. The corresponding *-enabled directory contains symbolic links to the configurations that Apache should use.
A symbolic link is a filesystem reference that points to another file. In this layout, the link itself marks a configuration as selected without requiring a second copy of the source file.
| Available directory | Enabled directory | Configuration type | Activation relationship |
|---|---|---|---|
conf-available | conf-enabled | Shared configuration snippets | A link in conf-enabled points to a file in conf-available |
mods-available | mods-enabled | Module loading and module-specific settings | A link in mods-enabled points to module files in mods-available |
sites-available | sites-enabled | Virtual Host site definitions | A link in sites-enabled points to a site file in sites-available |
Edit the source file in an available directory, not the symbolic link in an enabled directory. This keeps the configuration's ownership and organization clear. Use Ubuntu's management commands to create or remove the links.
Generic Configuration Snippets
/etc/apache2/conf-available contains optional shared Apache configuration fragments. A fragment can hold reusable directives such as security headers, character-set settings, logging defaults, or custom server behavior.
/etc/apache2/conf-enabled contains links for the fragments currently active. For example:
sudo a2enconf example-feature
sudo a2disconf example-feature
After changing the enabled set, validate the configuration and reload Apache so the running process reads the change.
Apache Modules
An Apache module is a component that supplies a feature. mods-available contains the files describing available modules. These files may include a loading instruction and module-specific directives.
mods-enabled contains links for modules Apache should load and use. A module can be installed on the system but still inactive if its configuration is not enabled. In that situation, directives supplied by the module may be reported as unknown.
sudo a2enmod module_name
sudo a2dismod module_name
Some features therefore require both the module package or files to be present and the module configuration to be active. Always run a configuration test after changing module state.
ports.conf and Listening Ports
/etc/apache2/ports.conf commonly contains Listen directives. The Listen directive tells Apache which address or TCP port should accept connections.
sudo less /etc/apache2/ports.conf
HTTP commonly uses port 80 and HTTPS commonly uses port 443. A nonstandard deployment might add another port:
Listen 8080
A listening port is only the network entry point. Apache still needs a matching Virtual Host definition to provide the site-specific behavior. For example:
<VirtualHost *:8080>
ServerName example.test
DocumentRoot /var/www/example
</VirtualHost>
The port in <VirtualHost> should correspond to a port Apache is listening on. Network firewalls and other access controls must also permit the port.
Virtual Host Site Configuration
A Virtual Host is an Apache configuration unit for a particular website, hostname, or service endpoint. Multiple Virtual Hosts allow one Apache server to host independent sites with separate document roots, host matching rules, logs, and other settings.
Put site definitions in sites-available. Enable only the sites that should be active; their symbolic links appear in sites-enabled.
sudo a2ensite example-site.conf
sudo a2dissite example-site.conf
ls -la /etc/apache2/sites-enabled
A request is selected using information such as the listening address and port and the requested hostname. The enabled Virtual Host must therefore have the appropriate ServerName or hostname matching, a suitable DocumentRoot, and a port that corresponds to the listener.
For two independent websites, create two separate files in sites-available, give each a distinct host-matching configuration and document root, and enable only the intended files. See Create a New Virtual Host and Default Virtual Host for related guidance.
envvars: Apache Startup Environment
/etc/apache2/envvars establishes environment variables for Apache's startup environment. These variables can affect how the process starts and can influence runtime behavior.
Change this file carefully. An incorrect variable or value can prevent Apache from starting or can change the account, paths, or process behavior used by the service. More detail is available in the envvars file guide.
magic and MIME Type Detection
The magic file contains signatures used to identify a file by inspecting its initial bytes. This is content-based detection: the actual beginning of the file is examined.
A MIME type is a media-type label describing content, such as an HTML document or an image. MIME handling can also use filename extensions, such as .html or .png. Extension-based mapping trusts the name; magic-based detection examines content. These are conceptually different methods and may be used for different purposes. See Magic File for more detail.
Safe Configuration Management Workflow
- Back up the file you intend to change and avoid unrelated edits.
- Choose the correct scope: a global setting, reusable snippet, module, listener, or Virtual Host.
- Edit the source file in an available directory when using the available/enabled pattern.
- Enable or disable the component with
a2enconf,a2disconf,a2enmod,a2dismod,a2ensite, ora2dissiteas appropriate. - Validate syntax before applying the change.
- Reload Apache for configuration changes to take effect. Restart only when a restart is required.
sudo apache2ctl configtest
sudo systemctl reload apache2
sudo systemctl restart apache2
A successful syntax test normally reports Syntax OK. A reload applies configuration changes without unnecessarily stopping the service; a restart fully stops and starts it.
Practical Configuration Scenarios
Activate a shared configuration snippet
Create a reusable directive set in /etc/apache2/conf-available/example-feature.conf, enable it, test the configuration, and reload:
sudo a2enconf example-feature
sudo apache2ctl configtest
sudo systemctl reload apache2
Enable a separate website
Place the Virtual Host file in sites-available, enable it, verify the link, test, and reload:
sudo a2ensite example-site.conf
ls -la /etc/apache2/sites-enabled
sudo apache2ctl configtest
sudo systemctl reload apache2
Prepare an additional port
Add the appropriate Listen directive to ports.conf and define a Virtual Host for the same port. For port 8080, both of these concepts must agree:
Listen 8080
<VirtualHost *:8080>
ServerName example.test
DocumentRoot /var/www/example
</VirtualHost>
Troubleshooting Apache Configuration
A site is in sites-available but is not served
- The site may not be enabled.
- The expected symbolic link may be missing or incorrect.
- Apache may not have been reloaded after the change.
- A different Virtual Host may match the hostname and port first.
Inspect sites-enabled, run sudo apache2ctl configtest, and confirm that the request's hostname and port match the intended Virtual Host.
Apache fails to reload
- Check for a syntax error with
sudo apache2ctl configtest. - Review service status and error output.
- Verify that every module required by the configuration is enabled.
- Confirm that site definitions use ports configured with
Listen.
A module directive is unknown
The module providing the directive may be installed but inactive. Check the relevant files in mods-available and links in mods-enabled, enable the required module, and retest.
A Virtual Host on a nonstandard port cannot be reached
Verify the Listen directive, the <VirtualHost> address-and-port declaration, and firewall or network policy. A correct Apache file cannot receive traffic from a blocked port.
An enabled change has no effect
You may have edited the wrong source file, another included file may override the directive, Apache may not have been reloaded, or the symbolic link may point somewhere unexpected. Resolve the link, search the configuration tree for duplicate directives, run the syntax test, and reload.
Exam-Relevant Summary
/etc/apache2is the standard Ubuntu Apache configuration directory.apache2.confis Ubuntu's primary server configuration file;httpd.confis a common main-file name elsewhere.conf-available,mods-available, andsites-availablestore inactive or selectable definitions.conf-enabled,mods-enabled, andsites-enabledcontain symbolic links for active definitions.ports.confdefines listener ports with directives such asListen.- A Virtual Host must be enabled, match the request hostname and port, and point to the intended document root.
envvarscontrols Apache startup environment variables, whilemagicsupports content-based file type identification.- Use
apache2ctl configtestbefore reloading or restarting Apache.
For installation context, see Install Apache on Ubuntu. Related topics include Apache2 Conf File, Ports Conf File, Mods Available Directory, Mods Enabled Directory, Sites Available Directory, and Sites Enabled Directory.