masdhan

Enable-Disable Site Nginx

Feb 3rd, 2019
153
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. ##
  4. #  File:
  5. #    nginx_modsite
  6. #  Description:
  7. #    Provides a basic script to automate enabling and disabling websites found
  8. #    in the default configuration directories:
  9. #      /etc/nginx/sites-available and /etc/nginx/sites-enabled
  10. #    For easy access to this script, copy it into the directory:
  11. #      /usr/local/sbin
  12. #    Run this script without any arguments or with -h or --help to see a basic
  13. #    help dialog displaying all options.
  14. ##
  15.  
  16. # Copyright (C) 2010 Michael Lustfield <mtecknology@ubuntu.com>
  17.  
  18. # Redistribution and use in source and binary forms, with or without
  19. # modification, are permitted provided that the following conditions
  20. # are met:
  21. # 1. Redistributions of source code must retain the above copyright
  22. #    notice, this list of conditions and the following disclaimer.
  23. # 2. Redistributions in binary form must reproduce the above copyright
  24. #    notice, this list of conditions and the following disclaimer in the
  25. #    documentation and/or other materials provided with the distribution.
  26. #
  27. # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  28. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. # ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
  31. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37. # SUCH DAMAGE.
  38.  
  39. ##
  40. # Default Settings
  41. ##
  42.  
  43. NGINX_CONF_FILE="$(awk -F= -v RS=' ' '/conf-path/ {print $2}' <<< $(nginx -V 2>&1))"
  44. NGINX_CONF_DIR="${NGINX_CONF_FILE%/*}"
  45. NGINX_SITES_AVAILABLE="$NGINX_CONF_DIR/sites-available"
  46. NGINX_SITES_ENABLED="$NGINX_CONF_DIR/sites-enabled"
  47. SELECTED_SITE="$2"
  48.  
  49. ##
  50. # Script Functions
  51. ##
  52.  
  53. ngx_enable_site() {
  54.     [[ ! "$SELECTED_SITE" ]] &&
  55.         ngx_select_site "not_enabled"
  56.  
  57.     [[ ! -e "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" ]] &&
  58.         ngx_error "Site does not appear to exist."
  59.     [[ -e "$NGINX_SITES_ENABLED/$SELECTED_SITE" ]] &&
  60.         ngx_error "Site appears to already be enabled"
  61.  
  62.     ln -sf "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" -T "$NGINX_SITES_ENABLED/$SELECTED_SITE"
  63.     ngx_reload
  64. }
  65.  
  66. ngx_disable_site() {
  67.     [[ ! "$SELECTED_SITE" ]] &&
  68.         ngx_select_site "is_enabled"
  69.  
  70.     [[ ! -e "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" ]] &&
  71.         ngx_error "Site does not appear to be \'available\'. - Not Removing"
  72.     [[ ! -e "$NGINX_SITES_ENABLED/$SELECTED_SITE" ]] &&
  73.         ngx_error "Site does not appear to be enabled."
  74.  
  75.     rm -f "$NGINX_SITES_ENABLED/$SELECTED_SITE"
  76.     ngx_reload
  77. }
  78.  
  79. ngx_list_site() {
  80.     echo "Available sites:"
  81.     ngx_sites "available"
  82.     echo "Enabled Sites"
  83.     ngx_sites "enabled"
  84. }
  85.  
  86. ##
  87. # Helper Functions
  88. ##
  89.  
  90. ngx_select_site() {
  91.     sites_avail=($NGINX_SITES_AVAILABLE/*)
  92.     sa="${sites_avail[@]##*/}"
  93.     sites_en=($NGINX_SITES_ENABLED/*)
  94.     se="${sites_en[@]##*/}"
  95.  
  96.     case "$1" in
  97.         not_enabled) sites=$(comm -13 <(printf "%s\n" $se) <(printf "%s\n" $sa));;
  98.         is_enabled) sites=$(comm -12 <(printf "%s\n" $se) <(printf "%s\n" $sa));;
  99.     esac
  100.  
  101.     ngx_prompt "$sites"
  102. }
  103.  
  104. ngx_prompt() {
  105.     sites=($1)
  106.     i=0
  107.  
  108.     echo "SELECT A WEBSITE:"
  109.     for site in ${sites[@]}; do
  110.         echo -e "$i:\t${sites[$i]}"
  111.         ((i++))
  112.     done
  113.  
  114.     read -p "Enter number for website: " i
  115.     SELECTED_SITE="${sites[$i]}"
  116. }
  117.  
  118. ngx_sites() {
  119.     case "$1" in
  120.         available) dir="$NGINX_SITES_AVAILABLE";;
  121.         enabled) dir="$NGINX_SITES_ENABLED";;
  122.     esac
  123.  
  124.     for file in $dir/*; do
  125.         echo -e "\t${file#*$dir/}"
  126.     done
  127. }
  128.  
  129. ngx_reload() {
  130.     read -p "Would you like to reload the Nginx configuration now? (Y/n) " reload
  131.     [[ "$reload" != "n" && "$reload" != "N" ]] && invoke-rc.d nginx reload
  132. }
  133.  
  134. ngx_error() {
  135.     echo -e "${0##*/}: ERROR: $1"
  136.     [[ "$2" ]] && ngx_help
  137.     exit 1
  138. }
  139.  
  140. ngx_help() {
  141.     echo "Usage: ${0##*/} [options]"
  142.     echo "Options:"
  143.     echo -e "\t<-e|--enable> <site>\tEnable site"
  144.     echo -e "\t<-d|--disable> <site>\tDisable site"
  145.     echo -e "\t<-l|--list>\t\tList sites"
  146.     echo -e "\t<-h|--help>\t\tDisplay help"
  147.     echo -e "\n\tIf <site> is left out a selection of options will be presented."
  148.     echo -e "\tIt is assumed you are using the default sites-enabled and"
  149.     echo -e "\tsites-disabled located at $NGINX_CONF_DIR."
  150. }
  151.  
  152. ##
  153. # Core Piece
  154. ##
  155.  
  156. case "$1" in
  157.     -e|--enable)    ngx_enable_site;;
  158.     -d|--disable)   ngx_disable_site;;
  159.     -l|--list)  ngx_list_site;;
  160.     -h|--help)  ngx_help;;
  161.     *)      ngx_error "No Options Selected" 1; ngx_help;;
  162. esac
RAW Paste Data