Created
October 10, 2014 07:09
-
-
Save connected/b5ac3e10c1cf88ba5d07 to your computer and use it in GitHub Desktop.
Create host
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
#!/bin/bash | |
set -e | |
NO_ARGS=0 | |
E_OPTERROR=85 | |
SERVER_ROOT_DOCUMENT_PATH=/home/andrew/Hosts | |
DOMAIN="" # ".lh" | |
OWNER='andrew:andrew' | |
usage() { | |
echo "Usage: `basename $0` -H test [-r public]" | |
echo "-H Hostname" | |
echo "-r Document root (Default: public)" | |
echo "-h This help" | |
exit $E_OPTERROR | |
} | |
if [ $# -eq "$NO_ARGS" ]; then | |
usage | |
fi | |
while getopts ":H:r:" o; do | |
case "${o}" in | |
H) | |
hostname=${OPTARG} | |
;; | |
r) | |
root_path=${OPTARG} | |
;; | |
h) | |
usage | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
if [ -z $hostname ]; then | |
usage | |
fi | |
if [ -z $root_path ]; then | |
root_path=public | |
fi | |
if [ "$EUID" -ne 0 ] | |
then echo "Please run as root" | |
exit | |
fi | |
if [ -f "$SERVER_ROOT_DOCUMENT_PATH/$hostname" ] || [ -d "$SERVER_ROOT_DOCUMENT_PATH/$hostname" ]; then | |
echo "[ERROR] Directory exists [$SERVER_ROOT_DOCUMENT_PATH/$hostname]" | |
exit -1 | |
fi | |
mkdir -p "$SERVER_ROOT_DOCUMENT_PATH/$hostname/$root_path" | |
echo "Hello, World" > "$SERVER_ROOT_DOCUMENT_PATH/$hostname/$root_path/index.php" | |
chown $OWNER "$SERVER_ROOT_DOCUMENT_PATH/$hostname" -R | |
VHOST_PATTERN=$(cat <<"EOF" | |
<VirtualHost *:80> | |
ServerName SERVER_NAME | |
ServerAdmin webmaster@localhost | |
DocumentRoot DOCUMENT_ROOT | |
<Directory DOCUMENT_ROOT> | |
Options All | |
AllowOverride All | |
Require all granted | |
</Directory> | |
ErrorLog /var/log/apache2/HOSTNAME-error.log | |
</VirtualHost> | |
EOF | |
) | |
SERVER_NAME=$hostname$DOMAIN | |
DOCUMENT_ROOT=$SERVER_ROOT_DOCUMENT_PATH/$hostname/$root_path | |
VHOST_PATTERN=$(echo "$VHOST_PATTERN" | sed -e "s@SERVER_NAME@$SERVER_NAME@g") | |
VHOST_PATTERN=$(echo "$VHOST_PATTERN" | sed -e "s@DOCUMENT_ROOT@$DOCUMENT_ROOT@g") | |
VHOST_PATTERN=$(echo "$VHOST_PATTERN" | sed -e "s@HOSTNAME@$hostname@g") | |
echo "$VHOST_PATTERN" > "/etc/apache2/sites-available/$hostname.conf" | |
ln -s "/etc/apache2/sites-available/$hostname.conf" "/etc/apache2/sites-enabled/$hostname.conf" | |
echo "127.0.0.1 $SERVER_NAME" | tee -a /etc/hosts > /dev/null | |
service apache2 restart > /dev/null | |
echo "[INFO] Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment