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")
echo "Enable Apache config for virtualhost."
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 " -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 " -o <option> Subdomain or Alias or Redirect URL if specified macro requires one."
echo " For Alias domains '-d <domain>' is the alias domain, and '-o <alias>'"
echo " is the existing VirtualHost to alias to."
echo " -o <option> Alias or Redirect URL if specified macro requires one."
echo " For Aliases & Redirects '-d <domain>' is the alias/redirect domain,"
echo " and '-o <option>' is the existing VirtualHost to alias/redirect to."
echo ""
echo " Available Apache Macros:"
echo " Available Apache Macros with examples:"
echo ""
# working on building autodetected macros
# needs work - remove $username, set other vars
# default_ifs="$IFS"
# IFS=""
# macro_help_array=($(grep Macro /etc/apache2/mods-available/macro.conf|cut -d ' ' -f 2-|cut -d \> -f 1|grep -v Macro))
# for each_macro in ${macro_help_array[*]}
# do
# echo "${each_macro}"
# done
# IFS="$default_ifs"
echo " vhost-enable.sh -m VHostHTTP -d example.com"
echo " vhost-enable.sh -m VHostHTTPS -d example.com"
echo " vhost-enable.sh -m VHostHTTPSVarnish -d example.com"
echo " vhost-enable.sh -m VHostSubdomainHTTP -d staging.example.com"
echo " vhost-enable.sh -m VHostSubdomainHTTPS -d staging.example.com"
echo " vhost-enable.sh -m VHostSubdomainHTTPSVarnish -d staging.example.com"
echo " vhost-enable.sh -m VMailHTTPS -d mail.example.com"
echo " vhost-enable.sh -m RedirectHTTP -d example.com -o https://www.example.org"
echo " vhost-enable.sh -m RedirectHTTPS -d example.com -o https://www.example.org"
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 " 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 ""
}
macro_array=($(grep Macro /etc/apache2/mods-available/macro.conf |cut -d ' ' -f 2|grep -v Macro))
macro_vhost_line="Use"
vhost:getoptions "$@"
# check for macro
if [[ -n $macro ]]; then
if [[ " ${macro_array[@]} " =~ " ${macro} " ]]; then
macro_vhost_line="$macro_vhost_line $macro"
macro_vhost_line="Use $macro"
else
echo "invalid macro name"
exit 1
@ -80,80 +58,84 @@ else
fi
# check for domain (virtualhost)
if [[ -n $domain ]]; then
macro_vhost_line="$macro_vhost_line $domain"
vhost_conf="$domain.conf"
else
if [[ ! -n $domain ]]; then
echo "domain is required"
exit
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
if [[ "$macro" == *"VHost"* ]]; then
# check for vhost dir
if [[ "$macro" == *"Alias"* ]]; then
vhost=$option
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
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
vhost=$domain
fi
if [[ -d "/srv/www/$vhost" ]]; then
# get and set $username
username=$(stat -c '%U' /srv/www/$vhost)
macro_vhost_line="$macro_vhost_line $username"
macro_vhost_line="$macro_vhost_line $vhost $username"
else
echo "VirtualHost dir for $vhost does not exist."
exit 1
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
if [[ "$macro" == *"Varnish"* ]]; then
varnish_host=$domain
if [[ "$macro" == *"Subdomain"* ]]; then
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"
if [[ ! -f "/etc/varnish/sites.d/$domain.vcl" ]]; then
echo "Varnish config file for $domain does not exist."
exit 1
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
# check for and set redirect
# check for redirect
if [[ "$macro" == *"Redirect"* ]]; then
if [[ -n $option ]]; then
redirect=$option
@ -170,19 +152,7 @@ if [[ "$macro" == *"Redirect"* ]]; then
fi
fi
# if https check for le cert
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
# create / update apache conf
echo "$macro_vhost_line" > /etc/apache2/sites-available/$domain.conf
# enable apache conf

View File

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