93 lines
2.9 KiB
Bash
93 lines
2.9 KiB
Bash
#!/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
|
|
|
|
# check for and set virtualhost
|
|
if [ -n "$1" ]; then
|
|
virtualhost=$1
|
|
else
|
|
echo "virtualhost not set"
|
|
exit 1
|
|
fi
|
|
|
|
# make sure virtualhost is enabled via symlink
|
|
if [ ! -h "/etc/apache2/sites-enabled/$virtualhost.conf" ]; then
|
|
echo "virtualhost is not enabled"
|
|
exit 1
|
|
fi
|
|
|
|
# make sure virtualhost config is in standard location
|
|
if [ ! -f "/etc/apache2/sites-available/$virtualhost.conf" ]; then
|
|
echo "virtualhost config for $virtualhost not in /etc/apache2/sites-available/"
|
|
exit 1
|
|
fi
|
|
|
|
# grab macro line from virtualhost config
|
|
if macro_vhost_line=`grep -m 1 "Use .*" /etc/apache2/sites-available/$virtualhost.conf` ; then
|
|
macro_name=`echo "$macro_vhost_line" | awk '{print $2}'`
|
|
else
|
|
echo "$virtualhost is not configured with mod_macro"
|
|
exit 1
|
|
fi
|
|
|
|
# make sure Varnish is not already enabled
|
|
if [[ $macro_name =~ ^.*Varnish$ ]]; then
|
|
echo "Varnish already enabled for $virtualhost"
|
|
exit 1
|
|
fi
|
|
|
|
# check for valid HTTPS VHost macro
|
|
if [[ $macro_name =~ ^VHost[[:alpha:]]*HTTPS$ ]]; then
|
|
macro_name_new="${macro_name}Varnish"
|
|
vhost_enable="$macro_name_new $virtualhost"
|
|
else
|
|
echo "$virtualhost must be enabled with an HTTPS VHost macro"
|
|
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\./ /")
|
|
vhost_enable="$vhost_enable $vhost_subdomain"
|
|
fi
|
|
|
|
# check if VHost macro is for an Alias
|
|
if [[ "$macro_name" == *"Alias"* ]]; then
|
|
vhost_alias=`echo "$macro_vhost_line" | awk '{print $5}'`
|
|
vhost_enable="$vhost_enable $vhost_alias"
|
|
fi
|
|
|
|
# check for ssl cert
|
|
if [ ! -f "/etc/ssl/letsencrypt/$virtualhost.pem" ]; then
|
|
echo "$virtualhost.pem cert file does not exist"
|
|
exit 1
|
|
fi
|
|
|
|
# make sure varnish is installed
|
|
if [ ! -f /etc/varnish/sites.d/example.com.vcl ]; then
|
|
echo "Varnish not installed & configured on this server"
|
|
exit 1
|
|
fi
|
|
|
|
# check for / create varnish config
|
|
if [ ! -f "/etc/varnish/sites.d/$virtualhost.vcl" ]; then
|
|
# create varnish config
|
|
echo "sub vcl_recv {" > /etc/varnish/sites.d/$virtualhost.vcl
|
|
echo " if (req.http.host == \"$virtualhost\" || req.http.host == \"www.$virtualhost\") {" >> /etc/varnish/sites.d/$virtualhost.vcl
|
|
echo " # Uncomment next line to bypass varnish cache" >> /etc/varnish/sites.d/$virtualhost.vcl
|
|
echo " #return (pass);" >> /etc/varnish/sites.d/$virtualhost.vcl
|
|
echo " call wordpress;" >> /etc/varnish/sites.d/$virtualhost.vcl
|
|
echo " }" >> /etc/varnish/sites.d/$virtualhost.vcl
|
|
echo "}" >> /etc/varnish/sites.d/$virtualhost.vcl
|
|
/usr/local/bin/varnish-update-sites.sh
|
|
systemctl is-active --quiet varnish && systemctl reload --quiet varnish
|
|
fi
|
|
|
|
/usr/local/bin/vhost-enable.sh $vhost_enable
|