From 89bb83c2fb0227b29306195121bc7798af5ea776 Mon Sep 17 00:00:00 2001 From: souparno majumder Date: Sat, 21 Apr 2018 15:26:50 +0530 Subject: [PATCH 1/2] virtual host now supports apache proxy port creation --- virtualhost.sh | 123 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 1 deletion(-) diff --git a/virtualhost.sh b/virtualhost.sh index 2bff5fa..01fd50d 100755 --- a/virtualhost.sh +++ b/virtualhost.sh @@ -6,6 +6,7 @@ TEXTDOMAIN=virtualhost action=$1 domain=$2 rootDir=$3 +port=$4 owner=$(who am i | awk '{print $1}') email='webmaster@localhost' sitesEnable='/etc/apache2/sites-enabled/' @@ -20,7 +21,7 @@ if [ "$(whoami)" != 'root' ]; then exit 1; fi -if [ "$action" != 'create' ] && [ "$action" != 'delete' ] +if [ "$action" != 'create' ] && [ "$action" != 'delete' ] && [ "$action" != 'create-proxy' ] then echo $"You need to prompt for action (create or delete) -- Lower-case only" exit 1; @@ -43,6 +44,126 @@ fi rootDir=$userDir$rootDir +if [ "$action" == 'create-proxy' ] + then + ### check if domain already exists + if [ -e $sitesAvailabledomain ]; then + echo -e $"This domain already exists.\nPlease Try Another one" + exit; + fi + + ### check if directory exists or not + if ! [ -d $rootDir ]; then + ### create the directory + mkdir $rootDir + ### give permission to root dir + chmod 755 $rootDir + ### write test file in the new domain dir + if ! echo "" > $rootDir/phpinfo.php + then + echo $"ERROR: Not able to write in file $rootDir/phpinfo.php. Please check permissions" + exit; + else + echo $"Added content to $rootDir/phpinfo.php" + fi + fi + + ### create virtual host rules file + if ! echo " + + ServerAdmin $email + ServerName $domain + ServerAlias $domain + DocumentRoot $rootDir + + Options -Indexes +FollowSymLinks + AllowOverride None + Require all granted + + ProxyPreserveHost On + ProxyVia Full + + Require all granted + + ProxyPass / http://127.0.0.1:$port/ + ProxyPassReverse / http://127.0.0.1:$port/ + ErrorLog /var/log/apache2/$domain-error.log + LogLevel error + CustomLog /var/log/apache2/$domain-access.log combined + " > $sitesAvailabledomain + then + echo -e $"There is an ERROR creating $domain file" + exit; + else + echo -e $"\nNew Virtual Host Created\n" + fi + + ### Add domain in /etc/hosts + if ! echo "127.0.0.1 $domain" >> /etc/hosts + then + echo $"ERROR: Not able to write in /etc/hosts" + exit; + else + echo -e $"Host added to /etc/hosts file \n" + fi + + if [ "$owner" == "" ]; then + chown -R $(whoami):$(whoami) $rootDir + else + chown -R $owner:$owner $rootDir + fi + + ### enable website + a2ensite $domain + + ### restart Apache + /etc/init.d/apache2 reload + + ### show the finished message + echo -e $"Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $rootDir" + exit; + + else + ### check whether domain already exists + if ! [ -e $sitesAvailabledomain ]; then + echo -e $"This domain does not exist.\nPlease try another one" + exit; + else + ### Delete domain in /etc/hosts + newhost=${domain//./\\.} + sed -i "/$newhost/d" /etc/hosts + + ### disable website + a2dissite $domain + + ### restart Apache + /etc/init.d/apache2 reload + + ### Delete virtual host rules files + rm $sitesAvailabledomain + fi + + ### check if directory exists or not + if [ -d $rootDir ]; then + echo -e $"Delete host root directory ? (y/n)" + read deldir + + if [ "$deldir" == 'y' -o "$deldir" == 'Y' ]; then + ### Delete the directory + rm -rf $rootDir + echo -e $"Directory deleted" + else + echo -e $"Host directory conserved" + fi + else + echo -e $"Host directory not found. Ignored" + fi + + ### show the finished message + echo -e $"Complete!\nYou just removed Virtual Host $domain" + exit 0; +fi + if [ "$action" == 'create' ] then ### check if domain already exists From 4739e3edf8b201868484ff4d7e395117147bf7c5 Mon Sep 17 00:00:00 2001 From: souparno majumder Date: Fri, 4 May 2018 16:43:18 +0530 Subject: [PATCH 2/2] create-proxy command is added to help --- virtualhost.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/virtualhost.sh b/virtualhost.sh index 01fd50d..8b1a19f 100755 --- a/virtualhost.sh +++ b/virtualhost.sh @@ -23,7 +23,7 @@ fi if [ "$action" != 'create' ] && [ "$action" != 'delete' ] && [ "$action" != 'create-proxy' ] then - echo $"You need to prompt for action (create or delete) -- Lower-case only" + echo $"You need to prompt for action (create or create-proxy or delete) -- Lower-case only" exit 1; fi