2

I want to write a configuration for an apache server which should work for apache-2.2 and -2.4 without alterations. Since there are a quite a few new modules in v2.4 which have to be loaded,and a few altered directives, I will need some conditional statements. What I'd like is something along the lines of

<If ApacheVersion >= 2.4>
  LoadModule mod_only_in_2.4.so
</If>

but I haven't found the right expression yet. Can somebody point me in the right direction?

Clarification of my motives: I am developing a server which I want to be able to just copy onto a server and run without further host-dependent configuration. And since the machines can have either version of apache, I want a configuration which works in both cases.

3
  • 3
    This is a wrong approach. Use configuration management to achieve this. Something like cfengine, chef, puppet, etc...
    – Marcel
    Feb 19, 2014 at 14:11
  • 1
    Exactly. You can't do this from a single configuration file. Use puppet or something similar. Feb 19, 2014 at 14:15
  • Another use of this would be to avoid warnings for NameVirtualHosts in the case of Apache 2.4+ May 18, 2016 at 12:46

3 Answers 3

6

You should be checking for the module, not the Apache version.

For instance:

<IfModule mod_authz_core.c>
Require all granted
</IfModule>

<IfModule !mod_authz_core.c>
Order deny,allow
Allow from all
</IfModule>

Note that you can't guess in advance whether someone has a specific Apache version; it's up to the sysadmin to make sure that the correct modules are being loaded. As the developer all you really need to worry about is that you are providing the correct directives, depending on what modules are available.

As a sysadmin you will have to maintain differing configurations for Apache 2.2 and 2.4; there is no way around this. Fortunately every configuration management system makes this relatively easy, and if you aren't already using such a system, you should be.

6
  • My problem is not in using these modules, but in loading them. IfModule only tells me if a module is loaded, not if can be loaded. I want to load mod_authz_core if its available (on 2.4), but not cause an error if it's not (on 2.2).
    – mat
    Feb 19, 2014 at 14:07
  • Unless you're the administrator, you don't have to worry about that. Feb 19, 2014 at 14:08
  • I am. No chance of delegating the problem.
    – mat
    Feb 19, 2014 at 14:09
  • 2
    In that case, you aren't going to solve it in the Apache configuration file like this. Perhaps you should clarify your question. Feb 19, 2014 at 14:10
  • I just elaborated on my motives.
    – mat
    Feb 19, 2014 at 14:15
5

If mod_version is loaded, which should be available by default (although not loaded by default) in both Apache 2.2 and 2.4, you can use the <IfVersion> directive to check for Apache's (and only Apache's) version:

<IfVersion >= 2.3>
    # this happens only in versions greater or
    # equal 2.3.0.
</IfVersion>

Example taken from the documentation.

2

Here's what I ended up doing: I edited my start script to set a variable specifying the apache version

apache_version_string=`$apache_bin -v|grep -oP "Apache/(\d\.\d)"`
if [[ $apache_version_string == "Apache/2.2" ]]; then
    server_flags=$server_flags" -DAPACHEv22"
 else
    if [[ $apache_version_string == "Apache/2.4" ]]; then
        server_flags=$server_flags" -DAPACHEv24"
    fi
fi

and in the apache configuration I checked for those definitions

<IfDefine APACHEv24>
  LoadModule authz_core_module        /usr/lib/apache2/mod_authz_core.so
</IfDefine>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .