Include files in Apache HTTP Server Version 2.2 httpd.conf

I have a large httpd.conf file, most of which is virtual hosts. Is there a way to make a file, say virtual_hosts.conf, and include it from httpd.conf? I've googled a bit, but can't seem to find much as far as includes, just module loading.


Solution 1:

Information on apache httpd.conf files can be found at here.

Some snippets have been copied from this website to ensure that the information is not lost if the link would deprecated:

Include /usr/local/apache2/conf/ssl.conf
Include /usr/local/apache2/conf/vhosts/*.conf

Relative paths:

Include conf/ssl.conf
Include conf/vhosts/*.conf

Wildcards:

Include conf/vhosts/*/*.conf

Solution 2:

I separate each virtual host into it's own vhost config file, that way you don't wind up searching through a giant document looking for one little directive. Similar to Quanta's post:

Include /etc/apache2/vhosts.d/*.conf

Just place it as the last line in your httpd.conf

then just split your single vhosts.conf into individual files for each domain, i.e.

domain1.conf
domain2.conf
domain3.conf
etc.... 

much easier to manage. -sean

Solution 3:

You can do it with Include directive:

Include /path/to/virtual_hosts.conf

Solution 4:

IncludeOptional

Also you can use IncludeOptional directive

IncludeOptional conf/vhosts/*.conf

So in such case there can be no any appropriate file. Or as exactly said in documentation,

It works identically to the Include directive, but it will be silently ignored (instead of causing an error) if wildcards are used and they do not match any file or directory or if a file path does not exist on the file system.

BUT!

Pay attention that some versions of Apache really fails to start in cases when "file path does not exist on the file system". Like on example above if there is no directory conf/vhosts/.

So, in such a problem you may be restricted in fact in safe usage of this directive by non-using some subdirectories when you are not sure if they exists

Like such a manner:

<VirtualHost *:80>

DocumentRoot    "/var/www/example.com"
ServerName      "example.com"
ServerAlias     "www.example.com" "example.com" 
...

IncludeOptional /var/www/example.com/*.httpd.conf

</VirtualHost>



<VirtualHost *:80>

DocumentRoot    "/var/www/other.org"
ServerName      "other.org"
ServerAlias     "www.other.org" "other.org" 
...

IncludeOptional /var/www/other.org/*.httpd.conf

</VirtualHost>

When some like

IncludeOptional /var/www/other.org/*.httpd.conf

will works good (have you any file matching given mask or not), but

IncludeOptional /var/www/other.org/config_optional_subdir/*.conf

will fail if there is no such subdirectory config_optional_subdir.

Note that

IncludeOptional /var/www/other.org/extra.httpd.conf

will fail too if at least one of sites will not have exactly specified file extra.httpd.conf. Wildcards are the key.


So, if common site configuration created and can be recreated automatically by some template, but some concrete site requires an extra apache configuration that cant be wrote just in a .htaccess file - you can use this approach.