vhost-stack/bin/vhost-enable.sh

200 lines
7.0 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
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 ""
2024-09-18 12:35:55 -07:00
echo "usage: $thisfilename -d <domain> [-m <macro>] [-f <fpm>] [-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."
echo " -m <macro> Name of Apache macro to apply. Optional, script will attempt to autoselect if possible."
2024-09-18 12:35:55 -07:00
echo " -f <fpm> PHP-FPM version to enable. Optional, defaults to default PHP version, only used by VHost macros."
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"
2024-09-18 12:35:55 -07:00
echo " vhost-enable.sh -m VHostHTTPS -d example.com -f 8.3"
2022-10-21 14:41:20 -07:00
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"
2022-10-21 14:41:20 -07:00
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 domain (virtualhost)
if [[ ! -n $domain ]]; then
echo "domain is required"
exit
fi
2023-05-17 15:48:00 -07:00
# 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
2021-04-04 13:28:22 -07:00
fi
2023-05-17 15:48:00 -07:00
# verify macro name
if [[ " ${macro_array[@]} " =~ " ${macro} " ]]; then
macro_vhost_line="Use $macro"
else
echo "invalid macro name"
exit 1
fi
2021-04-04 13:28:22 -07:00
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
2024-09-18 12:35:55 -07:00
# set username & php-fpm 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)
2024-09-18 12:35:55 -07:00
# 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
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
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
2021-10-05 13:10:01 -07:00
a2ensite --quiet $domain.conf
2021-04-04 13:28:22 -07:00
fi