clean up and rework vhost-enable.sh

This commit is contained in:
Matthew Saunders Brown 2022-10-21 14:41:20 -07:00
parent a40e7b73db
commit 980574bdc1
2 changed files with 87 additions and 117 deletions

View File

@ -13,63 +13,41 @@ help()
thisfilename=$(basename -- "$0") thisfilename=$(basename -- "$0")
echo "Enable Apache config for virtualhost." echo "Enable Apache config for virtualhost."
echo "" echo ""
echo "usage: $thisfilename -d <domain> -m <macro> [-o <subdomain>|<alias>|<redirect_url>] [-h]" echo "usage: $thisfilename -d <domain> -m <macro> [-o <alias>|<redirect_url>] [-h]"
echo "" echo ""
echo " -h Print this help." echo " -h Print this help."
echo " -d <domain> Domain name of VirtualHost to remove." echo " -d <domain> Domain name of VirtualHost to add."
echo " -m <macro> Name of Apache macro to apply." echo " -m <macro> Name of Apache macro to apply."
echo " -o <option> Subdomain or Alias or Redirect URL if specified macro requires one." echo " -o <option> Alias or Redirect URL if specified macro requires one."
echo " For Alias domains '-d <domain>' is the alias domain, and '-o <alias>'" echo " For Aliases & Redirects '-d <domain>' is the alias/redirect domain,"
echo " is the existing VirtualHost to alias to." echo " and '-o <option>' is the existing VirtualHost to alias/redirect to."
echo "" echo ""
echo " Available Apache Macros:" echo " Available Apache Macros with examples:"
echo "" echo ""
# working on building autodetected macros echo " vhost-enable.sh -m VHostHTTP -d example.com"
# needs work - remove $username, set other vars echo " vhost-enable.sh -m VHostHTTPS -d example.com"
# default_ifs="$IFS" echo " vhost-enable.sh -m VHostHTTPSVarnish -d example.com"
# IFS="" echo " vhost-enable.sh -m VHostSubdomainHTTP -d staging.example.com"
# macro_help_array=($(grep Macro /etc/apache2/mods-available/macro.conf|cut -d ' ' -f 2-|cut -d \> -f 1|grep -v Macro)) echo " vhost-enable.sh -m VHostSubdomainHTTPS -d staging.example.com"
# for each_macro in ${macro_help_array[*]} echo " vhost-enable.sh -m VHostSubdomainHTTPSVarnish -d staging.example.com"
# do echo " vhost-enable.sh -m VMailHTTPS -d mail.example.com"
# echo "${each_macro}" echo " vhost-enable.sh -m RedirectHTTP -d example.com -o https://www.example.org"
# done echo " vhost-enable.sh -m RedirectHTTPS -d example.com -o https://www.example.org"
# IFS="$default_ifs" echo " vhost-enable.sh -m VHostAliasHTTP -d example.com -o example.org"
echo " vhost-enable.sh -m VHostAliasHTTPS -d example.com -o example.org"
echo "" echo ""
echo " Usage examples:"
echo " vhost-enable.sh VHostHTTPS example.com"
echo " vhost-enable.sh VHostSubdomainHTTPS example.com staging"
echo " vhost-enable.sh -d example.com(exising Vhost) -m VHostAliasHTTPS -o alias(site to alias to existing vhost)"
echo " vhost-enable.sh VMailHTTPS mail.example.com"
echo " vhost-enable.sh RedirectHTTPS example.com https://my.newsite.com/path/page.html"
echo ""
echo " Apache mod_macro config will look like:"
echo ' Use VHostHTTP $domain $username'
echo ' Use VHostHTTPS $domain $username'
echo ' Use VHostHTTPSVarnish $domain $username'
echo ' Use VHostSubdomainHTTP $domain $username $subdomain'
echo ' Use VHostSubdomainHTTPS $domain $username $subdomain'
echo ' Use VHostSubdomainHTTPSVarnish $domain $username $subdomain'
echo ' Use VHostAliasHTTP $domain $username $alias'
echo ' Use VHostAliasHTTPS $domain $username $alias'
echo ' Use VHostAliasHTTPSVarnish $domain $username $alias'
echo ' Use VMailHTTPS $domain'
echo ' Use RedirectHTTP $domain $redirect'
echo ' Use RedirectHTTPS $domain $redirect'
echo ''
echo ' $username is autodetected from vhost dir ownership'
echo " See /etc/apache2/mods-available/macro.conf for macro details." echo " See /etc/apache2/mods-available/macro.conf for macro details."
echo "" echo ""
} }
macro_array=($(grep Macro /etc/apache2/mods-available/macro.conf |cut -d ' ' -f 2|grep -v Macro)) macro_array=($(grep Macro /etc/apache2/mods-available/macro.conf |cut -d ' ' -f 2|grep -v Macro))
macro_vhost_line="Use"
vhost:getoptions "$@" vhost:getoptions "$@"
# check for macro # check for macro
if [[ -n $macro ]]; then if [[ -n $macro ]]; then
if [[ " ${macro_array[@]} " =~ " ${macro} " ]]; then if [[ " ${macro_array[@]} " =~ " ${macro} " ]]; then
macro_vhost_line="$macro_vhost_line $macro" macro_vhost_line="Use $macro"
else else
echo "invalid macro name" echo "invalid macro name"
exit 1 exit 1
@ -80,80 +58,84 @@ else
fi fi
# check for domain (virtualhost) # check for domain (virtualhost)
if [[ -n $domain ]]; then if [[ ! -n $domain ]]; then
macro_vhost_line="$macro_vhost_line $domain"
vhost_conf="$domain.conf"
else
echo "domain is required" echo "domain is required"
exit exit
fi fi
# if https check for le cert
if [[ "$macro" == *"HTTPS"* ]]; then
if [[ ! -f "/etc/ssl/letsencrypt/$domain.pem" ]]; then
echo "cert file for $domain does not exist"
exit 1
fi
fi
# set username for all VHost macros # set username for all VHost macros
if [[ "$macro" == *"VHost"* ]]; then if [[ "$macro" == *"VHost"* ]]; then
# check for vhost dir # check for vhost dir
if [[ "$macro" == *"Alias"* ]]; then if [[ "$macro" == *"Alias"* ]]; then
if [[ -d "/srv/www/$domain" ]]; then
echo "$domain is already installed as it's own vhost."
exit 1
else
if [[ -n $option ]]; then
vhost=$option vhost=$option
else
echo "option (existing virtualhost) not set"
exit 1
fi
fi
elif [[ "$macro" == *"Subdomain"* ]]; then
subdomain=$(echo $domain|cut -d '.' -f 1)
vhost=$(echo $domain|cut -d '.' -f 2-)
if [[ ! -d "/srv/www/$vhost/html/$subdomain" ]]; then
echo "Subdomain directory (/srv/www/$vhost/html/$subdomain) does not exist, create that first."
exit 1
elif [[ -d "/srv/www/$domain" ]]; then
echo "$domain is already installed as it's own VirtualHost."
exit 1
fi
else else
vhost=$domain vhost=$domain
fi fi
if [[ -d "/srv/www/$vhost" ]]; then if [[ -d "/srv/www/$vhost" ]]; then
# get and set $username # get and set $username
username=$(stat -c '%U' /srv/www/$vhost) username=$(stat -c '%U' /srv/www/$vhost)
macro_vhost_line="$macro_vhost_line $username" macro_vhost_line="$macro_vhost_line $vhost $username"
else else
echo "VirtualHost dir for $vhost does not exist." echo "VirtualHost dir for $vhost does not exist."
exit 1 exit 1
fi fi
# check for and set Subdomain
if [[ "$macro" == *"Subdomain"* ]]; then
if [[ -n $option ]]; then
subdomain=$option
# make sure Subdomain isn't already installed
if [[ -d "/srv/www/$subdomain.$domain" ]]; then
echo "$subdomain.$domain is already installed as it's own VirtualHost."
exit 1
fi
if [[ ! -d "/srv/www/$domain/$subdomain" ]]; then
echo "Subdomain directory (/srv/www/$domain/$subdomain) does not exist"
exit 1
fi
macro_vhost_line="$macro_vhost_line $subdomain"
vhost_conf="$subdomain.$domain.conf"
else
echo "subdomain (-o OPTION) not set"
exit 1
fi
fi
# check for and set Alias
if [[ "$macro" == *"Alias"* ]]; then
if [ -n $option ]; then
alias=$option
# make sure Alias domain isn't already installed as it's own vhost
if [ -d "/srv/www/$domain" ]; then
echo "$domain is already installed as it's own vhost"
exit 1
else
macro_vhost_line="$macro_vhost_line $alias"
fi
else
echo "alias not set"
exit 1
fi
fi
# check for varnish config # check for varnish config
if [[ "$macro" == *"Varnish"* ]]; then if [[ "$macro" == *"Varnish"* ]]; then
varnish_host=$domain if [[ ! -f "/etc/varnish/sites.d/$domain.vcl" ]]; then
if [[ "$macro" == *"Subdomain"* ]]; then echo "Varnish config file for $domain does not exist."
varnish_host="$subdomain.$varnish_host"
fi
if [[ ! -f "/etc/varnish/sites.d/$varnish_host.vcl" ]]; then
echo "$varnish_config_file Varnish config file does not exist"
exit 1 exit 1
fi fi
fi fi
# check for Alias option
if [[ "$macro" == *"Alias"* ]]; then
macro_vhost_line="$macro_vhost_line $domain"
fi
# check for Subdomain
if [[ "$macro" == *"Subdomain"* ]]; then
macro_vhost_line="$macro_vhost_line $subdomain"
fi
else
macro_vhost_line="$macro_vhost_line $domain"
fi
# check for Mail domain
if [[ "$macro" == "VMailHTTPS" ]]; then
maildomain=$(echo $domain|cut -d '.' -f 2-)
if [[ ! -d /var/vmail/$maildomain ]]; then
echo "Email for $maildomain not enabled on this server."
exit 1
fi
fi fi
# check for and set redirect # check for redirect
if [[ "$macro" == *"Redirect"* ]]; then if [[ "$macro" == *"Redirect"* ]]; then
if [[ -n $option ]]; then if [[ -n $option ]]; then
redirect=$option redirect=$option
@ -170,19 +152,7 @@ if [[ "$macro" == *"Redirect"* ]]; then
fi fi
fi fi
# if https check for le cert # create / update apache conf
if [[ "$macro" == *"HTTPS"* ]]; then
cert_host=$domain
if [[ "$macro" == *"Subdomain"* ]]; then
cert_host="$subdomain.$cert_host"
fi
if [[ ! -f "/etc/ssl/letsencrypt/$cert_host.pem" ]]; then
echo "cert file for $cert_host does not exist"
exit 1
fi
fi
# create / edit apache conf
echo "$macro_vhost_line" > /etc/apache2/sites-available/$domain.conf echo "$macro_vhost_line" > /etc/apache2/sites-available/$domain.conf
# enable apache conf # enable apache conf

