How to Include Multiple Apache Config Files?
Managing a growing web server can become chaotic if all directives
are crammed into a single file. Fortunately, Apache allows you to split
your configuration into smaller, modular files and load them dynamically
into the main configuration. This article provides a quick overview of
how to use the Include and IncludeOptional
directives, how to organize your configuration directory structure, and
best practices for testing your changes without bringing down your live
server.
The Include Directives
Apache provides two primary directives to reference external
configuration files within your main httpd.conf or
apache2.conf file.
Include: Tells Apache to load specific files or directories. If the specified file or path does not exist, Apache will throw a syntax error and fail to start or reload.IncludeOptional: Functions exactly likeInclude, but with one key difference: if the target file or directory does not exist or contains no matching files, Apache will ignore it and continue booting up without throwing an error.
How to Use Absolute and Relative Paths
You can include files by specifying their exact location on the
server. If you use a relative path, Apache resolves it against the
directory defined by your ServerRoot directive.
# Loading a single file via absolute path
Include /etc/httpd/conf.d/vhosts/example.com.conf
# Loading a single file via relative path (relative to ServerRoot)
Include conf.d/custom-settings.confLoading Entire Directories with Wildcards
Instead of adding a new line for every single website or
configuration change, you can instruct Apache to load all files inside a
specific folder using the * wildcard. This is highly
efficient for managing multiple Virtual Hosts.
# Include all configuration files in a specific directory
IncludeOptional conf.d/*.conf
# Include configuration files from a nested structure
Include /etc/apache2/sites-enabled/*.confBest Practices for Modular Configurations
To maintain a clean and reliable server environment, consider adopting the standard structural patterns used by major Linux distributions:
- Sites-Available vs. Sites-Enabled: Keep your actual
Virtual Host files in a
sites-available/directory. Use symbolic links inside asites-enabled/directory to point to the active sites, and useIncludeon the enabled directory. - Use Descriptive Extensions: Always suffix your
modular files with
.confso they are easily targetable by wildcards and distinct from backup or temporary files. - Always Test Syntax: Before restarting your web server after modifying configuration files, always run the configuration test command to check for syntax errors:
# For Ubuntu/Debian systems
apache2ctl configtest
# For CentOS/RHEL/Fedora systems
httpd -t