Apache Web Server Difference Between Include vs IncludeOptional Directives
Apache Web Server’s configuration is split across multiple files and that totally makes sense because of obvious reasons like better separation, less clutter, better organisation and manageability, etc.
How does it all get read by httpd server though ? Well the server actually loads the main config file –
apache2.conf that includes all the different configuration files. There are two ways (or directives) to include:
So what is the difference between both ? Well the thing is both can be used with glob pattern wildcards but
Include will fail with an error if a file or folder specified directly or with a wildcard doesn’t exists whereas
IncludeOptional will silently ignore.
Let’s see an example, shall we? If I add the following to the end of my main config file:
Now see what happens when
httpd will try to parse the configuration:
# mkdir include-fail # apachectl configtest apache2: Syntax error on line 229 of /etc/apache2/apache2.conf: No matches for the wildcard '*' in '/etc/apache2/include-fail', failing (use IncludeOptional if required) Action 'configtest' failed. The Apache error log may have more information. # touch include-fail/test.conf # apachectl configtest Syntax OK
Theres’a an error failure first but then once a file is created inside the directory it passes fine. Of course this failure wouldn’t have occurred if
IncludeOptional was used instead. Similarly if we did something like
Include dir/*/* then that’d require
dir to have at least one subdirectory containing at least one file.
Do remember that if you include a directory, then Apache will recursively read all the files and subdirectories inside that. You may not want this as leaving temporary files that can cause unnecessary chaos is a possibility. It’s better to have better specificity by doing something like say
Include dir/*.conf instead of
Note: The values passed to either of the directives must be either absolute path or relative path to the