Skip to content

Instantly share code, notes, and snippets.

@Linnk
Created December 17, 2012 10:30
Show Gist options
  • Save Linnk/4317309 to your computer and use it in GitHub Desktop.
Save Linnk/4317309 to your computer and use it in GitHub Desktop.
Add a virtualhost for MAMP and local domain in /etc/hosts in one command.
#!/bin/bash
if [ -z $1 ]
then
read -e -p "Please enter your local domain name: " DOMAIN
if [ -z $DOMAIN ]
then
echo "You must specify a local domain name (example: name.dev)."
exit 1
fi
else
DOMAIN=$1
fi
if [ -z $2 ]
then
read -e -p "Enter the path for the virtualhost [$PWD]: " DIR
if [ -z $DIR ]
then
DIR=$PWD
fi
else
DIR=$2
fi
DOEXIST=`cat /etc/hosts | grep "$DOMAIN"`
if [ "$DOEXIST" != "" ]
then
echo ""
echo "This domain looks like it's already in /etc/hosts."
echo "Better check this manually. I'm a coward."
exit 1
fi
# INSERT ENTRY IN /ETC/HOSTS
line=`cat /etc/hosts | grep "MAMP VirtualHosts" -n`
arr=($(echo $line | tr ":" "\n"))
n=$[${arr[0]}+1]
sudo sed -i '' $n'i\
127.0.0.1 '$DOMAIN'
' /etc/hosts
echo "Added local redirection to \""$DOMAIN"\" in /etc/hosts"
# ADD VIRTUALHOST TO APACHE'S MAMP
echo "<VirtualHost *:80>
DocumentRoot $DIR/
ServerName $DOMAIN
</VirtualHost>" >> /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
echo "Added VirtualHost in /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf"
# RESTART APACHE'S MAMP
sudo /Applications/MAMP/bin/apache2/bin/apachectl restart
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment