openSuse: Apache modules management from shell

To manage Apache Httpd on openSuse we have the classic command a2enmod, shared with many other distros, which allows to: 1) know if a module is enabled, 2) enable a module and 3) disable a module.

To extend what already present in a2enmod i have written a simple wrapper to a2enmod in order to have mote functionalities: 1) get the list of every module available on the system, 2) get the list of every module enabled, 3) check if a module has been enabled or not, 4) enable one or more modules and 5) disable one or more module.

To be able to use this tool by yourself create file /usr/local/bin/apache2mods.sh, make it executable and inside it copy & paste what follows:

#!/bin/sh

LIBSUFFIX=""
[ "$(arch)" = "x86_64" ] && LIBSUFFIX=64

MODSDIR=/usr/lib${LIBSUFFIX}/apache2
A2ENMOD=$( which a2enmod 2>/dev/null )
A2CTL=$( which apache2ctl 2>/dev/null )

if [ ! -e ${MODSDIR} ]; then
	echo "Directory ${MODSDIR} not found"
	exit 1
fi

if [ "$A2ENMOD" = "" ]; then
	echo "Executable 'a2enmod' not found"
	exit 1
fi

if [ "$A2CTL" = "" ]; then
	echo "Executable 'apache2ctl' not found"
	exit 1
fi

cd $MODSDIR 2>&1 >/dev/null

MODSAVAIL=$( ls mod_*.so | sed -es/^mod_// | sed -es/.so$// | tr '\n' ' ' )
MODSLOADED=$( $A2ENMOD -l )


if [ "$1" = "" ]; then
	echo ""
	echo "Usage: apache2mods.sh <option> [modules]"
	echo "Option: "
	echo "- all : display all available modules"
	echo "- all-enabled : display all enabled modules"
	echo "- enabled <mod> : display if the given module as been loaded"
	echo "- enable <mod> [<mod> ...] : enable the given modules"
	echo "- disable <mod> [<mod> ...] : enable the given modules"
	echo ""
	exit 0
fi

if [ "$1" = "all" ]; then
	echo "Apache2 available modules: "
	echo "${MODSAVAIL}"
	echo ""
	exit 0
fi

if [ "$1" = "all-enabled" ]; then
	echo "Apache2 enabled modules: "
	echo "$( ${A2ENMOD} -l)"
	echo ""
	exit 0
fi


if [ "$1" = "enabled" ]; then
	MOD=$2
	ISOK="not enabled"
	$A2ENMOD -q ${MOD} 2>/dev/null >/dev/null && ISOK="enabled"
	echo "Module ${MOD} ${ISOK}"
	echo ""
	exit 0
fi

if [ "$1" = "enable" ]; then
	shift 1
	echo "Apache2 enabling modules: "
	for MOD in $@ ;
	do 
		ISOK="error"
		$A2ENMOD ${MOD} 2>/dev/null >/dev/null && ISOK="ok"
		echo "- enabling ${MOD}: ${ISOK}"
	done
	$A2CTL restart
	echo ""
	exit 0
fi

if [ "$1" = "disable" ]; then
	shift 1
	echo "Apache2 disabling modules: "
	for MOD in $@ ;
	do 
		$A2ENMOD -q ${MOD} 2>/dev/null >/dev/null
		if [ "$?" != "0" ]; then
			ISOK="not loaded, nothing to do"
		else
			ISOK="error"
			$A2ENMOD -d ${MOD} 2>/dev/null >/dev/null && ISOK="ok"
		fi
		echo "- disabling ${MOD}: ${ISOK}"
	done
	$A2CTL restart
	echo ""
	exit 0
fi

At this point you will be able to call

  • apache2mods.sh all : get the list of every module available
  • apache2mods.sh all-enabled : get the list of every module enabled
  • apache2mods.sh enabled <module> : check if the module has been enabled
  • apache2mods.sh enable <module> [<module> ... <module>] : enable the module, or the modules, specified in the parameters' list
  • apache2mods.sh disable <module> [ <module> ... <module>] : disable the module, or the modules, specified in the parameters' list