vhost-stack/bin/vhost-enable.sh
2023-05-17 15:48:00 -07:00

170 lines
5.4 KiB
Bash
Executable File

#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# load include file
source $(dirname $0)/vhost.sh
help()
{
thisfilename=$(basename -- "$0")
echo "Enable Apache config for virtualhost."
echo ""
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 add."
echo " -m <macro> Name of Apache macro to apply. Optional, script will attempt to autoselect if possible."
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 with examples:"
echo ""
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 " 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))
vhost:getoptions "$@"
# check for domain (virtualhost)
if [[ ! -n $domain ]]; then
echo "domain is required"
exit
fi
# autodetect macro to use if not set
if [[ ! -n $macro ]]; then
if [[ "$domain" =~ ^mail.* ]]; then
macro=VMailHTTPS
elif [[ -f "/etc/ssl/letsencrypt/$domain.pem" ]]; then
macro=VHostHTTPS
else
macro=VHostHTTP
fi
fi
# verify macro name
if [[ " ${macro_array[@]} " =~ " ${macro} " ]]; then
macro_vhost_line="Use $macro"
else
echo "invalid macro name"
exit 1
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
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 $vhost $username"
else
echo "VirtualHost dir for $vhost does not exist."
exit 1
fi
# check for varnish config
if [[ "$macro" == *"Varnish"* ]]; then
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 redirect
if [[ "$macro" == *"Redirect"* ]]; then
if [[ -n $option ]]; then
redirect=$option
# make sure Redirect 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 $redirect"
fi
else
echo "redirect not set"
exit 1
fi
fi
# create / update apache conf
echo "$macro_vhost_line" > /etc/apache2/sites-available/$domain.conf
# enable apache conf
if [[ -h /etc/apache2/sites-enabled/$domain.conf ]]; then
# modify timestamp on existing symlink to trigger apache restart
touch --no-dereference /etc/apache2/sites-enabled/$domain.conf
else
a2ensite --quiet $domain.conf
fi