vhost-stack/bin/vhost-enable.sh

201 lines
6.3 KiB
Bash
Raw Normal View History

2021-04-04 13:28:22 -07:00
#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
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 ""
2021-10-05 11:33:24 -07:00
echo "usage: $thisfilename -d <domain> -m <macro> [-o <subdomain>|<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."
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."
2021-10-05 13:22:39 -07:00
echo " For Alias domains '-d <domain>' is the alias domain, and '-o <alias>'"
echo " is the existing VirtualHost to alias to."
2021-09-16 16:21:35 -07:00
echo ""
2021-10-05 11:33:24 -07:00
echo " Available Apache Macros:"
2021-09-16 16:21:35 -07:00
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 ""
2021-10-05 11:33:24 -07:00
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"
2021-09-16 16:21:35 -07:00
echo ""
2021-10-05 11:33:24 -07:00
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'
2021-09-16 16:21:35 -07:00
echo ''
2021-10-05 11:33:24 -07:00
echo ' $username is autodetected from vhost dir ownership'
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))
macro_vhost_line="Use"
2021-10-05 11:33:24 -07:00
vhost:getoptions "$@"
# check for macro
if [[ -n $macro ]]; then
if [[ " ${macro_array[@]} " =~ " ${macro} " ]]; then
macro_vhost_line="$macro_vhost_line $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)
if [[ -n $domain ]]; then
macro_vhost_line="$macro_vhost_line $domain"
vhost_conf="$domain.conf"
else
echo "domain is required"
exit
2021-04-04 13:28:22 -07:00
fi
# 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
vhost=$option
else
vhost=$domain
fo
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)
2021-04-04 13:28:22 -07:00
macro_vhost_line="$macro_vhost_line $username"
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
# check for and set Subdomain
2021-10-05 11:33:24 -07:00
if [[ "$macro" == *"Subdomain"* ]]; then
if [[ -n $option ]]; then
subdomain=$option
2021-04-04 13:28:22 -07:00
# make sure Subdomain isn't already installed
2021-10-05 11:33:24 -07:00
if [[ -d "/srv/www/$subdomain.$domain" ]]; then
echo "$subdomain.$domain is already installed as it's own VirtualHost."
2021-04-04 13:28:22 -07:00
exit 1
fi
2021-10-05 11:33:24 -07:00
if [[ ! -d "/srv/www/$domain/$subdomain" ]]; then
echo "Subdomain directory (/srv/www/$domain/$subdomain) does not exist"
2021-04-04 13:28:22 -07:00
exit 1
fi
macro_vhost_line="$macro_vhost_line $subdomain"
2021-10-05 13:10:01 -07:00
vhost_conf="$subdomain.$domain.conf"
2021-04-04 13:28:22 -07:00
else
2021-10-05 11:33:24 -07:00
echo "subdomain (-o OPTION) not set"
2021-04-04 13:28:22 -07:00
exit 1
fi
fi
# check for and set Alias
2021-10-05 11:33:24 -07:00
if [[ "$macro" == *"Alias"* ]]; then
if [ -n $option ]; then
alias=$option
2021-04-04 13:28:22 -07:00
# make sure Alias domain isn't already installed as it's own vhost
2021-10-05 13:22:39 -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 $alias"
fi
else
echo "alias not set"
exit 1
fi
fi
# check for varnish config
2021-10-05 11:33:24 -07:00
if [[ "$macro" == *"Varnish"* ]]; then
varnish_host=$domain
if [[ "$macro" == *"Subdomain"* ]]; then
2021-04-04 13:28:22 -07:00
varnish_host="$subdomain.$varnish_host"
fi
2021-10-05 11:33:24 -07:00
if [[ ! -f "/etc/varnish/sites.d/$varnish_host.vcl" ]]; then
2021-04-04 13:28:22 -07:00
echo "$varnish_config_file Varnish config file does not exist"
exit 1
fi
fi
fi
# check for and set 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
# if https check for le cert
2021-10-05 11:33:24 -07:00
if [[ "$macro" == *"HTTPS"* ]]; then
2021-10-05 13:22:39 -07:00
cert_host=$domain
if [[ "$macro" == *"Subdomain"* ]]; then
cert_host="$subdomain.$cert_host"
2021-04-04 13:28:22 -07:00
fi
2021-10-05 11:33:24 -07:00
if [[ ! -f "/etc/ssl/letsencrypt/$cert_host.pem" ]]; then
2021-04-04 13:28:22 -07:00
echo "cert file for $cert_host does not exist"
exit 1
fi
fi
# create / edit 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