Skip to content

Instantly share code, notes, and snippets.

@FranciscoG
Created September 20, 2018 23:35
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 FranciscoG/eec5643e9ad8d168fab7460ddfbb45d9 to your computer and use it in GitHub Desktop.
Save FranciscoG/eec5643e9ad8d168fab7460ddfbb45d9 to your computer and use it in GitHub Desktop.
Simple shell script to add a new site to my local macOS LAMP stack
#!/usr/bin/env bash
if [ "$EUID" -ne 0 ]
then echo "You must use sudo to run this script"
exit
fi
# Colors
# Black 0;30 Dark Gray 1;30
# Blue 0;34 Light Blue 1;34
# Green 0;32 Light Green 1;32
# Cyan 0;36 Light Cyan 1;36
# Red 0;31 Light Red 1;31
# Purple 0;35 Light Purple 1;35
# Brown/Orange 0;33 Yellow 1;33
# Light Gray 0;37 White 1;37
green='\033[0;32m'
red='\033[0;31m'
lgray='\033[0;37m'
NC='\033[0m' # No Color
WD="$(pwd | tr -d '\n')"
VHOSTS_DIR=/usr/local/apache2/conf/vhosts
TMPL=$VHOSTS_DIR/example.conf-template
# always remove the backup files that sed produces
function finish {
rm -f $VHOSTS_DIR/*.confbak
}
trap finish EXIT
#################################################
# Argument checks
#
if [ -z "$1" ]
then
echo -e "${green}site name:${NC}"
read SITENAME
else
SITENAME=$2
fi
# if still no site name, then we exit with error
if [ -z "$SITENAME" ]; then
echo "you must specify a site name"
exit 1
fi
NEWCONF=$VHOSTS_DIR/$SITENAME.conf
if [ -z "$2" ]
then
echo -e "${green}enter a docroot or press enter to use the current directory: ${lgray}[${WD}]${NC}"
read PROMPTROOT
# handle the default PWD if user just pressed enter during prompt
if [[ -z "$PROMPTROOT" ]];
then DOCROOT="$WD"
else
DOCROOT="$PROMPTROOT"
fi
else
DOCROOT=$2
fi
#################################################
# Make CONF file from template
#
# copy the template conf to the new sitename.conf
\cp "$TMPL" "$NEWCONF"
# things to replace in the new conf:
# %ROOT%
# %NAME%
sed -i bak -e "s,%ROOT%,$DOCROOT,g" "$NEWCONF"
sed -i bak -e "s,%NAME%,$SITENAME,g" "$NEWCONF"
#################################################
# Add domain to hosts files
#
HOSTCHECK="$(grep $SITENAME /etc/hosts)"
if [ -z "$HOSTCHECK" ]; then
# add new domain to /etc/hosts file
echo "127.0.0.1 $SITENAME.fgmac.com" >> /etc/hosts
fi
#################################################
# Add new link to the nav page so I can remember
# all the sites I have added
echo "<a target=\"_blank\" href=\"http://$SITENAME.fgmac.com\">$SITENAME</a>" >> ~/projects/fgmac/index.html
#################################################
# restart apache and complete
#
apachectl restart
# wait a couple seconds for the restart to complete
sleep 2
# open the new site
open http://$SITENAME.fgmac.com
exit 0
<VirtualHost *:80>
DocumentRoot "%ROOT%"
ServerName %NAME%.fgmac.com
</VirtualHost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment