Skip to content

Instantly share code, notes, and snippets.

@nawawishkid-old
Created April 28, 2018 16:19
Show Gist options
  • Save nawawishkid-old/5d3217e1ea1335ab6242000426576b71 to your computer and use it in GitHub Desktop.
Save nawawishkid-old/5d3217e1ea1335ab6242000426576b71 to your computer and use it in GitHub Desktop.
Create Apache2 virtual host via bash
#!/bin/bash
if [ $# -ne 5 ]; then
printf "\n\nสวัสดี $USER คุณอาจต้องการความช่วยเหลือ:\n\n"
cat /opt/kid/help.txt
printf "\n"
exit
fi
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-n|--name)
NAME="$2"
shift # past argument
shift # past value
;;
-r|--root)
ROOTDIR="$2"
shift # past argument
shift # past value
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
echo VHost name = "${NAME}"
echo Root directory = "${ROOTDIR}"
cd /etc/apache2/sites-available
sudo cp example.conf $NAME.conf
sudo sed -i "s,/var/www/example,/var/www/$ROOTDIR,g; s/example/$NAME/g" $NAME.conf
printf "Virtual host '$NAME' created.\n\n"
cat $NAME.conf
printf '\n\nMapping hostname...'
printf "\n\n# $NAME\n127.0.0.1 $NAME.local" | sudo tee -a /etc/hosts
printf '\n\nHostname mapped!\n'
sudo a2ensite $NAME
sudo service apache2 restart
echo Apache2 restarted!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment