Skip to content

Instantly share code, notes, and snippets.

@iamkirkbater
Last active January 2, 2016 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamkirkbater/8334327 to your computer and use it in GitHub Desktop.
Save iamkirkbater/8334327 to your computer and use it in GitHub Desktop.
Shell Script to add Vhost.Run as root, takes one param: Directory to add, and extra optional param as subdirectory written as /subdirectory where / is folder root.ex: sudo bash newSite.sh [-h | -c | -m ] <nameOfNewsite> [subDirectory DocumentRoot] To run as commands, place files in your /usr/local folder without the .sh extensions, and then just…
#!/bin/sh
# Define colors
red="\033[33;31m"
green="\033[33;32m"
yellow="\033[33;33m"
blue="\033[33;34m"
magenta="\033[33;35m"
gray="\033[33;30m"
cyan="\033[33;36m"
reset="\033[33;0m"
#!/bin/sh
##############################
## This script assumes ##
## that you've set up ##
## apache like debian: ##
## http://apple.stackexchange.com/questions/37241/how-to-configure-local-apache-virtual-hosts
##############################
##############################
## ##
## SETTINGS ##
## ##
##############################
##
username="kbater" #Computer Username
pathToSite="/Users/${username}/Dev/"
pathToVhosts="/etc/apache2/sites-enabled/"
pathToHosts="/etc/hosts"
pubDir="dist"
sitePostfix=".dev"
pubPostfix=".pub"
#Logging Options
individualLogs=true; #True or False
#If the above bool is false, uncomment the following line and comment out the others
#pathToMasterLog="/path/to/apache/log"
pathToLogFolder="${pathToSite}${siteName}"
################################
################################
#Check for flags:
#
# -c : disable colors
# -h : show help dialogs
# -m : don't make the directory
#
colors=true
makeDir=true
optCount=0
while getopts ":hcmc:" opt; do
let optCount=optCount+1
case $opt in
c)
colors=false
;;
h)
printf "Usage: sudo newSite.sh [-h|-c|-m] <Site Name> [subDirectory for Apache]\nWhere -h displays Help, -c disables colors, and -m disables making new directory\n";
exit 0
;;
m)
makeDir=false
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
#Separate Args from Opts
argArray=("$@")
arg1=$optCount
arg2=arg1+1
siteName=${argArray[arg1]};
subDir=${argArray[arg2]};
# Import Colors Script if not flagged for no-colors
if [ ${colors} == true ]; then
source colors
fi
## Run The Script
# Create a new directory, blank file, and OWN IT
#sucess tracks the progress of the script, because if one completes it won't kill anything, you'll just get errors,
#but then it checks before continuing the script for errors.
# Only performs this block if user doesn't set the -n flag.
echo "${green}Creating New Directory at ${reset}${pathToSite}${siteName}";
if [ ${makeDir} == true ]; then
success=0
mkdir ${pathToSite}${siteName}
let success=success+$?
chown -R ${username} ${pathToSite}${siteName}
let success=success+$?
if [ ${success} -ne "0" ]; then
echo -e "${red}Couldn't create new Directory at ${reset}${pathToSite}${siteName}. Script Failed.${reset}"
exit 1
fi
fi
# Create a new entry in the Apache virtual hosts file.
echo "${green}Adding Entry into ${reset}${pathToVhosts}"
printf "<VirtualHost *:80>\n DocumentRoot \"${pathToSite}${siteName}${subDir}\"\n ServerName ${siteName}${sitePostfix}\n ErrorLog \"${pathToLogFolder}apache-error.log\"\n</VirtualHost>\n\n<VirtualHost *:80>\n DocumentRoot \"${pathToSite}${siteName}/${pubDir}\"\n ServerName ${siteName}${pubPostfix}\n ErrorLog \"${pathToLogFolder}apache-error.log\"\n</VirtualHost>" >> ${pathToVhosts}vhost-${siteName}${sitePostfix}
if [ "$?" -ne "0" ]; then
echo "${red}Appending to ${reset}${pathToVhosts} ${red} could not happen. Script Failed.${reset}"
rm ${pathToVhosts}vhost-${siteName}${sitePostfix}
rmdir ${pathToSite}/${siteName}
exit 1
fi
# Add an entry to the /etc/hosts file and restart Apache
echo "${green}Adding Entry into ${reset}${pathToHosts}"
sudo printf "\n127.0.0.1\t${siteName}.dev" >> ${pathToHosts}
if [ "$?" -ne "0" ]; then
echo "${red}Appending to ${reset}${pathToHosts} ${red}could not happen. Script Failed.${reset}"
rm ${pathToVhosts}vhost-${siteName}${sitePostfix}
rmdir ${pathToSite}${siteName}
exit 1
fi
# Restart Apache
echo "${green}Restarting Apache.${reset}"
apachectl restart >/dev/null
#clean Exit
echo "${green}#########################################################################"
echo ""
echo "${green}## ${yellow}Successfully added ${siteName}${sitePostfix} as a virtual server!${green}"
echo ""
echo "${green}#########################################################################${reset}"
return 0
#!/bin/sh
site=$1
#import Colors
source colors
#Check for Arg
if [ site == "" ]; then
echo "${red}Usage: ${yellow}ff5 <siteName>${reset}"
exit 1
fi
#Run NewSite.sh
source newSite -c $site "/src"
#Check to make sure that NewSite executed properly
if [ $? -ne 0 ]; then
exit $?
fi
echo "${yellow}Changing Directory to ${reset}${pathToSite}${siteName}"
cd ${pathToSite}${siteName}
#Run as sudo with the -u flag so that bower doesn't get upset.
sudo -u ${username} yo ase
sudo -u ${username} grunt make
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment