Skip to content

Instantly share code, notes, and snippets.

@blues911
Last active December 21, 2019 16:26
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 blues911/f5789908ce288f425f7f86282ef16972 to your computer and use it in GitHub Desktop.
Save blues911/f5789908ce288f425f7f86282ef16972 to your computer and use it in GitHub Desktop.
NGINX sites manager
#!/bin/bash
#
# nginx-modsite
# (c) valera.padolochniy@gmail.com
# This script is released under the MIT License (MIT)
#
# DESCRIPTION
# NGINX sites manager.
#
# INSTALL & USAGE (for Linux)
# 1) Create symlink:
# sudo ln -s ~/dev/scripts/nginx_modsite /usr/local/bin/nginx_modsite
# 2) Add bash alias:
# echo "alias nginx-modsite='sudo /usr/local/bin/nginx_modsite/main.sh'" >> ~/.bash_aliases
# 3) Run for help:
# nginx-modsite -h
# settings
BASE_DIR="$(dirname "$0")"
SITES_AVAILABLE='/etc/nginx/sites-available/'
SITES_ENABLED='/etc/nginx/sites-enabled/'
SITE_NAME=$2
# functions block
fn_help() {
# displays help information
echo "usage: nginx_modsite [-h|--help] [-l|--list] [-e SITE|--enable SITE] [-d SITE|--disable SITE]"
echo "arguments:"
echo " -h | --help show this help message and exit"
echo " -l | --list show sites list"
echo " -e | --enable SITE enable site"
echo " -d | --disbale SITE disable site"
}
fn_list() {
# prepares and displays sites list with enabled/disabled marks
sa=("${SITES_AVAILABLE}*")
se=("${SITES_ENABLED}*")
saList=()
seList=()
for site in $sa; do
saList+=("$(echo $site | cut -d'/' -f5)")
done
for site in $se; do
seList+=("$(echo $site | cut -d'/' -f5)")
done
for site in "${saList[@]}"; do
if [[ "${seList[@]}" == *"$site"* ]]; then
echo -e "\033[32m[+]\033[0m $site"
else
echo -e "\033[31m[-]\033[0m $site"
fi
done
}
fn_enable() {
# creates site symlink
if [ -z $SITE_NAME ]; then
echo "Error: site name is missing"
exit 1
fi
sa="${SITES_AVAILABLE}${SITE_NAME}"
se="${SITES_ENABLED}${SITE_NAME}"
if [ ! -f $sa ]; then
echo "Error: site not found"
exit 1
elif [ -L $se ]; then
echo "Error: site enabled"
exit 1
else
ln -s $sa $se
service nginx restart
echo "Success: site enabled"
fi
}
fn_disable() {
# removes site symlink
if [ -z $SITE_NAME ]; then
echo "Error: site name is missing"
exit 1
fi
sa="${SITES_AVAILABLE}${SITE_NAME}"
se="${SITES_ENABLED}${SITE_NAME}"
if [ ! -f $sa ]; then
echo "Error: site not found"
exit 1
elif [ ! -L $se ]; then
echo "Error: site disabled"
exit 1
else
rm $se
service nginx restart
echo "Success: site disabled"
fi
}
# handle input args & execute functions
case "$1" in
-h|--help)
fn_help
;;
-l|--list)
fn_list
;;
-e|--enable)
fn_enable
;;
-d|--disable)
fn_disable
;;
*)
echo "use [-h|--help] for help"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment