vhost-stack/bin/vhost-enable.sh

200 lines
7.0 KiB
Bash
Executable File

#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# 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)
# 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>] [-f <fpm>] [-o <alias>|<redirect_url>] [-h]"
echo ""
echo " -h Print this help."
echo " -d <domain> Domain name of VirtualHost to add."
echo " -m <macro> Name of Apache macro to apply. Optional, script will attempt to autoselect if possible."
echo " -f <fpm> PHP-FPM version to enable. Optional, defaults to default PHP version, only used by VHost macros."
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."
echo ""
echo " Available Apache Macros with examples:"
echo ""
echo " vhost-enable.sh -m VHostHTTP -d example.com"
echo " vhost-enable.sh -m VHostHTTPS -d example.com -f 8.3"
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 Mailman3HTTPS -d lists.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"
echo ""
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))
vhost:getoptions "$@"
# check for domain (virtualhost)
if [[ ! -n $domain ]]; then
echo "domain is required"
exit
fi
# autodetect macro to use if not set
if [[ ! -n $macro ]]; then
if [[ "$domain" =~ ^mail.* ]]; then
macro=VMailHTTPS
elif [[ "$domain" =~ ^lists.* ]]; then
macro=Mailman3HTTPS
elif [[ -f "/etc/ssl/letsencrypt/$domain.pem" ]]; then
macro=VHostHTTPS
else
macro=VHostHTTP
fi
fi
# verify macro name
if [[ " ${macro_array[@]} " =~ " ${macro} " ]]; then
macro_vhost_line="Use $macro"
else
echo "invalid macro name"
exit 1
fi
# 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
# set username & php-fpm for all VHost macros
if [[ "$macro" == *"VHost"* ]]; then
# check for vhost dir
if [[ "$macro" == *"Alias"* ]]; then
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
else
vhost=$domain
fi
if [[ -d "/srv/www/$vhost" ]]; then
# get and set $username
username=$(stat -c '%U' /srv/www/$vhost)
# use default phpVersion for fpm if not otherwise specified
vhost::set-phpVersion
if [[ -z $fpm ]]; then
fpm=$phpVersion
else
# not using default php-fpm version, make sure config exists for specified version
if [[ ! -f /etc/php/$fpm/fpm/pool.d/$username.conf ]]; then
if [[ -f /etc/php/$phpVersion/fpm/pool.d/$username.conf ]]; then
cat /etc/php/$phpVersion/fpm/pool.d/$username.conf |sed "s|php$phpVersion|php$fpm|g" > /etc/php/$fpm/fpm/pool.d/$username.conf
fi
fi
fi
macro_vhost_line="$macro_vhost_line $vhost $username $fpm"
# if vhost is jailed ensure php-fpm is chrooted. should have already been set when user was initially jailed
if [[ -d /usr/jails/$username/srv/www/$domain ]]; then
if [[ -f /etc/php/$fpm/fpm/pool.d/$username.conf ]]; then
if ! /bin/grep -q "^chroot" /etc/php/$fpm/fpm/pool.d/$username.conf; then
echo "chroot = /usr/jails/$username" >> /etc/php/$fpm/fpm/pool.d/$username.conf
fi
fi
fi
# make sure sendmail_path is set for php-fpm
if [[ -f /etc/php/$fpm/fpm/pool.d/$username.conf ]]; then
if ! /bin/grep -q "^php_admin_value\[sendmail_path\]" /etc/php/$fpm/fpm/pool.d/$username.conf; then
echo "php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -fwebmaster@$domain" >> /etc/php/$fpm/fpm/pool.d/$username.conf
fi
fi
else
echo "VirtualHost dir for $vhost does not exist."
exit 1
fi
# 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."
exit 1
fi
fi
# check for Alias option
if [[ "$macro" == *"Alias"* ]]; then
macro_vhost_line="$macro_vhost_line $domain"
fi
# 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
fi
fi
# check for 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
# create / update apache conf
echo "$macro_vhost_line" > /etc/apache2/sites-available/$domain.conf
# enable apache conf
if [[ -h /etc/apache2/sites-enabled/$domain.conf ]]; then
# modify timestamp on existing symlink to trigger apache restart
touch --no-dereference /etc/apache2/sites-enabled/$domain.conf
else
a2ensite --quiet $domain.conf
fi