With a little side of applesauce...

Wednesday, June 29, 2011

Debian / Apache2.2 - better versions of a2ensite / a2dissite and a2enmod / a2dismod

I had posted these to both Debian and Ubuntu back in 2006-2007, and since they were never picked up, I thought I would post them here.

For those who don't know, Debian allows you to enable virtual hosts and modules via these cool a2en*/a2dis* scripts. The original scripts simply spit out all of the files in /etc/apache2/*-available/ folders in a space-delimited list, plus you could only enable one vhost or mod at a time. Since I was dealing with hundreds of vhosts at the time, I wrote these scripts which displayed my list of available vhosts/mods in columnar format, and allowed me to enable multiple hosts, (explicitly or globbing). These were posted as open bugs for some time, and then a Debian maintainer wrote some Perl versions of these scripts, which are the ones that are used currently in Debian-derivatives.

I wanted to post these, as I still think these perform the task very powerfully, and are much, (MUCH), shorter than the Perl-equivs. I hope that you find them useful. They work on Debian, Ubuntu and Linux Mint. I also have man pages for them, but won't worry about posting them unless someone would like them.

a2ensite:
#!/bin/sh -e

# Initialize variables
SYSCONFDIR='/etc/apache2'

prompt=1
enabledsites=
usage="
Usage: ${0} [OPTION] [FILE] [FILE]...

Options:
-f Never prompt
-h This message
"

while getopts ":ft" OPT
do
case $OPT in
f )
prompt=0
;;
h )
echo "$usage"
exit 1
;;
\?)
echo "$usage"
exit 1;;
esac
done
# remove the flags from $@
shift $((${OPTIND} - 1))


# If a2ensite is called without an argument, then
# print a list of sites to choose from. Multiple sites
# can be passed to the SITENAME variable, (including globbing).
if [ -z $1 ]; then
echo "Which site would you like to enable?"
echo "Your choices are: "
ls $SYSCONFDIR/sites-available/
echo -n "Site name? "
read SITENAME
else
SITENAME="$@"
fi

if [ "$SITENAME" = "default" ]; then
PRIORITY="000"
fi

ensite="y"
# Loop through the site files in SITENAME
for siteentry in $SITENAME
do
# Allow for file globbing
for site in $(ls $SYSCONFDIR/sites-available/$siteentry | xargs -n1 -r basename)
do
# Set default site with top priority
if [ $site = "default" ]; then
PRIORITY="000"
fi

# Check to see if the site has already been enabled
if [ -e $SYSCONFDIR/sites-enabled/$site -o \
-e $SYSCONFDIR/sites-enabled/"$PRIORITY"-"$site" ]; then
echo "$site is already enabled!"
enabled=1
fi

# If -f flag is not passed to the script, prompt user for confirmation on each site.
if [ ${enabled:-0} -eq 0 ]; then
if [ $prompt -eq 1 ]; then
ensitelast=$ensite
echo -n "a2ensite: enable $site? [${ensitelast}] "
read ensite

if [ "$ensite" = "" ]; then
ensite=$ensitelast
fi

if [ ${ensite:-n} = "y" ]; then
if [ $site = "default" ]; then
ln -sf $SYSCONFDIR/sites-available/$site \
$SYSCONFDIR/sites-enabled/"$PRIORITY"-"$site"
else
ln -sf $SYSCONFDIR/sites-available/$site \
$SYSCONFDIR/sites-enabled/$site
fi
enabledsites="$enabledsites $site "
fi
else
if [ $site = "default" ]; then
ln -sf $SYSCONFDIR/sites-available/$site \
$SYSCONFDIR/sites-enabled/"$PRIORITY"-"$site"
else
ln -sf $SYSCONFDIR/sites-available/$site \
$SYSCONFDIR/sites-enabled/$site
fi
enabledsites="$enabledsites $site "
fi
fi
unset enabled
done
done



# Print list of enabled sites, if any.
# Test to see if any sites were enabled
if [ ${#enabledsites} -gt 0 ]; then
echo "Following site(s) enabled: "
echo
for vhost in $enabledsites
do
echo "$vhost"
done
echo
echo "Run /etc/init.d/apache2 reload to fully enable."
else
echo
echo "No sites were enabled."
fi
unset enabledsites


a2dissite:
#!/bin/sh -e

# Initialize variables
SYSCONFDIR='/etc/apache2'

prompt=1
disabledsites=
usage="
Usage: ${0} [OPTION]...

Options:
-f Never prompt
-h This message
"

while getopts ":ft" OPT
do
case $OPT in
f )
prompt=0
;;
h )
echo "$usage"
exit 1
;;
\?)
echo "$usage"
exit 1;;
esac
done
# remove the flags from $@
shift $((${OPTIND} - 1))

# If a2dissite is called without an argument, then
# print a list of sites to choose from. Multiple sites
# can be passed to the SITENAME variable.
if [ -z $1 ]; then
echo "Which site would you like to disable?"
echo "Your choices are: "
ls $SYSCONFDIR/sites-enabled/
echo -n "Site name? "
read SITENAME
else
SITENAME="$@"
fi

delsite="n"
# Loop through the site files in SITENAME
for siteentry in $SITENAME
do
# Set default site with top priority
##
if [ $siteentry = "default" ]; then
siteentry="000-default"
fi

# Allow for file globbing
for site in $(ls $SYSCONFDIR/sites-enabled/$siteentry | xargs -n1 -r basename)
do

# Check to see if the site has already been disabled
if ! [ -e $SYSCONFDIR/sites-enabled/$site ]; then
echo "$site is already disabled, or does not exist!"
disabled=1
fi

# If -f flag is not passed to the script, prompt user for confirmation on each site.
if [ ${disabled:-0} -eq 0 ]; then
if [ $prompt -eq 1 ]; then
delsitelast=$delsite
echo -n "a2dissite: disable $site? [${delsitelast}] "
read delsite

