first commit
This commit is contained in:
373
nginx-modsite
373
nginx-modsite
@@ -1,179 +1,260 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
##
|
#!/bin/bash
|
||||||
# File:
|
|
||||||
# nginx-modsite
|
|
||||||
# Description:
|
|
||||||
# Provides a basic script to automate enabling and disabling websites found
|
|
||||||
# in the default configuration directories:
|
|
||||||
# /etc/nginx/sites-available and /etc/nginx/sites-enabled
|
|
||||||
# For easy access to this script, copy it into the directory:
|
|
||||||
# /usr/local/sbin
|
|
||||||
# Run this script without any arguments or with -h or --help to see a basic
|
|
||||||
# help dialog displaying all options.
|
|
||||||
##
|
|
||||||
|
|
||||||
# Copyright (C) 2010 Michael Lustfield <mtecknology@ubuntu.com>, 2016 Andrew Salkeld (ajsalkeld) <andrew@ajsalkeld.com>
|
################################################################################
|
||||||
|
# NGINX - Site Manager
|
||||||
# Redistribution and use in source and binary forms, with or without
|
|
||||||
# modification, are permitted provided that the following conditions
|
|
||||||
# are met:
|
|
||||||
# 1. Redistributions of source code must retain the above copyright
|
|
||||||
# notice, this list of conditions and the following disclaimer.
|
|
||||||
# 2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
# notice, this list of conditions and the following disclaimer in the
|
|
||||||
# documentation and/or other materials provided with the distribution.
|
|
||||||
#
|
#
|
||||||
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
# This script manages NGINX sites, providing functionality for enabling/disabling,
|
||||||
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
# listing, and editing site configurations. It simplifies the process of managing
|
||||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
# NGINX sites by providing a command-line interface for common operations.
|
||||||
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
|
#
|
||||||
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
# Usage:
|
||||||
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
# ./nginx-sm.sh [options]
|
||||||
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
#
|
||||||
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
# Options:
|
||||||
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
# -e, --enable Enable a site by enabling its configuration file.
|
||||||
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
# -d, --disable Disable a site by disabling its configuration file.
|
||||||
# SUCH DAMAGE.
|
# -l, --list List all available sites and their status.
|
||||||
|
# -ed, --edit Edit the configuration file of a site.
|
||||||
|
#
|
||||||
|
# Examples:
|
||||||
|
# ./nginx-sm.sh --enable example.com # Enable the site example.com
|
||||||
|
# ./nginx-sm.sh --list # List all available sites
|
||||||
|
# ./nginx-sm.sh --edit example.com # Edit the configuration of example.com
|
||||||
|
#
|
||||||
|
# Dependencies:
|
||||||
|
# - NGINX web server must be installed and running.
|
||||||
|
# - The script requires sudo privileges to modify NGINX configuration files.
|
||||||
|
#
|
||||||
|
# Copyright (c) [2023] [nobody]
|
||||||
|
# Licensed under the MIT License.
|
||||||
|
################################################################################
|
||||||
|
|
||||||
##
|
# MIT License
|
||||||
# Default Settings
|
|
||||||
##
|
|
||||||
|
|
||||||
NGINX_CONF_FILE="$(awk -F= -v RS=' ' '/conf-path/ {print $2}' <<< $(nginx -V 2>&1))"
|
# Copyright (c) [2023] [nobody]
|
||||||
NGINX_CONF_DIR="${NGINX_CONF_FILE%/*}"
|
|
||||||
NGINX_SITES_AVAILABLE="$NGINX_CONF_DIR/sites-available"
|
|
||||||
NGINX_SITES_ENABLED="$NGINX_CONF_DIR/sites-enabled"
|
|
||||||
SELECTED_SITE="$2"
|
|
||||||
|
|
||||||
##
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# Script Functions
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
##
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
ngx_enable_site() {
|
# The above copyright notice and this permission notice shall be included in all
|
||||||
[[ ! "$SELECTED_SITE" ]] &&
|
# copies or substantial portions of the Software.
|
||||||
ngx_select_site "not_enabled"
|
|
||||||
|
|
||||||
[[ ! -e "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" ]] &&
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
ngx_error "Site does not appear to exist."
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
[[ -e "$NGINX_SITES_ENABLED/$SELECTED_SITE" ]] &&
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
ngx_error "Site appears to already be enabled"
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
ln -sf "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" "$NGINX_SITES_ENABLED/$SELECTED_SITE"
|
declare -g site_to_enable=""
|
||||||
ngx_reload
|
declare -g site_to_disable=""
|
||||||
|
declare -g site_to_edit=""
|
||||||
|
declare -g list_sites=false
|
||||||
|
declare -g display_help=false
|
||||||
|
declare -g editor="nano"
|
||||||
|
|
||||||
|
# Fetch NGINX configuration directories
|
||||||
|
get_config_dirs() {
|
||||||
|
if ! command -v nginx >/dev/null 2>&1; then
|
||||||
|
echo "Error: nginx command not found."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
local confFile=$(nginx -V 2>&1 | grep -o 'conf-path=[^ ]*' | cut -d= -f2)
|
||||||
|
local confDir=$(dirname "$confFile")
|
||||||
|
declare -g sitesAvail="$confDir/sites-available"
|
||||||
|
declare -g sitesEnabled="$confDir/sites-enabled"
|
||||||
|
|
||||||
|
if [ ! -d "$sitesAvail" ] || [ ! -d "$sitesEnabled" ]; then
|
||||||
|
echo "Error: Unable to find NGINX sites-available or sites-enabled directory."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
ngx_disable_site() {
|
# Initialize variables
|
||||||
[[ ! "$SELECTED_SITE" ]] &&
|
initialize_variables() {
|
||||||
ngx_select_site "is_enabled"
|
site_to_enable=""
|
||||||
|
site_to_disable=""
|
||||||
[[ ! -e "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" ]] &&
|
site_to_edit=""
|
||||||
ngx_error "Site does not appear to be \'available\'. - Not Removing"
|
list_sites=false
|
||||||
[[ ! -e "$NGINX_SITES_ENABLED/$SELECTED_SITE" ]] &&
|
display_help=false
|
||||||
ngx_error "Site does not appear to be enabled."
|
editor="nano"
|
||||||
|
|
||||||
rm -f "$NGINX_SITES_ENABLED/$SELECTED_SITE"
|
|
||||||
ngx_reload
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ngx_edit_site() {
|
# Check if script was run using sudo for options that require it
|
||||||
[[ ! "$SELECTED_SITE" ]] &&
|
check_sudo() {
|
||||||
ngx_select_site "not_enabled"
|
if [[ $EUID -ne 0 ]]; then
|
||||||
|
echo "Please run this script with sudo or as root."
|
||||||
[[ ! -e "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" ]] &&
|
exit 1
|
||||||
ngx_error "Site does not appear to exist."
|
fi
|
||||||
|
|
||||||
nano "$NGINX_SITES_AVAILABLE/$SELECTED_SITE"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ngx_list_site() {
|
# Display help for script usage
|
||||||
echo "Available sites:"
|
show_help() {
|
||||||
ngx_sites "available"
|
echo -e "\nnginx Site Manager"
|
||||||
echo "Enabled Sites"
|
echo -e "Usage: $(basename "$0") [options] <site>\n"
|
||||||
ngx_sites "enabled"
|
echo -e "Options:"
|
||||||
|
echo -e "\t-e, --enable <site>: Enable site"
|
||||||
|
echo -e "\t-d, --disable <site>: Disable site"
|
||||||
|
echo -e "\t-l, --list: List sites"
|
||||||
|
echo -e "\t-ed, --edit <site>: Edit site"
|
||||||
|
echo -e "\t--editor <editor>: Set editor"
|
||||||
|
echo -e "\t-h, --help: Display help"
|
||||||
|
echo -e "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
##
|
# Enable a specified site by creating a symbolic link in the sites-enabled directory
|
||||||
# Helper Functions
|
enable_site() {
|
||||||
##
|
local site=$1
|
||||||
|
if [ ! -e "$sitesAvail/$site" ]; then
|
||||||
ngx_select_site() {
|
echo "Site does not appear to exist."
|
||||||
sites_avail=($NGINX_SITES_AVAILABLE/*)
|
elif [ -e "$sitesEnabled/$site" ]; then
|
||||||
sa="${sites_avail[@]##*/}"
|
echo "Site appears to already be enabled."
|
||||||
sites_en=($NGINX_SITES_ENABLED/*)
|
else
|
||||||
se="${sites_en[@]##*/}"
|
ln -s "$sitesAvail/$site" "$sitesEnabled/$site"
|
||||||
|
echo "Site enabled."
|
||||||
case "$1" in
|
fi
|
||||||
not_enabled) sites=$(comm -13 <(printf "%s\n" $se) <(printf "%s\n" $sa));;
|
|
||||||
is_enabled) sites=$(comm -12 <(printf "%s\n" $se) <(printf "%s\n" $sa));;
|
|
||||||
esac
|
|
||||||
|
|
||||||
ngx_prompt "$sites"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ngx_prompt() {
|
# Disable a specified site by removing the symbolic link in the sites-enabled directory
|
||||||
sites=($1)
|
disable_site() {
|
||||||
i=0
|
local site=$1
|
||||||
|
if [ ! -e "$sitesEnabled/$site" ]; then
|
||||||
echo "SELECT A WEBSITE:"
|
echo "Site does not appear to be enabled."
|
||||||
for site in ${sites[@]}; do
|
else
|
||||||
echo -e "$i:\t${sites[$i]}"
|
rm "$sitesEnabled/$site"
|
||||||
((i++))
|
echo "Site disabled."
|
||||||
done
|
fi
|
||||||
|
|
||||||
read -p "Enter number for website: " i
|
|
||||||
SELECTED_SITE="${sites[$i]}"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ngx_sites() {
|
# List all available and enabled sites
|
||||||
case "$1" in
|
list_sites() {
|
||||||
available) dir="$NGINX_SITES_AVAILABLE";;
|
echo "Available sites (not enabled):"
|
||||||
enabled) dir="$NGINX_SITES_ENABLED";;
|
for site in "$sitesAvail"/*; do
|
||||||
esac
|
local site=$(basename "$site")
|
||||||
|
if [ ! -e "$sitesEnabled/$site" ]; then
|
||||||
|
echo -e "\t$site"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
for file in $dir/*; do
|
echo -e "\nEnabled sites:"
|
||||||
echo -e "\t${file#*$dir/}"
|
for site in "$sitesEnabled"/*; do
|
||||||
done
|
echo -e "\t$(basename "$site")"
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
ngx_reload() {
|
# Check if the editor is available
|
||||||
read -p "Would you like to reload the Nginx configuration now? (Y/n) " reload
|
check_editor() {
|
||||||
[[ "$reload" != "n" && "$reload" != "N" ]] && invoke-rc.d nginx reload
|
if ! command -v $1 >/dev/null 2>&1; then
|
||||||
|
echo "Editor $1 not found."
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
ngx_reload_now() {
|
# Edit a specified site using the preferred editor
|
||||||
invoke-rc.d nginx reload
|
edit_site() {
|
||||||
|
local site=$1
|
||||||
|
if [ ! -e "$sitesAvail/$site" ]; then
|
||||||
|
echo "Site does not appear to exist."
|
||||||
|
else
|
||||||
|
$editor "$sitesAvail/$site"
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
ngx_error() {
|
parse_arguments() {
|
||||||
echo -e "${0##*/}: ERROR: $1"
|
while [ "$#" -gt 0 ]; do
|
||||||
[[ "$2" ]] && ngx_help
|
local key="$1"
|
||||||
exit 1
|
case "$key" in
|
||||||
|
-e|--enable)
|
||||||
|
if [ -z "$2" ]; then
|
||||||
|
echo "No site specified to enable. Use -h for help."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
check_sudo
|
||||||
|
site_to_enable="$2"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-d|--disable)
|
||||||
|
if [ -z "$2" ]; then
|
||||||
|
echo "No site specified to disable. Use -h for help."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
check_sudo
|
||||||
|
site_to_disable="$2"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-ed|--edit)
|
||||||
|
if [ -z "$2" ]; then
|
||||||
|
echo "No site specified to edit. Use -h for help."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
check_sudo
|
||||||
|
site_to_edit="$2"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
--editor)
|
||||||
|
if [ -z "$2" ]; then
|
||||||
|
echo "No editor specified. Use -h for help."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
editor="$2"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-l|--list)
|
||||||
|
list_sites=true
|
||||||
|
;;
|
||||||
|
-h|--help)
|
||||||
|
display_help=true
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Unknown option: $key"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
ngx_help() {
|
# Main function
|
||||||
echo "Usage: ${0##*/} [options]"
|
main() {
|
||||||
echo "Options:"
|
initialize_variables
|
||||||
echo -e "\t<-e|--enable> <site>\tEnable site"
|
parse_arguments "$@"
|
||||||
echo -e "\t<-d|--disable> <site>\tDisable site"
|
get_config_dirs
|
||||||
echo -e "\t<-x|--edit>\t\tEdit site"
|
|
||||||
echo -e "\t<-l|--list>\t\tList sites"
|
if [ -z "$1" ]; then
|
||||||
echo -e "\t<-h|--help>\t\tDisplay help"
|
display_help=true
|
||||||
echo -e "\n\tIf <site> is left out a selection of options will be presented."
|
fi
|
||||||
echo -e "\tIt is assumed you are using the default sites-enabled and"
|
|
||||||
echo -e "\tsites-disabled located at $NGINX_CONF_DIR."
|
if $display_help; then
|
||||||
|
if [ -n "$site_to_enable" ] || [ -n "$site_to_disable" ] || [ -n "$site_to_edit" ] || $list_sites; then
|
||||||
|
echo "Warning: --help was provided along with other options. Ignoring other options."
|
||||||
|
fi
|
||||||
|
show_help
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if check_editor "$editor"; then
|
||||||
|
if [ -n "$site_to_enable" ]; then
|
||||||
|
enable_site "$site_to_enable"
|
||||||
|
fi
|
||||||
|
if [ -n "$site_to_disable" ]; then
|
||||||
|
disable_site "$site_to_disable"
|
||||||
|
fi
|
||||||
|
if [ -n "$site_to_edit" ]; then
|
||||||
|
edit_site "$site_to_edit"
|
||||||
|
fi
|
||||||
|
if $list_sites; then
|
||||||
|
list_sites
|
||||||
|
fi
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
##
|
main "$@"
|
||||||
# Core Piece
|
|
||||||
##
|
|
||||||
|
|
||||||
case "$1" in
|
|
||||||
-e|--enable) ngx_enable_site;;
|
|
||||||
-d|--disable) ngx_disable_site;;
|
|
||||||
-x|--edit) ngx_edit_site;;
|
|
||||||
-r|--reload) ngx_reload_now;;
|
|
||||||
-l|--list) ngx_list_site;;
|
|
||||||
-h|--help) ngx_help;;
|
|
||||||
*) ngx_error "No Options Selected" 1; ngx_help;;
|
|
||||||
esac
|
|
||||||
Reference in New Issue
Block a user