Skip to content

Instantly share code, notes, and snippets.

@jonathands
Forked from Adamwaheed/vhost.sh
Created May 28, 2020 21:46
Show Gist options
  • Save jonathands/1d58c98b5be977088d79e470fd4f3890 to your computer and use it in GitHub Desktop.
Save jonathands/1d58c98b5be977088d79e470fd4f3890 to your computer and use it in GitHub Desktop.
Simple Apache Vhost manager
#!/bin/bash
NONE='\033[00m'
RED='\033[01;31m'
GREEN='\033[01;32m'
YELLOW='\033[01;33m'
PURPLE='\033[01;35m'
CYAN='\033[01;36m'
WHITE='\033[01;37m'
BOLD='\033[1m'
UNDERLINE='\033[4m'
sitesEnabled="/etc/apache2/sites-enabled/"
sitesAvailable="/etc/apache2/sites-available/"
hostsFile="/etc/hosts"
echo -e "${WHITE}Apache${RED}VirtualHost${GREEN}Manager${NONE}"
echo -e "${RED}---------------------------------"
tput sgr0
PS3='Please enter your choice:'
options=("Create" "List" "Delete" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Create")
#-----------------------------------------------------
read -p $'\e[31mEnter Host Name\e[0m:' host
read -p $'\e[31mEnter Webroot\e[0m: ' root
root="/home/amin/web/"$root
dir="\"$root\""
template="<VirtualHost *:80>"
template+="\n ServerName $host"
template+="\n DocumentRoot "$root
template+="\n <Directory "$dir" >"
template+="\n Options Indexes MultiViews FollowSymLinks"
template+="\n AllowOverride All"
template+="\n Order allow,deny"
template+="\n Allow from all"
template+="\n Require all granted"
template+="\n </Directory>"
template+="\n </VirtualHost>"
mkdir -p $root
#createing vhost file in apache directoru
echo -e $template > "$sitesAvailable"$host".conf";
#rm "$sitesEnabled"/*
#cp "$sitesAvailable"$host".conf" "$sitesEnabled"
hostsnames="127.0.0.1 "
for entry in $sitesAvailable*
do
((count+=1))
hst=$(echo "$entry" | awk -F/ '{print $5}')
cl=$(echo ${hst::-5})
hostsnames+="$cl"
hostsnames+=" "
path=/"$count"-"$entry"
done
echo -e $hostsnames > "/etc/hosts";
rsync -r "$sitesAvailable"/* "$sitesEnabled"
sudo service apache2 restart
echo -e "\e[92mNew Domain Created"
tput sgr0
#Updateing hostfiles
#----------------------------------------------------
;;
"List")
search_dir="/etc/apache2/sites-available"
count=0;
for entry in "$search_dir"/*
do
((count+=1))
path=/"$count"-"$entry"
echo "$path" | awk -F'/' '{print $2 $6}'
done
;;
"Delete")
search_dir="/etc/apache2/sites-available"
#selected delete item
read -p $'\e[31mSelecet Host to delete\e[0m: ' deleteHost
count=0;
for entry in "$search_dir"/*
do
((count+=1))
if [ "$count" = "$deleteHost" ]; then
deletedRequested="$entry";
fi
done
if [ $count -lt $deleteHost ]; then
echo Host not found
else
read -p $'\e[31mAre you Sure (y/n):\e[0m: ' sure
if [ "$sure" = 'y' ]; then
rm "$deletedRequested";
#update host file
hostsnames="127.0.0.1 "
sites="/etc/apache2/sites-available"
for entry in "$sites"/*
do
((count+=1))
hostsnames+=$(echo "$entry" | awk -F/ '{print $5}')
hostsnames+=" "
done
echo -e $hostsnames > "/etc/hosts";
sudo service apache2 restart
echo -e "\e[92mNew Domain Deleted"
#update hostfile
else
echo -e "\e[91mCannot Delete"
tput sgr0
fi
#echo "$deletedRequested";
fi
;;
"Quit")
break
;;
*) echo invalid option;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment