#!/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 macro_name vhost [subdomain|alias]" echo "" echo " -h Print this help." 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 VHostAliasHTTPS example.com existingsite" 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 $vhost $username' echo ' Use VHostHTTPS $vhost $username' echo ' Use VHostHTTPSVarnish $vhost $username' echo ' Use VHostSubdomainHTTP $vhost $username $subdomain' echo ' Use VHostSubdomainHTTPS $vhost $username $subdomain' echo ' Use VHostSubdomainHTTPSVarnish $vhost $username $subdomain' echo ' Use VHostAliasHTTP $vhost $username $alias' echo ' Use VHostAliasHTTPS $vhost $username $alias' echo ' Use VHostAliasHTTPSVarnish $vhost $username $alias' echo ' Use VMailHTTPS $vhost' echo ' Use RedirectHTTP $vhost $redirect' echo ' Use RedirectHTTPS $vhost $redirect' echo '' echo ' $username is autodetected from vhost dir ownership' echo " See /etc/apache2/mods-available/macro.conf for macro details." echo "" exit } 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 macro & vhost if [ -n "$1" ]; then if [ $1 == "-h" ]; then help else # check for and set macro macro_name=$1 shift if [[ " ${macro_array[@]} " =~ " ${macro_name} " ]]; then macro_vhost_line="$macro_vhost_line $macro_name" else echo "invalid macro name" exit 1 fi # check for and set vhost if [ -n "$2" ]; then vhost=$2 shift macro_vhost_line="$macro_vhost_line $vhost" vhost_conf="$vhost.conf" else echo "vhost not set" echo help fi fi else help 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