Last active
August 29, 2015 14:02
-
-
Save michaelbunch/89a5f52134fbe1dd01f0 to your computer and use it in GitHub Desktop.
Apache VirtualHost Builder for OSX
This file contains 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
#!/bin/bash | |
# --------------------------------------------------------------- | |
# Apache VirtualHost Builder for OSX | |
# | |
# This script tries to make it easier to add Apache VirtualHosts | |
# to the default install of OSX. For the script to run `as-is` | |
# two changes must me made to Apache. | |
# | |
# 1. Create a new folder in /etc/apache2 named vhosts | |
# 2. Add the following line to the end of the Apache config file | |
# /etc/apache2/httpd.conf | |
# | |
# Include /private/etc/apache2/vhosts/*.conf | |
# | |
# If you would like the scaffold to create the site files in a | |
# different location, change the SITEROOT variable below. | |
# | |
# IMPORTANT NOTE: This script will not autmatically add the | |
# hostname to the /etc/hosts file. You will need to do that | |
# manually. | |
# | |
# --------------------------------------------------------------- | |
# --------------------------------------------------------------- | |
# Validate the commandline options for the action and hostname. | |
# --------------------------------------------------------------- | |
if test -z "$1" | |
then | |
echo 'Please supply an action. [add|scaffold]' | |
exit 0 | |
else | |
if [[ "$1" -ne "add" ]] || [[ $1 -ne "scaffold" ]]; then | |
echo 'Please supply a valid action. [add|scaffold]' | |
exit 0 | |
fi | |
fi | |
if test -z "$2" | |
then | |
echo 'Please supply a domain. [sample.app]' | |
exit 0 | |
fi | |
# --------------------------------------------------------------- | |
# Set variables | |
# --------------------------------------------------------------- | |
# What action is being performed | |
ACTION=$1 | |
# What is the hostname for the VirtualHost | |
HOST=$2 | |
# Sets the user as the currently logged in user | |
USER=`whoami` | |
# Path to the VirtualHost directory | |
VHOST_PATH=/etc/apache2/vhosts | |
# Path of the webroot folder | |
SITEROOT=/Users/$USER/Projects/$HOST | |
# --------------------------------------------------------------- | |
# Define functions | |
# --------------------------------------------------------------- | |
# Create a new VirtualHost file unless the host already has one. | |
create_vhost() | |
{ | |
VHOST=$VHOST_PATH/${HOST//./_}.conf | |
if [[ -e "$VHOST" ]]; then | |
echo 'This VirtualHost already exists.' | |
exit 0 | |
else | |
VHOST_TMP=/Users/$USER/vhost.tmp | |
touch $VHOST_TMP | |
echo "NameVirtualHost $HOST:80" >> $VHOST_TMP | |
echo "Listen 80" >> $VHOST_TMP | |
echo "" >> $VHOST_TMP | |
echo "<VirtualHost $HOST:80>" >> $VHOST_TMP | |
echo " DocumentRoot $SITEROOT/public" >> $VHOST_TMP | |
echo " ServerName $HOST" >> $VHOST_TMP | |
echo " ServerAdmin admin@$HOST" >> $VHOST_TMP | |
echo " " >> $VHOST_TMP | |
echo " <Directory \"$SITEROOT/public\">" >> $VHOST_TMP | |
echo " Options Indexes FollowSymLinks Includes ExecCGI" >> $VHOST_TMP | |
echo " AllowOverride All" >> $VHOST_TMP | |
echo " Order Allow,Deny" >> $VHOST_TMP | |
echo " Allow from all" >> $VHOST_TMP | |
echo " </Directory>" >> $VHOST_TMP | |
echo "</VirtualHost>" >> $VHOST_TMP | |
sudo mv $VHOST_TMP $VHOST | |
fi | |
} | |
# Create the folders for the site in the users Projects folder. | |
create_site() | |
{ | |
if [[ -d "$SITEROOT" ]]; then | |
echo "This site already exists." | |
else | |
mkdir $SITEROOT | |
mkdir $SITEROOT/public | |
fi | |
} | |
# --------------------------------------------------------------- | |
# Fire functions based on the action supplied at the commandline. | |
# --------------------------------------------------------------- | |
if [[ "$ACTION" -eq "add" ]]; then | |
create_vhost | |
fi | |
if [[ "$ACTION" -eq "scaffold" ]]; then | |
create_vhost | |
create_site | |
fi | |
# --------------------------------------------------------------- | |
# Restart Apache to enable settings. | |
# --------------------------------------------------------------- | |
sudo /usr/sbin/httpd -k restart |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment