Skip to content

Instantly share code, notes, and snippets.

@nathanieltubb
Last active May 30, 2017 01:53
Show Gist options
  • Save nathanieltubb/6b7c451be6acf2b3f93f237099b50ed1 to your computer and use it in GitHub Desktop.
Save nathanieltubb/6b7c451be6acf2b3f93f237099b50ed1 to your computer and use it in GitHub Desktop.
Bash Script to Add a Custom Localhost to Brew Apache Mac OSX
#!/bin/bash
# USAGE: addlocalhost mycustomsite.localhost /path/to/document/root/
HNAME=$1
HPATH=$2
HOSTSFILE="/etc/hosts"
HTTPSITESFILE="/usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf"
if ! [[ -n $HNAME || -n $HPATH ]]; then
echo "Please Enter Valid Hostname and Site Path"; exit 1;
fi
if ! [[ -e $HOSTSFILE || -e $HTTPSITESFILE ]]; then
echo "Problem With Hosts File Or HTTP Config"; exit 1;
fi
cat >> $HTTPSITESFILE << EOL
#<!-- START SITE ${HNAME} -->
<VirtualHost *:80>
ServerName ${HNAME}
DocumentRoot "${HPATH}"
<Directory "${HPATH}">
AllowOverride All
Require all granted
Options +Indexes
</Directory>
</VirtualHost>
#<!-- END SITE ${HNAME} -->
EOL
sudo tee -a ${HOSTSFILE} &> /dev/null << EOL
#<!-- START SITE ${HNAME} -->
127.0.0.1 ${HNAME}
#<!-- END SITE ${HNAME} -->
EOL
echo "-----"
echo "HTTPSITESFILE ADDED: ${HNAME} ${HPATH}"
# cat $HTTPSITESFILE
echo "-----"
echo "HOSTSFILE ADDED: 127.0.0.1 ${HNAME}"
# cat $HOSTSFILE
echo "-----"
echo "RESTARTING APACHE..."
sudo apachectl -k restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment