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 "Enables Varnish for specified virtualhost."
|
|
|
|
echo ""
|
2021-10-05 11:33:24 -07:00
|
|
|
echo "usage: $thisfilename -d <domain> [-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 (VirtualHost) to enable Varnish for."
|
2021-09-16 16:21:35 -07:00
|
|
|
echo ""
|
2021-10-05 11:33:24 -07:00
|
|
|
echo " Creates Varnish config, loads it in Varnish and then"
|
|
|
|
echo " enables Apache proxy to Varnish for virtualhost."
|
2021-09-16 16:21:35 -07:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2021-10-05 11:33:24 -07:00
|
|
|
vhost:getoptions "$@"
|
|
|
|
|
|
|
|
# check for domain (virtualhost)
|
|
|
|
if [[ -z $domain ]]; then
|
|
|
|
echo "domain is required"
|
|
|
|
exit
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
|
2021-10-05 11:33:24 -07:00
|
|
|
|
2021-04-04 13:28:22 -07:00
|
|
|
# make sure virtualhost is enabled via symlink
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ ! -h "/etc/apache2/sites-enabled/$domain.conf" ]]; then
|
2021-04-04 13:28:22 -07:00
|
|
|
echo "virtualhost is not enabled"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# make sure virtualhost config is in standard location
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ ! -f "/etc/apache2/sites-available/$domain.conf" ]]; then
|
|
|
|
echo "virtualhost config for $domain not in /etc/apache2/sites-available/"
|
2021-04-04 13:28:22 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# grab macro line from virtualhost config
|
2021-10-05 11:33:24 -07:00
|
|
|
if macro_vhost_line=`grep -m 1 "Use .*" /etc/apache2/sites-available/$domain.conf` ; then
|
2021-04-04 13:28:22 -07:00
|
|
|
macro_name=`echo "$macro_vhost_line" | awk '{print $2}'`
|
|
|
|
else
|
2021-10-05 11:33:24 -07:00
|
|
|
echo "$domain is not configured with mod_macro"
|
2021-04-04 13:28:22 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# make sure Varnish is not already enabled
|
|
|
|
if [[ $macro_name =~ ^.*Varnish$ ]]; then
|
2021-10-05 11:33:24 -07:00
|
|
|
echo "Varnish already enabled for $domain"
|
2021-04-04 13:28:22 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check for valid HTTPS VHost macro
|
|
|
|
if [[ $macro_name =~ ^VHost[[:alpha:]]*HTTPS$ ]]; then
|
|
|
|
macro_name_new="${macro_name}Varnish"
|
2021-10-05 14:03:01 -07:00
|
|
|
vhost_enable="-d $domain -m $macro_name_new"
|
2021-04-04 13:28:22 -07:00
|
|
|
else
|
2021-10-05 11:33:24 -07:00
|
|
|
echo "$domain must be enabled with an HTTPS VHost macro"
|
2021-04-04 13:28:22 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check if VHost macro is for a Subdomain
|
|
|
|
if [[ "$macro_name" == *"Subdomain"* ]]; then
|
|
|
|
vhost_subdomain=`echo "$macro_vhost_line" | awk '{print $5}'`
|
|
|
|
vhost_enable=$(echo "$vhost_enable" | sed -e "s/ $vhost_subdomain\./ /")
|
2021-10-05 14:03:01 -07:00
|
|
|
vhost_enable="$vhost_enable -o $vhost_subdomain"
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
# check if VHost macro is for an Alias
|
|
|
|
if [[ "$macro_name" == *"Alias"* ]]; then
|
|
|
|
vhost_alias=`echo "$macro_vhost_line" | awk '{print $5}'`
|
2021-10-05 14:03:01 -07:00
|
|
|
vhost_enable="$vhost_enable -o $vhost_alias"
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
# check for ssl cert
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ ! -f "/etc/ssl/letsencrypt/$domain.pem" ]]; then
|
|
|
|
echo "$domain.pem cert file does not exist"
|
2021-04-04 13:28:22 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# make sure varnish is installed
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ ! -f /etc/varnish/sites.d/example.com.vcl ]]; then
|
2021-04-04 13:28:22 -07:00
|
|
|
echo "Varnish not installed & configured on this server"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check for / create varnish config
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ ! -f "/etc/varnish/sites.d/$domain.vcl" ]]; then
|
2021-04-04 13:28:22 -07:00
|
|
|
# create varnish config
|
2021-10-05 11:33:24 -07:00
|
|
|
echo "sub vcl_recv {" > /etc/varnish/sites.d/$domain.vcl
|
|
|
|
echo " if (req.http.host == \"$domain\" || req.http.host == \"www.$domain\") {" >> /etc/varnish/sites.d/$domain.vcl
|
|
|
|
echo " # Uncomment next line to bypass varnish cache" >> /etc/varnish/sites.d/$domain.vcl
|
|
|
|
echo " #return (pass);" >> /etc/varnish/sites.d/$domain.vcl
|
|
|
|
echo " call wordpress;" >> /etc/varnish/sites.d/$domain.vcl
|
|
|
|
echo " }" >> /etc/varnish/sites.d/$domain.vcl
|
|
|
|
echo "}" >> /etc/varnish/sites.d/$domain.vcl
|
2021-08-24 15:03:50 -07:00
|
|
|
/usr/local/bin/vhost-varnish-update-sites.sh
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
/usr/local/bin/vhost-enable.sh $vhost_enable
|