Last active
January 1, 2016 02:28
-
-
Save iansinnott/8079045 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This function does three things: | |
# 1. Cretes a directory for $sitename | |
# 2. Creates a hosts entry for $sitename | |
# 3. Creates a virtual-hosts entry for $sitename | |
# | |
# Note: Be careful with this function if you aren't comfortable | |
# with editing these system files yourself. If you enter a url you | |
# actiually use, say google.com, then whenever you try to go to | |
# google.com your browser will be pointed to your local google site | |
# at $HOME/Sites/google.com | |
# | |
# The usual way of appending to files would be a standard | |
# echo 'stuff' >> file.txt | |
# But that won't work as output redirection happens before sudo | |
# would be called. Thus sudo echo 'stuff' >> file.txt won't work | |
# and we need to use tee with the append flag. | |
function new_host { | |
sudo -v # Force user to have admin privelages | |
sitename=$1 # Rename var for clarity | |
# Set the vhosts entry. Note that indentation is important. Needs everything | |
# right up against the edge, otherwise the last 'ENTRY' line will print as well. | |
vhosts_entry=$(cat <<ENTRY | |
<VirtualHost *:80> | |
DocumentRoot "$HOME/Sites/$sitename" | |
ServerName $sitename | |
</VirtualHost> | |
ENTRY) | |
# 1. | |
echo "Creating directory $HOME/Sites/$sitename" | |
mkdir $HOME/Sites/$sitename | |
echo "" #newline | |
# 2. | |
echo "Creating new hosts entry at /private/etc/hosts:" | |
echo "127.0.0.1 $sitename" | sudo tee -a /private/etc/hosts | |
echo "" #newline | |
# 3. | |
echo "Creating new virtual-hosts entry at /private/etc/apache2/extra/httpd-vhosts.conf:" | |
echo "$vhosts_entry" | sudo tee -a /private/etc/apache2/extra/httpd-vhosts.conf | |
# Restart apache so changes will take effect | |
sudo apachectl restart | |
echo "Success! New local environment created at http://$sitename." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment