When managing complex Apache configurations, especially in environments with multiple virtual hosts, splitting your httpd.conf
into modular files becomes essential. Apache HTTP Server 2.2 provides robust inclusion mechanisms that many administrators underutilize.
The simplest way to include external configuration files is using the Include
directive:
# In your main httpd.conf
Include conf/virtual_hosts.conf
This path is relative to your ServerRoot
directory. For absolute paths:
Include /etc/apache2/custom/virtual_hosts.conf
For even better organization, you can include multiple files matching a pattern:
# Include all .conf files in the vhosts.d directory
Include conf/vhosts.d/*.conf
This approach is particularly useful when:
- Managing client-specific configurations
- Separating SSL and non-SSL virtual hosts
- Maintaining environment-specific settings (dev/stage/prod)
Here's a complete example of how to structure your virtual hosts across multiple files:
# httpd.conf
Include conf/vhosts/default.conf
Include conf/vhosts/client1.conf
Include conf/vhosts/client2.conf
Then in conf/vhosts/client1.conf
:
ServerName client1.example.com
DocumentRoot /var/www/client1
ErrorLog /var/log/apache2/client1_error.log
CustomLog /var/log/apache2/client1_access.log combined
Apache 2.2 introduced IncludeOptional
which won't throw errors if files don't exist:
IncludeOptional conf/vhosts/client3.conf
This is particularly useful for:
- Optional configurations
- Temporary maintenance pages
- Environment-specific files that may not exist everywhere
For optimal maintainability:
- Keep your main
httpd.conf
minimal - Group related configurations (all SSL, all redirects, etc.)
- Use descriptive filenames (
ssl_vhosts.conf
,redirects.conf
) - Document includes with comments
After making changes, always test before restarting Apache:
apachectl configtest
Common errors to watch for:
- Missing included files (when not using IncludeOptional)
- Permission issues on included files
- Duplicate directives across included files
In Apache HTTP Server 2.2, the Include directive allows you to break down your monolithic configuration into manageable pieces. This is particularly useful when dealing with numerous virtual hosts or complex configurations.
# Basic syntax in httpd.conf
Include /path/to/virtual_hosts.conf
Here's how to migrate your virtual hosts to a separate file:
- Create a new configuration file (e.g., /etc/apache2/vhosts/virtual_hosts.conf)
- Move all your VirtualHost blocks to this new file
- Add the Include directive to your main httpd.conf
# Example virtual_hosts.conf content
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example
# Other directives...
</VirtualHost>
<VirtualHost *:80>
ServerName test.example.com
DocumentRoot /var/www/test
# Other directives...
</VirtualHost>
You can use wildcards to include multiple files:
# Include all .conf files in a directory
Include /etc/apache2/vhosts/*.conf
# Include all files matching a pattern
Include /etc/apache2/conf.d/site_*.conf
- Store included files in a dedicated directory (e.g., /etc/apache2/conf.d/)
- Use descriptive filenames (e.g., ssl_vhosts.conf, php_settings.conf)
- Keep related configurations together in the same file
- Document included files with comments in your main httpd.conf
# In httpd.conf
# Virtual Host configurations
Include /etc/apache2/vhosts/virtual_hosts.conf
# SSL-specific configurations
Include /etc/apache2/ssl/ssl_config.conf
If your includes aren't working:
- Check file permissions (Apache needs read access)
- Verify the path is correct (use absolute paths)
- Ensure no syntax errors in included files
- Check Apache error logs for specific messages
While includes improve maintainability, be aware that:
- Each included file requires additional I/O operations
- Too many small files may impact performance
- Apache must parse all included files at startup