Skip to content

Instantly share code, notes, and snippets.

@rameerez
Last active December 26, 2023 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rameerez/123d2f97eac6352cc3c6c364cf576560 to your computer and use it in GitHub Desktop.
Save rameerez/123d2f97eac6352cc3c6c364cf576560 to your computer and use it in GitHub Desktop.
Remove site from Apache conf
#!/bin/bash
# Ensure the script is run as root
if [ "$(id -u)" -ne 0 ]; then
echo -e "${RED}ERROR: This script must be run as root.${NC}"
exit 1
fi
# Define aesthetics
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
ALIEN='\xF0\x9F\x91\xBD'
# ARGUMENTS
# 1: website domain without alias / subdomains (ex: example.com)
if [ -z "$1" ]; then
echo -e "${RED}${ALIEN} ERROR: No website domain provided as an argument.${NC}"
exit 1
fi
DOMAIN=$1
echo -e "${RED}${ALIEN} Warning: This action will permanently remove the website for $DOMAIN.${NC}"
read -p "Are you sure you want to continue? [Y/n] " confirmation
if [ "$confirmation" != "Y" ]; then
echo -e "${GREEN}${ALIEN} Website removal aborted.${NC}"
exit 0
fi
# Disable the site in Apache
echo -e "${GREEN}${ALIEN} Disabling VirtualHost for $DOMAIN...${NC}"
a2dissite $DOMAIN.conf
a2dissite $DOMAIN-le-ssl.conf
# Reload Apache to apply changes
echo -e "${GREEN}${ALIEN} Reloading Apache...${NC}"
systemctl reload apache2
# Remove the VirtualHost configuration file
echo -e "${GREEN}${ALIEN} Removing Apache configuration file for $DOMAIN...${NC}"
rm -f /etc/apache2/sites-available/$DOMAIN.conf
rm -f /etc/apache2/sites-available/$DOMAIN-le-ssl.conf
rm -f /etc/apache2/sites-enabled/$DOMAIN.conf
rm -f /etc/apache2/sites-enabled/$DOMAIN-le-ssl.conf
# Remove the website directory
echo -e "${GREEN}${ALIEN} Removing web directory for $DOMAIN...${NC}"
rm -rf /var/www/$DOMAIN
# Optional: Remove SSL Certificates
echo -e "${GREEN}${ALIEN} Removing SSL certificates for $DOMAIN...${NC}"
certbot delete --cert-name $DOMAIN
echo -e "${GREEN}${ALIEN} Website for $DOMAIN has been removed successfully.${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment