vhost-stack/bin/vhost-del.sh

86 lines
2.2 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 "Removes virtualhost from server."
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 " -d <domain> Domain name of VirtualHost to remove."
2021-09-16 16:21:35 -07:00
echo " -h Print this help."
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
# check for virtualhost dir
if [[ ! -d /srv/www/$domain ]]; then
2021-04-04 13:28:22 -07:00
echo "virtualhost dir does not exist"
exit 1
fi
2021-10-05 11:33:24 -07:00
username=$(stat -c '%U' /srv/www/$domain)
2021-04-04 13:28:22 -07:00
# disable the apache conf and reload apache
2021-10-05 11:33:24 -07:00
if [[ -h /etc/apache2/sites-enabled/$domain.conf ]]; then
a2dissite --quiet $domain
2021-04-04 13:28:22 -07:00
systemctl --quiet is-active apache2 && systemctl --quiet reload apache2
fi
# remove the apache config
2021-10-05 11:33:24 -07:00
if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then
rm /etc/apache2/sites-available/$domain.conf
2021-04-04 13:28:22 -07:00
fi
# remove varnish config
2021-10-05 11:33:24 -07:00
if [[ -f /etc/varnish/sites.d/$domain.vcl ]]; then
rm /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
# don't bother to restart varnish as it will clear cache unnecessarily
fi
# if virtualhost is mounted in a jail, unmount it
if grep -q " /usr/jails/$username/srv/www/$domain " /etc/mtab; then
2021-10-05 11:33:24 -07:00
umount /usr/jails/$username/srv/www/$domain
2021-04-04 13:28:22 -07:00
fi
2021-08-18 16:13:02 -07:00
# if virtualhost mount in fstab.jails exists remove it
if grep -q " /usr/jails/$username/srv/www/$domain " /etc/fstab.jails; then
sed -i "\| /usr/jails/$username/srv/www/$domain |d" /etc/fstab.jails
2021-04-04 13:28:22 -07:00
fi
# if virtualhost symlink exists in jail remove it
2021-10-05 11:33:24 -07:00
if [[ -h /usr/jails/$username/home/$username/$domain ]]; then
unlink /usr/jails/$username/home/$username/$domain
2021-04-04 13:28:22 -07:00
fi
# if virtualhost symlink exists in home dir remove it
2021-10-05 11:33:24 -07:00
if [[ -h /home/$username/$domain ]]; then
unlink /home/$username/$domain
2021-04-04 13:28:22 -07:00
fi
# if virtualhost dir exists in jail remove it
2021-10-05 11:33:24 -07:00
if [[ -d /usr/jails/$username/srv/www/$domain ]]; then
rm -r /usr/jails/$username/srv/www/$domain
2021-04-04 13:28:22 -07:00
fi
# remove virtualhost dir
2021-10-05 11:33:24 -07:00
if [[ -d /srv/www/$domain ]]; then
rm -r /srv/www/$domain
2021-04-04 13:28:22 -07:00
fi