Fix shellcheck warning

This commit is contained in:
joglomedia
2019-07-31 21:43:34 +07:00
parent b3a99f126b
commit 680d8f2f1a
3 changed files with 200 additions and 174 deletions

0
bin/lemper-cli.sh Normal file → Executable file
View File

View File

@@ -16,15 +16,14 @@
# | Original concept: Fideloper <https://gist.github.com/fideloper/9063376> |
# +-------------------------------------------------------------------------+
# Version Control
# Version Control.
APP_NAME=$(basename "$0")
APP_VERSION="1.6.0"
LAST_UPDATE="16/07/2019"
INSTALL_DIR=$(pwd)
# Test mode.
DRYRUN=false
# Decorator
# Decorator.
RED=91
GREEN=92
YELLOW=93
@@ -47,7 +46,7 @@ function echo_color() {
}
function error() {
local error_message="$@"
#local error_message="$@"
echo_color "$RED" -n "Error: " >&2
echo "$@" >&2
}
@@ -78,11 +77,12 @@ function warning() {
# actually make any changes.
function run() {
if "$DRYRUN"; then
echo_color "$YELLOW" -n "would run"
echo " $@"
echo_color "$YELLOW" -n "would run "
echo "$@"
else
if ! "$@"; then
error "Failure running '$@', exiting."
local CMDSTR="$*"
error "Failure running '${CMDSTR}', exiting."
exit 1
fi
fi
@@ -95,7 +95,7 @@ if [ "$(id -u)" -ne 0 ]; then
exit 1 #error
fi
# Check prerequisite packages
# Check prerequisite packages.
if [[ ! -f $(command -v unzip) || ! -f $(command -v git) || ! -f $(command -v rsync) ]]; then
warning "${APP_NAME^} requires rsync, unzip and git, please install it first!"
echo "help: sudo apt-get install rsync unzip git"
@@ -103,7 +103,7 @@ if [[ ! -f $(command -v unzip) || ! -f $(command -v git) || ! -f $(command -v rs
fi
## Show usage
# Output to STDERR
# output to STDERR.
#
function show_usage {
cat <<- _EOF_
@@ -148,7 +148,7 @@ _EOF_
## Output Default virtual host directive, fill with user input
# To be outputted into new file
# Work for default and WordPress site
# Work for default and WordPress site.
#
function create_vhost_default() {
cat <<- _EOF_
@@ -228,7 +228,7 @@ _EOF_
}
## Output Drupal virtual host directive, fill with user input
# To be outputted into new file
# To be outputted into new file.
#
function create_vhost_drupal() {
cat <<- _EOF_
@@ -313,7 +313,7 @@ _EOF_
}
## Output Laravel virtual host skeleton, fill with user input
# To be outputted into new file
# To be outputted into new file.
#
function create_vhost_laravel() {
cat <<- _EOF_
@@ -394,7 +394,7 @@ _EOF_
}
## Output Phalcon virtual host skeleton, fill with user input
# To be outputted into new file
# To be outputted into new file.
#
function create_vhost_phalcon() {
cat <<- _EOF_
@@ -479,7 +479,7 @@ server {
_EOF_
}
## Output Wordpress Multisite vHost header
## Output Wordpress Multisite vHost header.
#
function prepare_vhost_wpms() {
cat <<- _EOF_
@@ -492,7 +492,7 @@ map \$http_host \$blogid {
_EOF_
}
## Output server block for HTTP to HTTPS redirection
## Output server block for HTTP to HTTPS redirection.
#
function http_to_https() {
cat <<- _EOF_
@@ -507,7 +507,7 @@ server {
## Automatically redirect site to HTTPS protocol.
location / {
return 301 https://$server_name$request_uri;
return 301 https://\$server_name\$request_uri;
}
}
@@ -515,7 +515,7 @@ _EOF_
}
## Output index.html skeleton for default index page
# To be outputted into new index.html file in document root
# To be outputted into new index.html file in document root.
#
function create_index_file() {
cat <<- _EOF_
@@ -549,7 +549,7 @@ _EOF_
}
## Output PHP-FPM pool configuration
# To be outputted into new pool file in fpm/pool.d
# To be outputted into new pool file in fpm/pool.d.
#
function create_fpm_pool_conf() {
cat <<- _EOF_
@@ -594,44 +594,45 @@ _EOF_
}
## Install WordPress
# Installing WordPress skeleton
# Installing WordPress skeleton.
#
function install_wordpress() {
CLONE_SKELETON=${1:-false}
# Clone new WordPress skeleton files
if [ ${CLONE_SKELETON} == true ]; then
# Check WordPress install directory
if [ ! -d ${WEBROOT}/wp-admin ]; then
if [ "${CLONE_SKELETON}" == true ]; then
# Check WordPress install directory.
if [ ! -d "${WEBROOT}/wp-admin" ]; then
status "Copying WordPress skeleton files..."
run wget --no-check-certificate -q https://wordpress.org/latest.zip
run unzip -q latest.zip
run rsync -r wordpress/ ${WEBROOT}
run rsync -r wordpress/ "${WEBROOT}"
run rm -f latest.zip
run rm -fr wordpress
else
warning "It seems that WordPress files already exists."
fi
else
# Create default index file
# Create default index file.
status "Creating default WordPress index file..."
create_index_file > ${WEBROOT}/index.html
run chown ${USERNAME}:${USERNAME} ${WEBROOT}/index.html
create_index_file > "${WEBROOT}/index.html"
run chown "${USERNAME}:${USERNAME}" "${WEBROOT}/index.html"
fi
# Pre-install nginx helper plugin
if [[ -d ${WEBROOT}/wp-content/plugins && ! -d ${WEBROOT}/wp-content/plugins/nginx-helper ]]; then
# Pre-install nginx helper plugin.
if [[ -d "${WEBROOT}/wp-content/plugins" && ! -d "${WEBROOT}/wp-content/plugins/nginx-helper" ]]; then
status "Copying Nginx Helper plugin into WordPress install..."
warning "Please activate the plugin after WordPress installation!"
run wget --no-check-certificate -q https://downloads.wordpress.org/plugin/nginx-helper.zip
run unzip -q nginx-helper.zip
run mv nginx-helper ${WEBROOT}/wp-content/plugins/
run mv nginx-helper "${WEBROOT}/wp-content/plugins/"
run rm -f nginx-helper.zip
fi
}
# Get server IP Address
# Get server IP Address.
function get_ip_addr() {
IP_INTERNAL=$(ip addr | grep 'inet' | grep -v inet6 | \
grep -vE '127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | \
@@ -648,33 +649,19 @@ function get_ip_addr() {
## Main App
#
function init_app() {
getopt --test
if [ "$?" != 4 ]; then
# Even Centos 5 and Ubuntu 10 LTS have new-style getopt, so I don't expect
# this to be hit in practice on systems that are actually able to run
# Nginx web server.
fail "Your version of getopt is too old. Exiting with no changes made."
fi
opts=$(getopt -o u:d:f:w:p:cshv \
OPTS=$(getopt -o u:d:f:w:p:cshv \
-l username:,domain-name:,framework:,webroot:,php-version: \
-l enable-fastcgi-cache,clone-skeleton,help,version \
-n "$APP_NAME" -- "$@")
# Sanity Check - are there an arguments with value?
if [ $? != 0 ]; then
fail "Terminating..."
exit 1
fi
eval set -- "$opts"
eval set -- "${OPTS}"
# Default value
FRAMEWORK="default"
PHP_VERSION="7.3"
ENABLE_FASTCGI_CACHE=false
ENABLE_PAGESPEED=false
ENABLE_HTTPS=false
#ENABLE_HTTPS=false
CLONE_SKELETON=false
DRYRUN=false
@@ -682,26 +669,27 @@ function init_app() {
MAIN_ARGS=0
# Parse flags
while true; do
while true
do
case "${1}" in
-u | --username) shift
USERNAME="${1}"
MAIN_ARGS=$(($MAIN_ARGS + 1))
MAIN_ARGS=$((MAIN_ARGS + 1))
shift
;;
-d | --domain-name) shift
SERVERNAME="${1}"
MAIN_ARGS=$(($MAIN_ARGS + 1))
MAIN_ARGS=$((MAIN_ARGS + 1))
shift
;;
-w | --webroot) shift
WEBROOT="${1%%+(/)}"
MAIN_ARGS=$(($MAIN_ARGS + 1))
MAIN_ARGS=$((MAIN_ARGS + 1))
shift
;;
-f | --framework) shift
FRAMEWORK="${1}"
MAIN_ARGS=$(($MAIN_ARGS + 1))
MAIN_ARGS=$((MAIN_ARGS + 1))
shift
;;
-p | --php-version) shift
@@ -734,19 +722,19 @@ function init_app() {
if [ ${MAIN_ARGS} -ge 4 ]; then
# Additional Check - are user already exist?
if [[ -z $(getent passwd ${USERNAME}) ]]; then
if [[ -z $(getent passwd "${USERNAME}") ]]; then
fail -e "\nError: The user ${USERNAME} does not exist, please add new user first! Aborting...
Help: useradd username, try ${APP_NAME} -h for more helps."
fi
# Check PHP fpm version is exists.
if [[ -n $(command -v php-fpm${PHP_VERSION}) && -d /etc/php/${PHP_VERSION}/fpm ]]; then
if [[ -n $(command -v "php-fpm${PHP_VERSION}") && -d "/etc/php/${PHP_VERSION}/fpm" ]]; then
# Additional check - if FPM user's pool already exist
if [ ! -f "/etc/php/${PHP_VERSION}/fpm/pool.d/${USERNAME}.conf" ]; then
warning "The PHP${PHP_VERSION} FPM pool configuration for user ${USERNAME} doesn't exist."
echo "Creating new PHP-FPM pool [${USERNAME}] configuration..."
create_fpm_pool_conf > /etc/php/${PHP_VERSION}/fpm/pool.d/${USERNAME}.conf
create_fpm_pool_conf > "/etc/php/${PHP_VERSION}/fpm/pool.d/${USERNAME}.conf"
run touch "/var/log/php${PHP_VERSION}-fpm_slow.${USERNAME}.log"
# Restart PHP FPM
@@ -769,35 +757,35 @@ Help: useradd username, try ${APP_NAME} -h for more helps."
VHOST_FILE="/etc/nginx/sites-available/${SERVERNAME}.conf"
# Check if vhost not exists.
if [ ! -f ${VHOST_FILE} ]; then
if [ ! -f "${VHOST_FILE}" ]; then
echo "Adding domain ${SERVERNAME} to virtual host..."
# Creates document root.
if [ ! -d ${WEBROOT} ]; then
if [ ! -d "${WEBROOT}" ]; then
echo "Creating web root directory: ${WEBROOT}..."
run mkdir -p ${WEBROOT}
run chown -R ${USERNAME}:${USERNAME} ${WEBROOT}
run chmod 755 ${WEBROOT}
run mkdir -p "${WEBROOT}"
run chown -R "${USERNAME}:${USERNAME}" "${WEBROOT}"
run chmod 755 "${WEBROOT}"
fi
echo "Selecting ${FRAMEWORK^} framewrok..."
# Ugly hacks for custom framework-specific configs + Skeleton auto installer.
case ${FRAMEWORK} in
case "${FRAMEWORK}" in
drupal)
echo "Setting up Drupal virtual host..."
# Clone new Drupal skeleton files.
if [ ${CLONE_SKELETON} == true ]; then
# Check Drupal install directory.
if [ ! -d ${WEBROOT}/core/lib/Drupal ]; then
if [ ! -d "${WEBROOT}/core/lib/Drupal" ]; then
status "Copying Drupal latest skeleton files..."
run wget --no-check-certificate -O drupal.zip -q \
https://www.drupal.org/download-latest/zip
run unzip -q drupal.zip
run rsync -rq drupal-*/ ${WEBROOT}
run rsync -rq drupal-*/ "${WEBROOT}"
run rm -f drupal.zip
run rm -fr drupal-*/
else
@@ -806,14 +794,14 @@ Help: useradd username, try ${APP_NAME} -h for more helps."
else
# Create default index file.
status "Creating default index file..."
create_index_file > ${WEBROOT}/index.html
create_index_file > "${WEBROOT}/index.html"
run chown ${USERNAME}:${USERNAME} ${WEBROOT}/index.html
run chown "${USERNAME}:${USERNAME}" "${WEBROOT}/index.html"
fi
# Create vhost.
echo "Creating virtual host file: ${VHOST_FILE}..."
create_vhost_drupal > ${VHOST_FILE}
create_vhost_drupal > "${VHOST_FILE}"
status "New domain ${SERVERNAME} has been added to virtual host."
;;
@@ -824,22 +812,22 @@ Help: useradd username, try ${APP_NAME} -h for more helps."
# clone new Laravel files.
if [ ${CLONE_SKELETON} == true ]; then
# Check Laravel install.
if [ ! -f ${WEBROOT}/server.php ]; then
if [ ! -f "${WEBROOT}/server.php" ]; then
status "Copying Laravel skeleton files..."
run git clone -q https://github.com/laravel/laravel.git ${WEBROOT}
run git clone -q https://github.com/laravel/laravel.git "${WEBROOT}"
else
warning "It seems that Laravel skeleton files already exists."
fi
else
# Create default index file.
status "Creating default index file..."
create_index_file > ${WEBROOT}/index.html
run chown ${USERNAME}:${USERNAME} ${WEBROOT}/index.html
create_index_file > "${WEBROOT}/index.html"
run chown "${USERNAME}:${USERNAME}" "${WEBROOT}/index.html"
fi
# Create vhost.
echo "Creating virtual host file: ${VHOST_FILE}..."
create_vhost_laravel > ${VHOST_FILE}
create_vhost_laravel > "${VHOST_FILE}"
status "New domain ${SERVERNAME} has been added to virtual host."
;;
@@ -850,7 +838,7 @@ Help: useradd username, try ${APP_NAME} -h for more helps."
# Create vhost.
echo "Creating virtual host file: ${VHOST_FILE}..."
create_vhost_phalcon > ${VHOST_FILE}
create_vhost_phalcon > "${VHOST_FILE}"
status "New domain ${SERVERNAME} has been added to virtual host."
;;
@@ -861,7 +849,7 @@ Help: useradd username, try ${APP_NAME} -h for more helps."
# Create vhost.
echo "Creating virtual host file: ${VHOST_FILE}..."
create_vhost_default > ${VHOST_FILE}
create_vhost_default > "${VHOST_FILE}"
status "New domain ${SERVERNAME} has been added to virtual host."
;;
@@ -869,11 +857,11 @@ Help: useradd username, try ${APP_NAME} -h for more helps."
echo "Setting up WordPress virtual host..."
# Install WordPress skeleton.
install_wordpress
install_wordpress ${CLONE_SKELETON}
# Create vhost.
echo "Creating virtual host file: ${VHOST_FILE}..."
create_vhost_default > ${VHOST_FILE}
create_vhost_default > "${VHOST_FILE}"
status "New domain ${SERVERNAME} has been added to virtual host."
;;
@@ -881,28 +869,28 @@ Help: useradd username, try ${APP_NAME} -h for more helps."
echo "Setting up WordPress Multi-site virtual host..."
# Install WordPress.
install_wordpress
install_wordpress ${CLONE_SKELETON}
# Pre-populate blog id mapping, used by Nginx vhost conf.
if [ ! -d ${WEBROOT}/wp-content/uploads ]; then
run mkdir ${WEBROOT}/wp-content/uploads
if [ ! -d "${WEBROOT}/wp-content/uploads" ]; then
run mkdir "${WEBROOT}/wp-content/uploads"
fi
if [ ! -d run mkdir ${WEBROOT}/wp-content/uploads/nginx-helper ]; then
run mkdir ${WEBROOT}/wp-content/uploads/nginx-helper
if [ ! -d "${WEBROOT}/wp-content/uploads/nginx-helper" ]; then
run mkdir "${WEBROOT}/wp-content/uploads/nginx-helper"
fi
if [ ! -f ${WEBROOT}/wp-content/uploads/nginx-helper/map.conf ]; then
run touch ${WEBROOT}/wp-content/uploads/nginx-helper/map.conf
if [ ! -f "${WEBROOT}/wp-content/uploads/nginx-helper/map.conf" ]; then
run touch "${WEBROOT}/wp-content/uploads/nginx-helper/map.conf"
fi
echo "Creating virtual host file: ${VHOST_FILE}..."
# Prepare vhost specific rule for WordPress Multisite.
prepare_vhost_wpms > ${VHOST_FILE}
prepare_vhost_wpms > "${VHOST_FILE}"
# Create vhost.
create_vhost_default >> ${VHOST_FILE}
create_vhost_default >> "${VHOST_FILE}"
status "New domain ${SERVERNAME} has been added to virtual host."
;;
@@ -911,18 +899,18 @@ Help: useradd username, try ${APP_NAME} -h for more helps."
echo "Setting up FileRun virtual host..."
# Install FileRun skeleton.
if [ ! -f ${WEBROOT}/system/classes/filerun.php ]; then
if [ ! -f "${WEBROOT}/system/classes/filerun.php" ]; then
# Clone new Filerun files.
if [ ${CLONE_SKELETON} == true ]; then
echo "Copying FileRun skeleton files..."
run wget -q -O FileRun.zip http://www.filerun.com/download-latest
run unzip -q FileRun.zip -d ${WEBROOT}
run unzip -q FileRun.zip -d "${WEBROOT}"
run rm -f FileRun.zip
else
# Create default index file.
echo "Creating default index files..."
create_index_file > ${WEBROOT}/index.html
run chown ${USERNAME}:${USERNAME} ${WEBROOT}/index.html
create_index_file > "${WEBROOT}/index.html"
run chown "${USERNAME}:${USERNAME}" "${WEBROOT}/index.html"
fi
else
warning "FileRun skeleton files already exists."
@@ -930,18 +918,18 @@ Help: useradd username, try ${APP_NAME} -h for more helps."
# Create vhost.
echo "Creating virtual host file: ${VHOST_FILE}..."
create_vhost_default > ${VHOST_FILE}
create_vhost_default > "${VHOST_FILE}"
status "New domain ${SERVERNAME} has been added to virtual host."
;;
codeigniter|mautic|default)
# Create default index file.
create_index_file > ${WEBROOT}/index.html
run chown ${USERNAME}:${USERNAME} ${WEBROOT}/index.html
create_index_file > "${WEBROOT}/index.html"
run chown "${USERNAME}:${USERNAME}" "${WEBROOT}/index.html"
# Create default vhost.
echo "Creating virtual host file: ${VHOST_FILE}..."
create_vhost_default > ${VHOST_FILE}
create_vhost_default > "${VHOST_FILE}"
status "New domain ${SERVERNAME} has been added to virtual host."
;;
@@ -956,11 +944,11 @@ Help: useradd username, try ${APP_NAME} -h for more helps."
if [ ${ENABLE_FASTCGI_CACHE} == true ]; then
echo "Enable FastCGI cache for ${SERVERNAME}..."
if [ -f /etc/nginx/includes/rules_fastcgi_cache.conf; ]; then
if [ -f /etc/nginx/includes/rules_fastcgi_cache.conf ]; then
# enable cached directives
run sed -i "s|#include\ /etc/nginx/includes/rules_fastcgi_cache.conf|include\ /etc/nginx/includes/rules_fastcgi_cache.conf|g" ${VHOST_FILE}
run sed -i "s|#include\ /etc/nginx/includes/rules_fastcgi_cache.conf|include\ /etc/nginx/includes/rules_fastcgi_cache.conf|g" "${VHOST_FILE}"
# enable fastcgi_cache conf
run sed -i "s|#include\ /etc/nginx/includes/fastcgi_cache.conf|include\ /etc/nginx/includes/fastcgi_cache.conf|g" ${VHOST_FILE}
run sed -i "s|#include\ /etc/nginx/includes/fastcgi_cache.conf|include\ /etc/nginx/includes/fastcgi_cache.conf|g" "${VHOST_FILE}"
else
warning "FastCGI cache is not enabled. There is no cached version of ${FRAMEWORK^} directive."
fi
@@ -972,17 +960,17 @@ Help: useradd username, try ${APP_NAME} -h for more helps."
if [[ -f /etc/nginx/includes/mod_pagespeed.conf && -f /etc/nginx/modules-enabled/50-mod-pagespeed.conf ]]; then
# enable mod pagespeed
run sed -i "s|#include\ /etc/nginx/includes/mod_pagespeed.conf|include\ /etc/nginx/includes/mod_pagespeed.conf|g" ${VHOST_FILE}
run sed -i "s|#include\ /etc/nginx/includes/mod_pagespeed.conf|include\ /etc/nginx/includes/mod_pagespeed.conf|g" "${VHOST_FILE}"
else
warning "PageSpeed is not enabled. Nginx must be installed with Mod_PageSpeed module enabled."
fi
fi
# Fix document root ownership
run chown -R ${USERNAME}:${USERNAME} ${WEBROOT}
run chown -R "${USERNAME}:${USERNAME}" "${WEBROOT}"
# Fix document root permission
if [ "$(ls -A ${WEBROOT})" ]; then
if [ "$(ls -A "${WEBROOT}")" ]; then
run find "${WEBROOT}" -type d -print0 | xargs -0 chmod 755
run find "${WEBROOT}" -type f -print0 | xargs -0 chmod 644
fi
@@ -991,7 +979,7 @@ Help: useradd username, try ${APP_NAME} -h for more helps."
# Enable site
#cd "/etc/nginx/sites-enabled"
run ln -s /etc/nginx/sites-available/${SERVERNAME}.conf /etc/nginx/sites-enabled/${SERVERNAME}.conf
run ln -s "/etc/nginx/sites-available/${SERVERNAME}.conf" "/etc/nginx/sites-enabled/${SERVERNAME}.conf"
# Reload Nginx
echo "Reloading Nginx configuration..."
@@ -1004,7 +992,7 @@ Help: useradd username, try ${APP_NAME} -h for more helps."
echo "Something went wrong with Nginx configuration."
fi
if [[ -f /etc/nginx/sites-enabled/${SERVERNAME}.conf && -e /var/run/nginx.pid ]]; then
if [[ -f "/etc/nginx/sites-enabled/${SERVERNAME}.conf" && -e /var/run/nginx.pid ]]; then
status "Your ${SERVERNAME} successfully added to Nginx virtual host.";
else
fail "An error occurred when adding ${SERVERNAME} to Nginx virtual host.";

View File

@@ -1,9 +1,9 @@
#!/bin/bash
# +-------------------------------------------------------------------------+
# | NgxTool - Simple Nginx vHost Manager |
# | Lemper manage - Simple Nginx Virtual Host Manager |
# +-------------------------------------------------------------------------+
# | Copyright (c) 2014-2019 ESLabs (https://eslabs.id/ngxtool) |
# | Copyright (c) 2014-2019 ESLabs (https://eslabs.id/lemper) |
# +-------------------------------------------------------------------------+
# | This source file is subject to the GNU General Public License |
# | that is bundled with this package in the file LICENSE.md. |
@@ -15,10 +15,11 @@
# | Authors: Edi Septriyanto <eslabs.id@gmail.com> |
# +-------------------------------------------------------------------------+
set -e
# Version control
APP_NAME=$(basename "$0")
APP_VERSION="1.6.1"
LAST_UPDATE="10/07/2019"
# Decorator
RED=91
@@ -45,7 +46,7 @@ function echo_color() {
}
function error() {
local error_message="$@"
#local error_message="$@"
echo_color "$RED" -n "Error: " >&2
echo "$@" >&2
}
@@ -75,12 +76,13 @@ function warning() {
# Additionally, this allows us to easily have a dryrun mode where we don't
# actually make any changes.
function run() {
if "$DRYRUN"; then
echo_color "$YELLOW" -n "would run"
echo " $@"
if "${DRYRUN}"; then
echo_color "$YELLOW" -n "would run "
echo "$@"
else
if ! "$@"; then
error "Failure running '$@', exiting."
local CMDSTR="$*"
error "Failure running '${CMDSTR}', exiting."
exit 1
fi
fi
@@ -136,13 +138,13 @@ _EOF_
# Enable vhost
function enable_vhost() {
# Verify user input hostname (domain name)
verify_host ${1}
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
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
@@ -157,13 +159,13 @@ function enable_vhost() {
# Disable vhost
function disable_vhost() {
# Verify user input hostname (domain name)
verify_host ${1}
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
if [ -f "/etc/nginx/sites-enabled/${1}.conf" ]; then
run unlink "/etc/nginx/sites-enabled/${1}.conf"
reload_nginx
@@ -178,27 +180,27 @@ function disable_vhost() {
# Remove vhost
function remove_vhost() {
# Verify user input hostname (domain name)
verify_host ${1}
verify_host "${1}"
echo -e "Removing virtual host is not reversible."
read -t 30 -rp "Press [Enter] to continue..." </dev/tty
# Remove Nginx's vhost config.
if [ -f /etc/nginx/sites-enabled/${1}.conf ]; then
run unlink /etc/nginx/sites-enabled/${1}.conf
if [ -f "/etc/nginx/sites-enabled/${1}.conf" ]; then
run unlink "/etc/nginx/sites-enabled/${1}.conf"
fi
run rm -f /etc/nginx/sites-available/${1}.conf
run rm -f "/etc/nginx/sites-available/${1}.conf"
status -e "\nVirtual host configuration file removed."
# Remove vhost root directory.
echo -en "\nDo you want to delete website root directory? [y/n]: "; read isdeldir
if [[ "${isdeldir}" == Y* || "${isdeldir}" == y* ]]; then
echo -n "Enter the real path to website root directory: "; read sitedir
echo -en "\nDo you want to delete website root directory? [y/n]: "; read -rp DELETE_DIR
if [[ "${DELETE_DIR}" == Y* || "${DELETE_DIR}" == y* ]]; then
read -rp "Enter the real path to website root directory: " -e WEBROOT
if [ -d ${sitedir} ]; then
run rm -fr ${sitedir}
if [ -d "${WEBROOT}" ]; then
run rm -fr "${WEBROOT}"
status -e "\nVirtual host root directory removed."
else
warning -e "\nSorry, directory couldn't be found. Skipped..."
@@ -206,21 +208,21 @@ function remove_vhost() {
fi
# Drop MySQL database.
echo -en "\nDo you want to Drop database associated with this vhost? [y/n]: "; read isdropdb
if [[ "${isdropdb}" == Y* || "${isdropdb}" == y* ]]; then
echo -en "\nDo you want to Drop database associated with this vhost? [y/n]: "; read -rp DROP_DB
if [[ "${DROP_DB}" == Y* || "${DROP_DB}" == y* ]]; then
until [[ "$MYSQLUSER" != "" ]]; do
read -rp "MySQL Username: " -e MYSQLUSER
done
until [[ "$MYSQLPSWD" != "" ]]; do
echo -n "MySQL Password: "; stty -echo; read MYSQLPSWD; stty echo; echo
echo -n "MySQL Password: "; stty -echo; read -r MYSQLPSWD; stty echo; echo
done
echo -e "\nStarting to drop database...\nPlease select your database name below!"
echo "==============="
# Show user's databases
mysql -u $MYSQLUSER -p"$MYSQLPSWD" -e "SHOW DATABASES" | grep -E -v "Database|mysql|*_schema"
mysql -u "$MYSQLUSER" -p"$MYSQLPSWD" -e "SHOW DATABASES" | grep -E -v "Database|mysql|*_schema"
echo "+----------------------+"
@@ -228,9 +230,9 @@ function remove_vhost() {
read -rp "MySQL Database: " -e DBNAME
done
if [ -d /var/lib/mysql/${DBNAME} ]; then
if [ -d "/var/lib/mysql/${DBNAME}" ]; then
echo -e "Dropping database..."
mysql -u $MYSQLUSER -p"$MYSQLPSWD" -e "DROP DATABASE $DBNAME"
mysql -u "$MYSQLUSER" -p"$MYSQLPSWD" -e "DROP DATABASE $DBNAME"
status -e "Database [${DBNAME}] dropped."
else
warning -e "\nSorry, database ${DBNAME} not found. Skipped..."
@@ -246,17 +248,18 @@ function remove_vhost() {
# Enable fastcgi cache
function enable_fastcgi_cache() {
# Verify user input hostname (domain name)
verify_host ${1}
verify_host "${1}"
echo "Enabling FastCGI cache for ${1}..."
if [ -f /etc/nginx/includes/rules_fastcgi_cache.conf ]; then
# enable cached directives
run sed -i "s|#include\ /etc/nginx/includes/rules_fastcgi_cache.conf|include\ /etc/nginx/includes/rules_fastcgi_cache.conf|g" /etc/nginx/sites-available/${1}.conf
run sed -i "s|#include\ /etc/nginx/includes/rules_fastcgi_cache.conf|include\ /etc/nginx/includes/rules_fastcgi_cache.conf|g" \
"/etc/nginx/sites-available/${1}.conf"
# enable fastcgi_cache conf
run sed -i "s|#include\ /etc/nginx/includes/fastcgi_cache.conf|include\ /etc/nginx/includes/fastcgi_cache.conf|g" \
/etc/nginx/sites-available/${1}.conf
"/etc/nginx/sites-available/${1}.conf"
reload_nginx
else
@@ -268,17 +271,18 @@ function enable_fastcgi_cache() {
# Disable fastcgi cache
function disable_fastcgi_cache() {
# Verify user input hostname (domain name)
verify_host ${1}
verify_host "${1}"
echo "Disabling FastCGI cache for ${1}..."
if [ -f /etc/nginx/includes/rules_fastcgi_cache.conf ]; then
# enable cached directives
run sed -i "s|include\ /etc/nginx/includes/rules_fastcgi_cache.conf|#include\ /etc/nginx/includes/rules_fastcgi_cache.conf|g" /etc/nginx/sites-available/${1}.conf
run sed -i "s|include\ /etc/nginx/includes/rules_fastcgi_cache.conf|#include\ /etc/nginx/includes/rules_fastcgi_cache.conf|g" \
"/etc/nginx/sites-available/${1}.conf"
# enable fastcgi_cache conf
run sed -i "s|include\ /etc/nginx/includes/fastcgi_cache.conf|#include\ /etc/nginx/includes/fastcgi_cache.conf|g" \
/etc/nginx/sites-available/${1}.conf
"/etc/nginx/sites-available/${1}.conf"
reload_nginx
else
@@ -290,14 +294,14 @@ function disable_fastcgi_cache() {
# Enable Mod PageSpeed
function enable_mod_pagespeed() {
# Verify user input hostname (domain name)
verify_host ${1}
verify_host "${1}"
echo "Enabling Mod PageSpeed for ${1}..."
if [[ -f /etc/nginx/includes/mod_pagespeed.conf && -f /etc/nginx/modules-enabled/50-mod-pagespeed.conf ]]; then
# enable mod pagespeed
run sed -i "s|#include\ /etc/nginx/includes/mod_pagespeed.conf|include\ /etc/nginx/includes/mod_pagespeed.conf|g" \
/etc/nginx/sites-available/${1}.conf
"/etc/nginx/sites-available/${1}.conf"
reload_nginx
else
@@ -309,14 +313,14 @@ function enable_mod_pagespeed() {
# Disable Mod PageSpeed
function disable_mod_pagespeed() {
# Verify user input hostname (domain name)
verify_host ${1}
verify_host "${1}"
echo "Disabling Mod PageSpeed for ${1}..."
if [[ -f /etc/nginx/includes/mod_pagespeed.conf && -f /etc/nginx/modules-enabled/50-mod-pagespeed.conf ]]; then
# Enable mod pagespeed
run sed -i "s|include\ /etc/nginx/includes/mod_pagespeed.conf|#include\ /etc/nginx/includes/mod_pagespeed.conf|g" \
/etc/nginx/sites-available/${1}.conf
"/etc/nginx/sites-available/${1}.conf"
reload_nginx
else
@@ -328,16 +332,15 @@ function disable_mod_pagespeed() {
# Enable ssl
function enable_ssl() {
# Verify user input hostname (domain name)
verify_host ${1}
verify_host "${1}"
#TODO: Generate Let's Encrypt SSL using Certbot
if [[ ! -d /etc/nginx/ssl/${1} ]]; then
if [[ ! -d "/etc/nginx/ssl/${1}" ]]; then
echo "Certbot: Get Let's Encrypt certificate..."
#generate certbot
if [ ! -d /etc/nginx/ssl ]; then
cd /etc/nginx/ssl/
#mkdir /etc/nginx/ssl/${1}
run mkdir "/etc/nginx/ssl/${1}"
fi
fi
@@ -345,8 +348,8 @@ function enable_ssl() {
if [ ! -f /etc/letsencrypt/ssl-dhparams-4096.pem ]; then
echo "Generating Diffie-Hellman parameters for enhanced security..."
#openssl dhparam -out /etc/letsencrypt/ssl-dhparams-2048.pem 2048
#openssl dhparam -out /etc/letsencrypt/ssl-dhparams-4096.pem 4096
#run openssl dhparam -out /etc/letsencrypt/ssl-dhparams-2048.pem 2048
#run openssl dhparam -out /etc/letsencrypt/ssl-dhparams-4096.pem 4096
fi
exit 0
@@ -355,19 +358,19 @@ function enable_ssl() {
# Disable ssl
function disable_ssl() {
# Verify user input hostname (domain name)
verify_host ${1}
verify_host "${1}"
echo "TODO: Disble SSL"
exit 0
}
function verify_host() {
if [[ -z ${1} ]]; then
if [[ -z "${1}" ]]; then
error "Virtual host (vhost) or domain name is required. Type ${APP_NAME} --help for more info!"
exit 1
fi
if [ ! -f /etc/nginx/sites-available/${1}.conf ]; then
if [ ! -f "/etc/nginx/sites-available/${1}.conf" ]; then
error -e "Sorry, we can't find Nginx virtual host: ${1}..."
exit 1
fi
@@ -407,39 +410,74 @@ function reload_nginx() {
# Main App
#
function init_app() {
#getopt
opts=$(getopt -o vhe:d:r:c:p:s: \
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" -- "$@")
-n "${APP_NAME}" -- "$@")
# Sanity Check - are there an arguments with value?
if [ $? != 0 ]; then
fail "Terminating..."
exit 1
fi
eval set -- "${OPTS}"
eval set -- "$opts"
while true; do
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;;
-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
;;
-h | --help)
show_usage
exit 0
shift 2
;;
-v | --version)
echo "${APP_NAME} version ${APP_VERSION}"
exit 0
shift 2
;;
--) shift
break
;;
*)
fail "Invalid argument: ${1}"
exit 1
;;
esac
done
echo "$APP_NAME: missing required argument"
echo "Try '$APP_NAME --help' for more information."
echo "${APP_NAME}: missing required argument"
echo "Try '${APP_NAME} --help' for more information."
}
# Start running things from a call at the end so if this script is executed