Skip to content

Instantly share code, notes, and snippets.

@tinker1987
Created January 14, 2017 16:27
Show Gist options
  • Save tinker1987/916e8c5f7ada218a35b90d1c78328af3 to your computer and use it in GitHub Desktop.
Save tinker1987/916e8c5f7ada218a35b90d1c78328af3 to your computer and use it in GitHub Desktop.
#!/bin/bash
VHOSTS_CONTAINER_FILE="/etc/apache2/sites-available/vhosts.conf"
HOSTS_FILE="/etc/hosts"
CREATE_SYMLINK="N"
HOME_DIR=""
while [[ $# -gt 1 ]]
do
key="$1"
case $key in
--document-root)
DOCUMENT_ROOT="$2"
;;
--home-dir)
HOME_DIR="$2";
CREATE_SYMLINK="Y";
;;
--host)
HOST_NAME="$2"
;;
*)
;;
esac
shift # past argument or value
done
CONFIG_FILE="/etc/apache2/sites-available/${HOST_NAME}.config"
DOCUMENT_ROOT="/var/www/${HOST_NAME}"
if [ "${HOME_DIR}" == "" ]; then
HOME_DIR="/home/$USER/Development/${HOST_NAME}/"
fi
# ===================================================================
# Creating symlink to /var/www
if [ "${CREATE_SYMLINK}" == "Y" ]; then
ln -snf $HOME_DIR $DOCUMENT_ROOT
fi
if [ "$(grep -E "(\s|\t)*Include(\s|\t)*${CONFIG_FILE}(\s|\t|$)" "${VHOSTS_CONTAINER_FILE}")" == "" ]; then
touch $CONFIG_FILE
text='<VirtualHost *:80>\n'
text+='\tServerName '${HOST_NAME}'\n'
text+='\tDocumentRoot '${DOCUMENT_ROOT}'\n'
text+='\t<Directory '${DOCUMENT_ROOT}'>\n'
text+='\t\tAllowOverride All\n'
text+='\t\tRequire all granted\n'
text+='\t</Directory>\n'
text+='</VirtualHost>\n'
echo -e $text > $CONFIG_FILE
chmod 0777 $CONFIG_FILE
echo "Include ${CONFIG_FILE}" >> $VHOSTS_CONTAINER_FILE
echo -e "Created ${HOST_NAME} config\n";
fi
service apache2 restart
if [ "$(grep -E "(\s|\t)*127\.0\.0\.1(\s|\t)*${HOST_NAME}(\s|\t|$)" "${HOSTS_FILE}")" == "" ]; then
echo "127.0.0.1 ${HOST_NAME}" >> $HOSTS_FILE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment