Created
April 28, 2018 16:19
-
-
Save nawawishkid-old/5d3217e1ea1335ab6242000426576b71 to your computer and use it in GitHub Desktop.
Create Apache2 virtual host via bash
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 | |
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