Skip to content

Instantly share code, notes, and snippets.

@Taidgh
Last active March 31, 2018 21:54
Show Gist options
  • Save Taidgh/c179fb6d7cde553b300b59aa8120beb2 to your computer and use it in GitHub Desktop.
Save Taidgh/c179fb6d7cde553b300b59aa8120beb2 to your computer and use it in GitHub Desktop.
Delete Nginx serverblock, folders and files
#!/usr/bin/env bash
#
# Nginx - delete block
# Based on this post: http://clubmate.fi/how-to-make-an-nginx-server-block-manually-or-with-a-shell-script/
# Functions
ok() { echo -e '\e[32m'$1'\e[m'; } # Green
die() { echo -e '\e[1;31m'$1'\e[m'; exit 1; }
read -p "Type the domain you want to remove (domain.com NOT http://domain.com/) then press enter: " delete
if [ "$delete" != "" ]; then
# Variables
NGINX_AVAILABLE_VHOSTS='/etc/nginx/sites-available'
NGINX_ENABLED_VHOSTS='/etc/nginx/sites-enabled'
WEB_DIR='/var/www'
CURRENT_TIME=$(date "+%Y%m%d-%H%M%S")
ARCHIVE_NAME=$CURRENT_TIME'.zip'
# Sanity check
[ $(id -g) != "0" ] && die "Script must be run as root."
# Delete serverblock directories
rm -rf $WEB_DIR/$delete
# Remove site and symbolic link
rm -f $NGINX_ENABLED_VHOSTS/$delete
rm -f $NGINX_AVAILABLE_VHOSTS/$delete
fi
# Restart
read -p "Do you wish to restart nginx? <y/N> " restart
if [ "$restart" = "y" ]; then
service nginx restart
else
echo "Your website may not work until you restart Nginx."
echo "Run service 'sudo nginx restart' to restart Nginx"
ok "Site Removed for $1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment