Search code examples
apacheubuntudebiandebdpkg

Detect version of Apache2 in shell script


I am creating a deb package that depends on apache2. I would like my package to work on all recent versions of Debian and Ubuntu, therefore it should work with both Apache 2.2 and 2.4.

What is an elegant way to detect the version of apache2?

ISAP22=$(apache2 -v | grep "Apache/2.2")
if [ "$ISAP22" ]; then
  #asssume apache 2.2
else
  #assume apache 2.4
fi

This works in a bash script, but when I put this in my .postinst the package installation fails.


Solution

  • I ended up using dpkg with --compare-versions:

    APACHE_VERSION=$(dpkg -l | awk '$2 ~ /apache2(-prefork)?-dev/ { print $3 }')
    echo "Found Apache version $APACHE_VERSION"
    if dpkg --compare-versions $APACHE_VERSION lt 2.4; then
        echo "Patching config for Apache 2.2"
    fi