Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Consts:
- bindDir="/etc/bind"
- bindMainConfig="$bindDir/named.conf.local"
- apSiteDir="/var/www"
- apBaseDir="/etc/apache2"
- # Check configuration:
- if [[ ! -d $bindDir ]] || [[ ! -f $bindMainConfig ]] ||
- [[ ! -d $apSiteDir ]] || [[ ! -d $apBaseDir ]]; then
- echo "[ FAIL ] Check configuration paths, some of them are invalids"
- exit
- fi
- if [[ "$1" == "--help" ]]; then
- echo "-db <database name> create a database with the given name and user with password for it"
- echo " You can specify a username and password using the --user and --password"
- echo "-a <aliase> will add to your domain"
- echo " TIPS: This parameter can be used repeatedly"
- exit
- fi
- # Check that required and validate action:
- if [[ $# < 2 ]] || ([[ "$1" != "add" ]] && [[ "$1" != "del" ]]); then
- echo "Usage: $0 <action: add|del> <domain> [options]"
- echo "Try '$0 --help' for more information"
- exit
- fi
- action="$1"; shift
- # Validate domain:
- if [[ $(echo "$1" | grep -E '^[a-zA-Z0-9_-]+\.[a-zA-Z]+$') != $1 ]]; then
- echo "[ FAIL ] Domain name ('$1') not valid"
- exit
- fi
- domain="$1"; shift
- # Parse args:
- while [[ $# > 1 ]]; do
- case "$1" in
- -a | --aliase)
- if [[ $(echo "$2" | grep -E '^[a-zA-Z0-9]+\.[a-zA-Z0-9_-]+\.[a-zA-Z]+$') == $2 ]]; then
- aliases+=("$2")
- else
- echo -n "Alias '$2' was DROPPED, want to continue? (y/N) "
- read answer
- if [[ $answer != "y" ]] && [[ $answer != "Y" ]]; then
- exit
- fi
- fi
- shift
- ;;
- -db)
- dbName="$2"
- shift
- ;;
- --user)
- dbUser="$2"
- shift
- ;;
- --password)
- dbPwd="$2"
- shift
- ;;
- esac
- shift
- done
- # Verify database name if available:
- if [[ $dbName ]]; then
- if [[ $(echo "$dbName" | grep -E '^[a-zA-Z0-9]+$') != $dbName ]]; then
- echo "[ FAIL ] Database name ('$dbName') not valid"; exit
- fi
- # And check mysql:
- if [[ ! $(mysql --help 2> /dev/null) ]]; then
- echo "[ FAIL ] MySQL not found"; exit
- fi
- # Also setup username and password if none:
- if [[ ! $dbUser ]]; then
- dbUser=$dbName
- fi
- if [[ ! $dbPwd ]]; then
- dbPwd=$domain
- fi
- fi
- # Delete command:
- if [[ $action == "del" ]]; then
- # Remove from apache:
- if [ -f $apBaseDir/sites-enabled/$domain.conf ]; then
- a2dissite $domain > /dev/null
- service apache2 reload
- rm -r $apSiteDir/$domain
- rm $apBaseDir/sites-available/$domain.conf
- echo "[ OK ] Domain '$domain' successfully DELETED from APACHE!"
- fi
- # From BIND:
- if [ -f $bindDir/db.$domain.conf ]; then
- sed "/zone \"$domain\"/,/}/d" -i $bindMainConfig
- service bind9 restart
- rm $bindDir/db.$domain.conf
- echo "[ OK ] Domain '$domain' successfully DELETED from BIND!"
- fi
- # From MySQL:
- if [[ $dbName ]]; then
- mysql -u root -e "drop user $dbUser@$dbName" 2> /dev/null
- mysql -u root -e "drop database $dbName" 2> /dev/null
- echo "[ OK ] Database '$dbName' dropped!"
- fi; exit
- fi
- # Now will ADD action ----------------------------------------
- # If already in use:
- message="[ FAIL ] Domain '$domain' already in use"
- if [[ $(cat $apBaseDir/sites-enabled/* | grep -v '^\s*#' | grep $domain) ]]; then
- echo $message; exit
- fi
- for alias in ${aliases[@]}; do
- if [[ $(cat $apBaseDir/sites-enabled/* | grep -v '^\s*#' | grep $alias) ]]; then
- echo "[ FAIL ] Alias '$alias' already in use"; exit
- fi
- done
- if [[ $(cat $bindDir/* | grep -v '^\s*#' | grep $domain) ]]; then
- echo $message; exit
- fi
- if [[ -d $apSiteDir/$domain ]] || [[ -f $apBaseDir/sites-available/$domain.conf ]] ||
- [[ -f $bindDir/db.$domain.conf ]]; then
- echo $message; exit
- fi
- # Create that needed ----------------------------------------
- # MySQL:
- if [[ $dbName ]]; then
- if [[ $(mysql -u root -e "create database $dbName" 2>&1) ]]; then
- echo "[ FAIL ] Database '$dbName' already exists!"; exit
- fi
- if [[ $(mysql -u root -e "create user $dbUser@$dbName identified by '$dbPwd'" 2>&1) ]]; then
- echo "[ FAIL ] User '$dbUser' already exists!"
- mysql -u root -e "drop database $dbName"; exit
- fi
- mysql -u root -e "grant all privileges on $dbName.* to $dbUser@$dbName"
- mysql -u root -e "flush privileges"
- echo "[ OK ] Database '$dbName' successfully created"
- echo " User = '$dbUser' | Password = '$dbPwd'"
- fi
- # Apache:
- apBaseDir="$apBaseDir/sites-available"
- mkdir $apSiteDir/$domain
- echo -e "<?php\n phpinfo();\n?>" >> $apSiteDir/$domain/index.php
- echo "<VirtualHost *:80>" >> $apBaseDir/$domain.conf
- echo " ServerName $domain" >> $apBaseDir/$domain.conf
- echo " ServerAdmin mail@$domain" >> $apBaseDir/$domain.conf
- echo " DocumentRoot $apBaseDir/$domain" >> $apBaseDir/$domain.conf
- echo " ErrorLog \${APACHE_LOG_DIR}/$domain.error.log" >> $apBaseDir/$domain.conf
- echo " CustomLog \${APACHE_LOG_DIR}/$domain.access.log combined" >> $apBaseDir/$domain.conf
- for alias in ${aliases[@]}; do
- echo " ServerAlias $alias" >> $apBaseDir/$domain.conf
- done
- echo "</VirtualHost>" >> $apBaseDir/$domain.conf
- # Bind:
- cp $bindDir/db.local $bindDir/db.$domain.conf
- sed -i -- "s/localhost/$domain/g" $bindDir/db.$domain.conf
- echo "zone \"$domain\" {" >> $bindMainConfig
- echo " type master;" >> $bindMainConfig
- echo " file \"$bindDir/db.$domain.conf\";" >> $bindMainConfig
- echo "};" >> $bindMainConfig
- # Enable this now:
- a2ensite $domain > /dev/null
- service apache2 reload
- service bind9 reload
- echo "[ OK ] Domain '$domain' successfully ADDED!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement