How do I include a file in Apache config without generating an error when it doesn't exist?

Solution 1:

I came up with a clever solution, though there may be a better way. Put one of the characters in brackets so Apache will treat it as a glob pattern, which is allowed to match zero files without causing an error. E.g.:

Include foo.con[f]

Solution 2:

According to http://httpd.apache.org/docs/2.4/mod/core.html#include you could use "IncludeOptional":

Alternatively, the following command will just be ignored in case of missing files or directories:

IncludeOptional conf/vhosts/*/*.conf

Solution 3:

IncludeOptional foo.conf

Apache httpd version 2.3.6 and later

https://httpd.apache.org/docs/2.4/mod/core.html#includeoptional

Solution 4:

I tried the same as Wouter Van Vliet, but I still got errors. Then I found this link. I added this snippet to my /etc/apache2/apache2.conf and it works like a charm!

Note: You need mod_perl for it!

Here is the code:

<perl>
    use File::stat;
    foreach $file (glob '/srv/www/vhosts/*/conf/vhost.conf') {
        my $stat = stat($file);
        if ($stat-&gt;uid != 0 || $stat-&gt;gid != 0) {
            warn "$file is not owned by root:root, skipping!\n";
            next;
        }
        if ($stat-&gt;mode &amp; 0002) {
            warn "$file is world-writable, skipping!\n";
            next;
        }
        push @Include, $file;
    }
</perl>