if [ "$delsite" = "" ]; then
delsite=$delsitelast
fi

if [ ${delsite:-n} = "y" ]; then
rm $SYSCONFDIR/sites-enabled/$site 2>/dev/null
disabledsites="$disabledsites $site "
fi
else
rm $SYSCONFDIR/sites-enabled/$site 2>/dev/null
disabledsites="$disabledsites $site "
fi
fi
unset disabled
done
done


# Print list of disabled sites, if any
# Test to see if any sites were disabled
if [ ${#disabledsites} -gt 0 ]; then
echo "Following site(s) disabled: "
echo
for vhost in $disabledsites
do
echo "$vhost"
done
echo
echo "Run /etc/init.d/apache2 reload to fully disable."
else
echo
echo "No sites were disabled."
fi
unset disabledsites



a2enmod:
#!/bin/sh -e

# Initialize variables
SYSCONFDIR='/etc/apache2'

prompt=1
enabledmods=
usage="
Usage: ${0} [OPTION] [FILE] [FILE]...

Options:
-f Never prompt
-h This message
"

while getopts ":ft" OPT
do
case $OPT in
f )
prompt=0
;;
h )
echo "$usage"
exit 1
;;
\?)
echo "$usage"
exit 1;;
esac
done
# remove the flags from $@
shift $((${OPTIND} - 1))


# If a2enmod is called without an argument, then
# print a list of mods to choose from. Multiple mods
# can be passed to the MODNAME variable, (including globbing).
if [ -z $1 ]; then
echo "Which mod would you like to enable?"
echo "Your choices are: "
ls $SYSCONFDIR/mods-available/ | cut -d. -f1 | uniq | column
echo -n "Mod name? "
read MODNAME
else
MODNAME="$@"
fi

enmod="y"
# Loop through the mod files in MODNAME
for modentry in $MODNAME
do
# Allow for file globbing
for mod in $(ls $SYSCONFDIR/mods-available/$modentry* | xargs -n1 -r basename | cut -d. -f1 | uniq)
do
# Check to see if the mod has already been enabled
if [ -e $SYSCONFDIR/mods-enabled/${mod}.conf -o \
-e $SYSCONFDIR/mods-enabled/${mod}.load ]; then
echo "$mod is already enabled!"
enabled=1
fi

# If -f flag is not passed to the script, prompt user for confirmation on each mod.
if [ ${enabled:-0} -eq 0 ]; then
if [ $prompt -eq 1 ]; then
enmodlast=$enmod
echo -n "a2enmod: enable $mod? [${enmodlast}] "
read enmod

if [ "$enmod" = "" ]; then
enmod=$enmodlast
fi

if [ ${enmod:-n} = "y" ]; then
ln -sf $SYSCONFDIR/mods-available/${mod}* $SYSCONFDIR/mods-enabled/
enabledmods="$enabledmods $mod "
fi
else
ln -sf $SYSCONFDIR/mods-available/${mod}* $SYSCONFDIR/mods-enabled/
enabledmods="$enabledmods $mod "
fi
fi
unset enabled
done
done



# Print list of enabled mods, if any.
# Test to see if any mods were enabled
if [ ${#enabledmods} -gt 0 ]; then
echo "Following mod(s) enabled: "
echo
for m in $enabledmods
do
echo "$m"
done
echo
echo "Run /etc/init.d/apache2 reload to fully enable."
else
echo
echo "No mods were enabled."
fi
unset enabledmods



a2dismod:
#!/bin/sh -e

# Initialize variables
SYSCONFDIR='/etc/apache2'

prompt=1
disabledmods=
usage="
Usage: ${0} [OPTION]...

Options:
-f Never prompt
-h This message
"

while getopts ":ft" OPT
do
case $OPT in
f )
prompt=0
;;
h )
echo "$usage"
exit 1
;;
\?)
echo "$usage"
exit 1;;
esac
done
# remove the flags from $@
shift $((${OPTIND} - 1))

# If a2dismod is called without an argument, then
# print a list of mods to choose from. Multiple mods
# can be passed to the MODNAME variable.
if [ -z $1 ]; then
echo "Which mod would you like to disable?"
echo "Your choices are: "
ls $SYSCONFDIR/mods-enabled/ | cut -d. -f1 | uniq | column
echo -n "Mod name? "
read MODNAME
else
MODNAME="$@"
fi

delmod="n"
# Loop through the mod files in MODNAME
for modentry in $MODNAME
do
# Allow for file globbing
for mod in $(ls $SYSCONFDIR/mods-enabled/$modentry* | xargs -n1 -r basename | cut -d. -f1 | uniq)
do
# If -f flag is not passed to the script, prompt user for confirmation on each mod.
if [ $prompt -eq 1 ]; then
delmodlast=$delmod
echo -n "a2dismod: disable $mod? [${delmodlast}] "
read delmod

if [ "$delmod" = "" ]; then
delmod=$delmodlast
fi

if [ ${delmod:-n} = "y" ]; then
rm $SYSCONFDIR/mods-enabled/${mod}* 2>/dev/null
disabledmods="$disabledmods $mod "
fi
else
rm $SYSCONFDIR/mods-enabled/${mod}* 2>/dev/null
disabledmods="$disabledmods $mod "
fi
done
done


# Print list of disabled mods, if any
# Test to see if any mods were disabled
if [ ${#disabledmods} -gt 0 ]; then
echo "Following mod(s) disabled: "
echo
for m in $disabledmods
do
echo "$m"
done
echo
echo "Run /etc/init.d/apache2 reload to fully disable."
else
echo
echo "No mods were disabled."
fi
unset disabledmods

No comments: