#!/bin/bash # +-------------------------------------------------------------------------+ # | NgxTool - Simple Nginx vHost Manager | # +-------------------------------------------------------------------------+ # | Copyright (c) 2014-2019 ESLabs (https://eslabs.id/ngxtool) | # +-------------------------------------------------------------------------+ # | This source file is subject to the GNU General Public License | # | that is bundled with this package in the file LICENSE.md. | # | | # | If you did not receive a copy of the license and are unable to | # | obtain it through the world-wide-web, please send an email | # | to license@eslabs.id so we can send you a copy immediately. | # +-------------------------------------------------------------------------+ # | Authors: Edi Septriyanto | # +-------------------------------------------------------------------------+ # Version Control APP_NAME=$(basename "$0") APP_VERSION="1.6.1" LAST_UPDATE="10/07/2019" # Decorator RED=91 GREEN=92 YELLOW=93 DRYRUN=false function begin_color() { color="$1" echo -e -n "\e[${color}m" } function end_color() { echo -e -n "\e[0m" } function echo_color() { color="$1" shift begin_color "$color" echo "$@" end_color } function error() { local error_message="$@" echo_color "$RED" -n "Error: " >&2 echo "$@" >&2 } # Prints an error message and exits with an error code. function fail() { error "$@" # Normally I'd use $0 in "usage" here, but since most people will be running # this via curl, that wouldn't actually give something useful. echo >&2 echo "For usage information, run this script with --help" >&2 exit 1 } function status() { echo_color "$GREEN" "$@" } function warning() { echo_color "$YELLOW" "$@" } # If we set -e or -u then users of this script will see it silently exit on # failure. Instead we need to check the exit status of each command manually. # The run function handles exit-status checking for system-changing commands. # Additionally, this allows us to easily have a dryrun mode where we don't # actually make any changes. INITIAL_ENV=$(printenv | sort) function run() { if "$DRYRUN"; then echo_color "$YELLOW" -n "would run" echo " $@" env_differences=$(comm -13 <(echo "$INITIAL_ENV") <(printenv | sort)) if [ -n "$env_differences" ]; then echo " with the following additional environment variables:" echo "$env_differences" | sed 's/^/ /' fi else if ! "$@"; then error "Failure running '$@', exiting." exit 1 fi fi } # May need to run this as sudo! # I have it in /usr/local/bin and run command 'ngxvhost' from anywhere, using sudo. if [ $(id -u) -ne 0 ]; then error "You need to be root to run this script" exit 1 #error fi # Help function show_usage() { cat <<- _EOF_ ${APP_NAME^} ${APP_VERSION} Simple Nginx virtual host (vHost) manager enable/disable/remove Nginx vHost config file on Debian/Ubuntu Server. Requirements: * LEMP stack setup uses [LEMPer](https://github.com/joglomedia/LEMPer) Usage: ${APP_NAME} [OPTION]... Options: -c, --enable-fastcgi-cache Enable PHP FastCGI cache. -d, --disable Disable virtual host. -e, --enable Enable virtual host. -p, --enable-pagespeed Enable Mod PageSpeed. -r, --remove Remove virtual host configuration. -s, --enable-ssl Enable Let's Encrypt SSL certificate. -h, --help Print this message and exit. -v, --version Output version information and exit. Example: ${APP_NAME} --remove example.com For more details visit https://ngxtools.eslabs.id Mail bug reports and suggestions to _EOF_ } # Enable vhost function enable_vhost() { # Verify user input hostname (domain name) verify_host $1 echo "Enabling virtual host: $1..." # Enable Nginx's vhost config. if [[ ! -f /etc/nginx/sites-enabled/$1.conf && -f /etc/nginx/sites-available/$1.conf ]]; then run ln -s /etc/nginx/sites-available/$1.conf /etc/nginx/sites-enabled/$1.conf reload_nginx status "Your virtual host $1 has been enabled..." exit 0 #success else fail "Sorry, we can't find virtual host: $1. Probably, it has been enabled or not created yet." exit 1 fi } # Disable vhost function disable_vhost() { # Verify user input hostname (domain name) verify_host $1 echo "Disabling virtual host: $1..." # Disable Nginx's vhost config. if [ -f /etc/nginx/sites-enabled/$1.conf ]; then run unlink /etc/nginx/sites-enabled/$1.conf reload_nginx status "Your virtual host $1 has been disabled..." exit 0 else fail "Sorry, we can't find $1. Probably, it has been disabled or removed." exit 1 fi } # Remove vhost function remove_vhost() { # Verify user input hostname (domain name) verify_host $1 echo -e "Removing virtual host is not reversible." read -t 10 -p "Press [Enter] to continue..." /dev/null 2>&1 else # Nginx service dead? Try to start it if [[ -n $(which nginx) ]]; then service nginx restart > /dev/null 2>&1 else warning "Something went wrong with your Nginx installation." exit 1 fi fi if [[ $(ps -ef | grep -v grep | grep nginx | wc -l) > 0 ]]; then status "Your change has been successfully applied to Nginx." exit 0 else fail "An error occurred when updating Nginx configuration."; exit 1 fi } # Main App # function init_app() { #getopt opts=$(getopt -o vhe:d:r:c:p:s: \ -l version,help,enable:,disable:,remove:,enable-fastcgi-cache:,disable-fastcgi-cache: \ -l enable-pagespeed:,disable-pagespeed:,enable-ssl:,disable-ssl: \ -n "$APP_NAME" -- "$@") # Sanity Check - are there an arguments with value? if [ $? != 0 ]; then fail "Terminating..." exit 1 fi eval set -- "$opts" while true; do case "$1" in -h | --help) show_usage; exit 0; shift;; -e | --enable) enable_vhost $2; shift 2;; -d | --disable) disable_vhost $2; shift 2;; -r | --remove) remove_vhost $2; shift 2;; -c | --enable-fastcgi-cache) enable_fastcgi_cache $2; shift 2;; --disable-fastcgi-cache) disable_fastcgi_cache $2; shift 2;; -p | --enable-pagespeed) enable_mod_pagespeed $2; shift 2;; --disable-pagespeed) disable_mod_pagespeed $2; shift 2;; -s | --enable-ssl) enable_ssl $2; shift 2;; --disable-ssl) disable_ssl $2; shift 2;; -v | --version) echo "$APP_NAME version $APP_VERSION"; exit 1; shift;; --) shift; break;; esac done echo "$APP_NAME: missing optstring argument" echo "Try '$APP_NAME --help' for more information." } # Start running things from a call at the end so if this script is executed # after a partial download it doesn't do anything. init_app "$@"