Skip to content

Instantly share code, notes, and snippets.

@ayan4m1
Created May 28, 2015 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayan4m1/791695be95be81816843 to your computer and use it in GitHub Desktop.
Save ayan4m1/791695be95be81816843 to your computer and use it in GitHub Desktop.
CentOS HTTPD Site Manager
#!/bin/bash
### httpdensite --- Bash script to enable or disable a site in httpd.
### Author: ayan4m1 <andrew@bulletlogic.com>
### Based on nginx_ensite by author: António P. P. Almeida <appa@perusio.net>
### Permission is hereby granted, free of charge, to any person obtaining a
## copy of this software and associated documentation files (the "Software"),
### to deal in the Software without restriction, including without limitation
### the rights to use, copy, modify, merge, publish, distribute, sublicense,
### and/or sell copies of the Software, and to permit persons to whom the
### Software is furnished to do so, subject to the following conditions:
### The above copyright notice and this permission notice shall be included in
### all copies or substantial portions of the Software.
### Except as contained in this notice, the name(s) of the above copyright
### holders shall not be used in advertising or otherwise to promote the sale,
### use or other dealings in this Software without prior written authorization.
### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
### IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
### FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
### THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
### LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
### FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
### DEALINGS IN THE SOFTWARE.
SCRIPTNAME=${0##*/}
function print_usage() {
echo "$SCRIPTNAME <site name>"
}
HTTPD=$(which httpd)
[ -x $HTTPD ] || exit 0
HTTPD_CONF_DIR=/etc/httpd
[ -d $HTTPD_CONF_DIR ] || exit 0
AVAILABLE_SITES="$HTTPD_CONF_DIR/sites.available.d"
ENABLED_SITES="$HTTPD_CONF_DIR/sites.d"
## Check the number of arguments.
if [ $# -ne 1 ]; then
print_usage
exit 1
fi
ACTION=$(echo $SCRIPTNAME | awk '$0 ~ /dissite/ {print "DISABLE"} $0 ~ /ensite/ {print "ENABLE"} $0 !~ /(dis|en)site/ {print "UNKNOWN"}')
if [ "$ACTION" == "UNKNOWN" ]; then
echo "$SCRIPTNAME: Unknown action!"
print_usage
exit 4
fi
case $ACTION in
ENABLE)
ORDINAL=`ls $ENABLED_SITES -Gg | grep -v '^total' | wc -l`
ORDINAL=`printf "%02d" $ORDINAL`
SOURCE="$AVAILABLE_SITES/$1"
DEST="$ENABLED_SITES/$ORDINAL-$1"
if [ ! -f $SOURCE ]; then
echo "$1 is not present in $AVAILABLE_SITES"
exit 2
fi
if [ -h $DEST ]; then
echo "$1 is already enabled."
exit 0
fi
ln -s $SOURCE $DEST
echo -n "Site $1 has been enabled. "
echo "Run \"service httpd reload\" to apply the changes."
;;
DISABLE)
SITE="$ENABLED_SITES/$1"
if [ -h $SITE ]; then
rm $SITE
echo -n "Site $1 has been disabled. "
echo "Run \"service httpd reload\" to apply the changes."
else
echo "Site $1 does not exist."
exit 0
fi
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment