#!/bin/bash # #Variables VHOSTNAME=$1 TIER=$2 # HTTPDCONF=/etc/httpd/conf/httpd.conf VHOSTCONFDIR=/etc/httpd/conf.vhosts.d DEFVHOSTCONFFILE=$VHOSTCONFDIR/00-default-vhost.conf VHOSTCONFFILE=$VHOSTCONFDIR/$VHOSTNAME.conf WWWROOT=/srv DEFVHOSTDOCROOT=$WWWROOT/default/www VHOSTDOCROOT=$WWWROOT/$VHOSTNAME/www #delete vhost if [ "$2" = 'del' ];then if [ -d $VHOSTDOCROOT ];then rm -rf $VHOSTDOCROOT echo "Delete Directory Successful." fi if [ -f $VHOSTCONFFILE ];then rm -rf $VHOSTCONFFILE echo "Delete Config Successful." else echo "Nothing to do." fi exit 1 fi #Check arguments if [ -z $VHOSTNAME ] || [ -z $TIER ];then echo "Usage: $0 VHOSTNAME TIER" exit 1 else #Set support email address case $TIER in 1) VHOSTADMIN='basic_support@example.com' ;; 2) VHOSTADMIN='business_support@example.com' ;; 3) VHOSTADMIN='enterprise_support@example.com' ;; *) echo "Invalid tier specfied." exit 1 ;; esac fi # #Create conf directory one time if non-existent if [ ! -d $VHOSTCONFDIR ];then mkdir $VHOSTCONFDIR if [ $? -ne 0 ];then echo "ERROR : Failed creating $VHOSTCONFDIR" exit 1 fi fi # #Add include one time if missing grep -q '^IncludeOptional conf\.vhosts\.d/\*\.conf$' $HTTPDCONF if [ $? -ne 0 ];then #Backup before modifying cp -a $HTTPDCONF $HTTPDCONF.orig echo "IncludeOptional conf.vhosts.d/*.conf" >>$HTTPDCONF if [ $? -ne 0 ];then echo "ERROR: Failed adding include directive." exit 1 fi fi # #Check for default virtual host if [ ! -f $DEFVHOSTCONFFILE ];then cat <$DEFVHOSTCONFFILE DocumentRoot $DEFVHOSTDOCROOT CustomLog "logs/default-vhost.log" combined # Require all granted DEFCONFEOF fi # if [ ! -d $VHOSTCONFDIR ];then mkdir $VHOSTCONFDIR restorecon -Rv /srv/ &>/dev/null fi #Check for VirtualHost conflict if [ -f $VHOSTCONFFILE ];then echo "ERROR:$VHOSTCONFFILE already exists." exit 1 elif [ -d $VHOSTDOCROOT ];then echo "ERROR:$VHOSTDOCROOT already exists." exit 1 else cat <$VHOSTCONFFILE Require all granted AllowOverride None # DocumentRoot $VHOSTDOCROOT ServerName $VHOSTNAME ServerAdmin $VHOSTADMIN ErrorLog "logs/${VHOSTNAME}_error_log" CustomLog "logs/${VHOSTNAME}_access_log" common CONFEOF mkdir -p $VHOSTDOCROOT restorecon -Rv $WWWROOT &>/dev/null fi # #Check config and reload apachectl configtest &>/dev/null if [ $? -eq 0 ];then systemctl reload httpd &>/dev/null else echo "ERROR:Config error" exit 1 fi