vhost-stack/bin/vhost-enable.sh
2021-04-04 14:15:16 -07:00

166 lines
4.5 KiB
Bash

#!/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
# vhost-enable.sh macro_name vhost [subdomain|alias]
# vhost-enable.sh VHostHTTPS example.com
# vhost-enable.sh VHostSubdomainHTTPS example.com staging
# vhost-enable.sh VHostAliasHTTPS example.com existingsite
# vhost-enable.sh VMailHTTPS mail.example.com
# vhost-enable.sh RedirectHTTPS example.com https://my.newsite.com/path/page.html
#
# mod_macro config will look like:
# Use VHostHTTP $vhost $username
# Use VHostHTTPS $vhost $username
# Use VHostHTTPSVarnish $vhost $username
# Use VHostSubdomainHTTP $vhost $username $subdomain
# Use VHostSubdomainHTTPS $vhost $username $subdomain
# Use VHostSubdomainHTTPSVarnish $vhost $username $subdomain
# Use VHostAliasHTTP $vhost $username $alias
# Use VHostAliasHTTPS $vhost $username $alias
# Use VHostAliasHTTPSVarnish $vhost $username $alias
# Use VMailHTTPS $vhost
# Use RedirectHTTP $vhost $redirect
# Use RedirectHTTPS $vhost $redirect
#
# $username is autodetected from vhost dir ownership
macro_array=($(grep Macro /etc/apache2/mods-available/macro.conf |cut -d ' ' -f 2|grep -v Macro))
macro_vhost_line="Use"
# check for and set mode
if [ -n "$1" ]; then
macro_name=$1
if [[ " ${macro_array[@]} " =~ " ${macro_name} " ]]; then
macro_vhost_line="$macro_vhost_line $macro_name"
else
echo "invalid macro name"
exit 1
fi
else
echo "macro name not set"
exit 1
fi
# check for and set vhost
if [ -n "$2" ]; then
vhost=$2
macro_vhost_line="$macro_vhost_line $vhost"
vhost_conf="$vhost.conf"
else
echo "vhost not set"
exit 1
fi
# set username for all VHost macros
if [[ "$macro_name" == *"VHost"* ]]; then
# check for vhost dir
if [ -d "/srv/www/$vhost" ]; then
# get and set $username
username=$(stat -c '%U' /srv/www/$vhost)
macro_vhost_line="$macro_vhost_line $username"
else
echo "vhost dir for $vhost does not exist"
exit 1
fi
# check for and set Subdomain
if [[ "$macro_name" == *"Subdomain"* ]]; then
if [ -n "$3" ]; then
subdomain=$3
# make sure Subdomain isn't already installed
if [ -d "/srv/www/$subdomain.$vhost" ]; then
echo "$subdomain.$vhost is already installed as it's own vhost"
exit 1
fi
if [ ! -d "/srv/www/$vhost/$subdomain" ]; then
echo "subdomain directory (/srv/www/$vhost/$subdomain) does not exist"
exit 1
fi
macro_vhost_line="$macro_vhost_line $subdomain"
vhost_conf="$subdomain.$vhost_conf"
else
echo "subdomain not set"
exit 1
fi
fi
# check for and set Alias
if [[ "$macro_name" == *"Alias"* ]]; then
if [ -n "$3" ]; then
# make sure Alias domain isn't already installed as it's own vhost
if [ -d "/srv/www/$vhost" ]; then
echo "$alias is already installed as it's own vhost"
exit 1
else
alias=$3
macro_vhost_line="$macro_vhost_line $alias"
fi
else
echo "alias not set"
exit 1
fi
fi
# check for varnish config
if [[ "$macro_name" == *"Varnish"* ]]; then
varnish_host=$vhost
if [[ "$macro_name" == *"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_name" == *"Redirect"* ]]; then
if [ -n "$3" ]; then
redirect=$3
# make sure Redirect domain isn't already installed as it's own vhost
if [ -d "/srv/www/$vhost" ]; then
echo "$vhost 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_name" == *"HTTPS"* ]]; then
cert_host=$vhost
if [[ "$macro_name" == *"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/$vhost_conf
# enable apache conf
if [[ ! -h /etc/apache2/sites-enabled/$vhost_conf ]]; then
a2ensite --quiet $vhost_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