View File

@ -267,13 +267,13 @@
<Macro VHostAliasHTTP $vhost $username $alias> <Macro VHostAliasHTTP $vhost $username $alias>
<VirtualHost *:80> <VirtualHost *:80>
ServerName $vhost ServerName $alias
ServerAlias www.$vhost ServerAlias www.$alias
<IfDefine AliasDomain> <IfDefine AliasDomain>
ServerAlias $vhost.${AliasDomain} ServerAlias $alias.${AliasDomain}
</IfDefine> </IfDefine>
DocumentRoot /srv/www/$alias/html DocumentRoot /srv/www/$vhost/html
ScriptAlias /cgi-wrap/ "/usr/local/lib/cgi-wrap/$alias/" ScriptAlias /cgi-wrap/ "/usr/local/lib/cgi-wrap/$vhost/"
RewriteEngine on RewriteEngine on
RewriteCond %{REQUEST_URI} ^/cgi-bin/.* RewriteCond %{REQUEST_URI} ^/cgi-bin/.*
RewriteRule ^/cgi-bin/(.*) /cgi-wrap/cgiwrap/$username/$1 [PT] RewriteRule ^/cgi-bin/(.*) /cgi-wrap/cgiwrap/$username/$1 [PT]
@ -285,13 +285,13 @@
<Macro VHostAliasHTTPS $vhost $username $alias> <Macro VHostAliasHTTPS $vhost $username $alias>
<VirtualHost *:80> <VirtualHost *:80>
ServerName $vhost ServerName $alias
ServerAlias www.$vhost ServerAlias www.$alias
<IfDefine AliasDomain> <IfDefine AliasDomain>
ServerAlias $vhost.${AliasDomain} ServerAlias $alias.${AliasDomain}
</IfDefine> </IfDefine>
DocumentRoot /srv/www/$alias/html DocumentRoot /srv/www/$vhost/html
ScriptAlias /cgi-wrap/ "/usr/local/lib/cgi-wrap/$alias/" ScriptAlias /cgi-wrap/ "/usr/local/lib/cgi-wrap/$vhost/"
RewriteEngine on RewriteEngine on
RewriteCond %{REQUEST_URI} ^/cgi-bin/.* RewriteCond %{REQUEST_URI} ^/cgi-bin/.*
RewriteRule ^/cgi-bin/(.*) /cgi-wrap/cgiwrap/$username/$1 [PT] RewriteRule ^/cgi-bin/(.*) /cgi-wrap/cgiwrap/$username/$1 [PT]
@ -307,13 +307,13 @@
</FilesMatch> </FilesMatch>
</VirtualHost> </VirtualHost>
<VirtualHost *:443> <VirtualHost *:443>
ServerName $vhost ServerName $alias
ServerAlias www.$vhost ServerAlias www.$alias
<IfDefine AliasDomain> <IfDefine AliasDomain>
ServerAlias $vhost.${AliasDomain} ServerAlias $alias.${AliasDomain}
</IfDefine> </IfDefine>
DocumentRoot /srv/www/$alias/html DocumentRoot /srv/www/$vhost/html
ScriptAlias /cgi-wrap/ "/usr/local/lib/cgi-wrap/$alias/" ScriptAlias /cgi-wrap/ "/usr/local/lib/cgi-wrap/$vhost/"
RewriteEngine on RewriteEngine on
RewriteCond %{REQUEST_URI} ^/cgi-bin/.* RewriteCond %{REQUEST_URI} ^/cgi-bin/.*
RewriteRule ^/cgi-bin/(.*) /cgi-wrap/cgiwrap/$username/$1 [PT] RewriteRule ^/cgi-bin/(.*) /cgi-wrap/cgiwrap/$username/$1 [PT]