Skip to content

Instantly share code, notes, and snippets.

@nedmas
Created October 14, 2011 15:40
Show Gist options
  • Save nedmas/1287451 to your computer and use it in GitHub Desktop.
Save nedmas/1287451 to your computer and use it in GitHub Desktop.
Just a simple bash script for adding new sites on an apache server
#!/bin/bash
while getopts ":h:p:a:d:l:" opt; do
case $opt in
h)
HOST="$OPTARG"
;;
p)
PORT="$OPTARG"
;;
a)
SERVER_ADMIN="$OPTARG"
;;
d)
DOCUMENT_ROOT="$OPTARG"
;;
l)
LOG_DIR="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
*)
echo "$OPTARG" >&2
;;
esac
done
shift $((OPTIND-1))
if [ $# -lt 1 ]
then
echo "Usage : $0 [url]"
exit
fi
DIR=$(pwd)
IP=$(ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{print $1}')
HOST="${HOST:=$IP}"
PORT="${PORT:=80}"
SERVER_ADMIN="${SERVER_ADMIN:[email protected]}"
SERVER_NAME="${SERVER_NAME:=$1}"
SERVER_ALIAS="${SERVER_ALIAS:=www.$1}"
DOCUMENT_ROOT="${DOCUMENT_ROOT:=$DIR/$SERVER_NAME/public/}"
LOG_DIR="${LOG_DIR:=$DIR/$SERVER_NAME/logs/}"
ERROR_LOG="${LOG_DIR}error.log"
CUSTOM_LOG="${LOG_DIR}access.log combined"
FILE="/etc/apache2/sites-available/$SERVER_NAME"
echo "<VirtualHost $HOST:$PORT>" >> $FILE
echo " ServerAdmin $SERVER_ADMIN" >> $FILE
echo " ServerName $SERVER_NAME" >> $FILE
echo " ServerAlias $SERVER_ALIAS" >> $FILE
echo " DocumentRoot $DOCUMENT_ROOT" >> $FILE
echo " ErrorLog $ERROR_LOG" >> $FILE
echo " CustomLog $CUSTOM_LOG" >> $FILE
echo "</VirtualHost>" >> $FILE
mkdir -p $DOCUMENT_ROOT
FILE="${DOCUMENT_ROOT}index.html"
echo "<html>" >> $FILE
echo " <head>" >> $FILE
echo " <title>Site created</title>" >> $FILE
echo " </head>" >> $FILE
echo " <body>" >> $FILE
echo " <h1>Site created</h1>" >> $FILE
echo " <h2>$SERVER_NAME</h2>" >> $FILE
echo " </body>" >> $FILE
echo "</html>" >> $FILE
chown -R www-data:dev $DOCUMENT_ROOT
chmod -R 774 $DOCUMENT_ROOT
mkdir -p $LOG_DIR
chown -R www-data:dev $LOG_DIR
chmod -R 740 $LOG_DIR
a2ensite $SERVER_NAME
service apache2 reload
@joshangell
Copy link

joshangell commented Oct 15, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment