vhost-stack/bin/vhost-enable.sh
Matthew Saunders Brown e8570e1a82 reworked getopts
2021-10-05 11:33:24 -07:00

198 lines
6.1 KiB
Bash
Executable File

#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
# 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 <subdomain>|<alias>|<redirect_url>] [-h]"
echo ""
echo " -h Print this help."
echo " -d <domain> Domain name of VirtualHost to remove."
echo " -m <macro> Name of Apache macro to apply."
echo " -o <option> Subdomain or Alias or Redirect URL if specified macro requires one."
echo ""
echo " Available Apache Macros:"
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 ""
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"
else
echo "invalid macro name"
exit 1
fi
else
echo "macro is required"
exit
fi
# check for domain (virtualhost)
if [[ -n $domain ]]; then
macro_vhost_line="$macro_vhost_line $domain"
vhost_conf="$domain.conf"
else
echo "domain is required"
exit
fi
# set username for all VHost macros
if [[ "$macro" == *"VHost"* ]]; then
# check for vhost dir
if [[ -d "/srv/www/$domain" ]]; then
# get and set $username
username=$(stat -c '%U' /srv/www/$domain)
macro_vhost_line="$macro_vhost_line $username"
else
echo "VirtualHost dir for $domain 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/$alias" ]; then
echo "$alias 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"
exit 1
fi
fi
fi
# check for and set 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
# if https check for le cert
if [[ "$macro" == *"HTTPS"* ]]; then
if [[ "$macro" == *"Alias"* ]]; then
cert_host="$alias"
else
cert_host=$domain
if [[ "$macro" == *"Subdomain"* ]]; then
cert_host="$subdomain.$cert_host"
fi
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
# enable apache conf
if [[ ! -h /etc/apache2/sites-enabled/$domain_conf ]]; then
a2ensite --quiet $domain_conf
fi
# restart apache
if systemctl --quiet is-active apache2 ; then
if /usr/sbin/apachectl -t >/dev/null 2>&1 ; then
systemctl --quiet reload apache2
else
echo "apache config test failed, not doing restart"
exit 2
fi
fi