2021-04-04 13:28:22 -07:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# vhost-stack
|
|
|
|
# https://git.stack-source.com/msb/vhost-stack
|
2022-08-22 13:22:16 -07:00
|
|
|
# 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)
|
2021-04-04 13:28:22 -07:00
|
|
|
|
2021-04-04 14:15:16 -07:00
|
|
|
# load include file
|
|
|
|
source $(dirname $0)/vhost.sh
|
2021-04-04 13:28:22 -07:00
|
|
|
|
2021-09-16 16:21:35 -07:00
|
|
|
help()
|
|
|
|
{
|
|
|
|
thisfilename=$(basename -- "$0")
|
|
|
|
echo "Enable Apache config for virtualhost."
|
|
|
|
echo ""
|
2022-10-21 14:41:20 -07:00
|
|
|
echo "usage: $thisfilename -d <domain> -m <macro> [-o <alias>|<redirect_url>] [-h]"
|
2021-09-16 16:21:35 -07:00
|
|
|
echo ""
|
2021-10-05 11:33:24 -07:00
|
|
|
echo " -h Print this help."
|
2022-10-21 14:41:20 -07:00
|
|
|
echo " -d <domain> Domain name of VirtualHost to add."
|
2021-10-05 11:33:24 -07:00
|
|
|
echo " -m <macro> Name of Apache macro to apply."
|
2022-10-21 14:41:20 -07:00
|
|
|
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."
|
2021-09-16 16:21:35 -07:00
|
|
|
echo ""
|
2022-10-21 14:41:20 -07:00
|
|
|
echo " Available Apache Macros with examples:"
|
2021-09-16 16:21:35 -07:00
|
|
|
echo ""
|
2022-10-21 14:41:20 -07:00
|
|
|
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"
|
2021-09-16 16:21:35 -07:00
|
|
|
echo ""
|
2021-10-05 11:33:24 -07:00
|
|
|
echo " See /etc/apache2/mods-available/macro.conf for macro details."
|
2021-09-16 16:21:35 -07:00
|
|
|
echo ""
|
|
|
|
}
|
2021-04-04 13:28:22 -07:00
|
|
|
|
|
|
|
macro_array=($(grep Macro /etc/apache2/mods-available/macro.conf |cut -d ' ' -f 2|grep -v Macro))
|
|
|
|
|
2021-10-05 11:33:24 -07:00
|
|
|
vhost:getoptions "$@"
|
|
|
|
|
|
|
|
# check for macro
|
|
|
|
if [[ -n $macro ]]; then
|
|
|
|
if [[ " ${macro_array[@]} " =~ " ${macro} " ]]; then
|
2022-10-21 14:41:20 -07:00
|
|
|
macro_vhost_line="Use $macro"
|
2021-04-04 13:28:22 -07:00
|
|
|
else
|
2021-10-05 11:33:24 -07:00
|
|
|
echo "invalid macro name"
|
|
|
|
exit 1
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
else
|
2021-10-05 11:33:24 -07:00
|
|
|
echo "macro is required"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check for domain (virtualhost)
|
2022-10-21 14:41:20 -07:00
|
|
|
if [[ ! -n $domain ]]; then
|
2021-10-05 11:33:24 -07:00
|
|
|
echo "domain is required"
|
|
|
|
exit
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
|
2022-10-21 14:41:20 -07:00
|
|
|
# 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
|
|
|
|
|
2021-04-04 13:28:22 -07:00
|
|
|
# set username for all VHost macros
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ "$macro" == *"VHost"* ]]; then
|
2021-04-04 13:28:22 -07:00
|
|
|
# check for vhost dir
|
2021-10-05 13:26:12 -07:00
|
|
|
if [[ "$macro" == *"Alias"* ]]; then
|
2022-10-21 14:41:20 -07:00
|
|
|
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
|
2021-10-05 13:26:12 -07:00
|
|
|
else
|
|
|
|
vhost=$domain
|
2021-10-05 13:27:45 -07:00
|
|
|
fi
|
2021-10-05 13:26:12 -07:00
|
|
|
if [[ -d "/srv/www/$vhost" ]]; then
|
2021-04-04 13:28:22 -07:00
|
|
|
# get and set $username
|
2021-10-05 13:26:12 -07:00
|
|
|
username=$(stat -c '%U' /srv/www/$vhost)
|
2022-10-21 14:41:20 -07:00
|
|
|
macro_vhost_line="$macro_vhost_line $vhost $username"
|
2021-04-04 13:28:22 -07:00
|
|
|
else
|
2021-10-05 13:26:12 -07:00
|
|
|
echo "VirtualHost dir for $vhost does not exist."
|
2021-04-04 13:28:22 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
2022-10-21 14:41:20 -07:00
|
|
|
# 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."
|
2021-04-04 13:28:22 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2022-10-21 14:41:20 -07:00
|
|
|
# check for Alias option
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ "$macro" == *"Alias"* ]]; then
|
2022-10-21 14:41:20 -07:00
|
|
|
macro_vhost_line="$macro_vhost_line $domain"
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
2022-10-21 14:41:20 -07:00
|
|
|
# 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
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-10-21 14:41:20 -07:00
|
|
|
# check for redirect
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ "$macro" == *"Redirect"* ]]; then
|
|
|
|
if [[ -n $option ]]; then
|
|
|
|
redirect=$option
|
2021-04-04 13:28:22 -07:00
|
|
|
# make sure Redirect domain isn't already installed as it's own vhost
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ -d "/srv/www/$domain" ]]; then
|
|
|
|
echo "$domain is already installed as it's own vhost"
|
2021-04-04 13:28:22 -07:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
macro_vhost_line="$macro_vhost_line $redirect"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "redirect not set"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-10-21 14:41:20 -07:00
|
|
|
# create / update apache conf
|
2021-10-05 13:10:01 -07:00
|
|
|
echo "$macro_vhost_line" > /etc/apache2/sites-available/$domain.conf
|
2021-04-04 13:28:22 -07:00
|
|
|
|
|
|
|
# enable apache conf
|
2021-10-05 13:10:01 -07:00
|
|
|
if [[ ! -h /etc/apache2/sites-enabled/$domain.conf ]]; then
|
|
|
|
a2ensite --quiet $domain.conf
|
2021-04-04 13:28:22 -07:00
|
|
|
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
|