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.

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.conf

Loading 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/*.conf

Best Practices for Modular Configurations

To maintain a clean and reliable server environment, consider adopting the standard structural patterns used by major Linux distributions:

# For Ubuntu/Debian systems
apache2ctl configtest

# For CentOS/RHEL/Fedora systems
httpd -t