From 0cca24d56cf64f03a1fa28475fd05b7aea9864ff Mon Sep 17 00:00:00 2001 From: Eddie Septr Date: Tue, 30 Apr 2019 21:04:57 +0700 Subject: [PATCH] adding decorator --- lemper.sh | 23 +++++--- scripts/add_repo.sh | 35 ++++++----- scripts/clean_server.sh | 19 ++++++ scripts/cleanup_server.sh | 30 ++++++++++ scripts/decorator.sh | 75 ++++++++++++++++++++++++ scripts/install_letsencrypt.sh | 20 ++++--- scripts/install_nginx.sh | 103 ++++++++++++++++++--------------- 7 files changed, 229 insertions(+), 76 deletions(-) create mode 100755 scripts/clean_server.sh create mode 100755 scripts/cleanup_server.sh create mode 100644 scripts/decorator.sh diff --git a/lemper.sh b/lemper.sh index bcd0191..7ba6ad8 100755 --- a/lemper.sh +++ b/lemper.sh @@ -31,23 +31,28 @@ set -e # Work even if somebody does "sh thisscript.sh". +# Include decorator +if [ "$(type -t run)" != "function" ]; then + . scripts/decorator.sh +fi + # Make sure only root can run this installer script if [ $(id -u) -ne 0 ]; then - echo "This script must be run as root..." - exit 0 + error "This script must be run as root..." + exit 1 fi # Make sure this script only run on Ubuntu install if [ ! -f "/etc/lsb-release" ]; then - echo "This installer only work on Ubuntu server..." - exit 0 + warning "This installer only work on Ubuntu server..." + exit 1 else # Variables arch=$(uname -p) IPAddr=$(hostname -i) # export lsb-release vars - . /etc/lsb-release + . /etc/lsb-release MAJOR_RELEASE_NUMBER=$(echo $DISTRIB_RELEASE | awk -F. '{print $1}') fi @@ -60,18 +65,18 @@ cat <<- _EOF_ #========================================================================# # A small tool to install Nginx + MariaDB (MySQL) + PHP on Linux # # # -# For more information please visit http://www.ngxtools.cf # +# For more information please visit https://ngxtools.eslabs.id # #========================================================================# _EOF_ sleep 1 } header_msg -echo "Starting LEMP installation, ensure that you're on a fresh box install!" +echo "Starting LEMP installation... Please ensure that you're on a fresh box install!" read -t 10 -p "Press [Enter] to continue..." /dev/null 2>&1 && pwd ) + . ${BASEDIR}/decorator.sh +fi + echo "Adding repositories..." if [[ "$DISTRIB_RELEASE" == "14.04" || "$MAJOR_RELEASE_NUMBER" == "17" ]]; then @@ -8,38 +14,38 @@ if [[ "$DISTRIB_RELEASE" == "14.04" || "$MAJOR_RELEASE_NUMBER" == "17" ]]; then # Nginx custom with ngx cache purge # https://rtcamp.com/wordpress-nginx/tutorials/single-site/fastcgi-cache-with-purging/ - add-apt-repository ppa:rtcamp/nginx + run add-apt-repository ppa:rtcamp/nginx # MariaDB 10.2 repo MARIADB_VER="10.2" - apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db + run apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 0xcbcb082a1bb943db #add-apt-repository 'deb http://ftp.osuosl.org/pub/mariadb/repo/10.2/ubuntu trusty main' elif [[ "$DISTRIB_RELEASE" == "16.04" || "$MAJOR_RELEASE_NUMBER" == "18" ]]; then # Ubuntu release 16.04, LinuxMint 18 DISTRIB_REPO="xenial" # Nginx custom repo with ngx cache purge - apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3050AC3CD2AE6F03 - sh -c "echo 'deb http://download.opensuse.org/repositories/home:/rtCamp:/EasyEngine/xUbuntu_16.04/ /' >> /etc/apt/sources.list.d/nginx-xenial.list" + run apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3050AC3CD2AE6F03 + run sh -c "echo 'deb http://download.opensuse.org/repositories/home:/rtCamp:/EasyEngine/xUbuntu_16.04/ /' >> /etc/apt/sources.list.d/nginx-xenial.list" # MariaDB 10.3 repo MARIADB_VER="10.3" - apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 + run apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 0xF1656F24C74CD1D8 #add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://ftp.osuosl.org/pub/mariadb/repo/10.3/ubuntu xenial main' elif [[ "$DISTRIB_RELEASE" == "18.04" || "$MAJOR_RELEASE_NUMBER" == "19" ]]; then # Ubuntu release 18.04, LinuxMint 19 DISTRIB_REPO="bionic" # Nginx repo - apt-key fingerprint ABF5BD827BD9BF62 - add-apt-repository ppa:nginx/stable + run apt-key fingerprint ABF5BD827BD9BF62 + run add-apt-repository ppa:nginx/stable # MariaDB 10.3 repo MARIADB_VER="10.3" - apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 + run apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 0xF1656F24C74CD1D8 #add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://ftp.osuosl.org/pub/mariadb/repo/10.3/ubuntu bionic main' else - echo "Sorry, this installation script only work for Ubuntu 14.04, 16.04 & 18.04 and Linux Mint 17, 18 & 19." + warning "Sorry, this installation script only work for Ubuntu 14.04, 16.04 & 18.04 and Linux Mint 17, 18 & 19." exit 0 fi @@ -56,15 +62,16 @@ fi # Add PHP (latest stable) from Ondrej's repo # Source: https://launchpad.net/~ondrej/+archive/ubuntu/php -add-apt-repository ppa:ondrej/php -y +run add-apt-repository ppa:ondrej/php -y # Fix for NO_PUBKEY key servers error -apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4F4EA0AAE5267A6C +run apt-key adv --keyserver khkp://keyserver.ubuntu.com:80 --recv-keys 4F4EA0AAE5267A6C -echo "Update repository and install pre-requisites..." +echo "Updating repository and install pre-requisites..." # Update repos -apt-get update -y +run apt-get update -y # Install pre-requirements -apt-get install -y software-properties-common python-software-properties build-essential git unzip cron curl gnupg2 ca-certificates lsb-release rsync openssl snmp spawn-fcgi fcgiwrap geoip-database +run apt-get install -y software-properties-common python-software-properties build-essential git unzip cron curl gnupg2 ca-certificates lsb-release rsync openssl snmp spawn-fcgi fcgiwrap geoip-database +status "Adding repositories completed..." diff --git a/scripts/clean_server.sh b/scripts/clean_server.sh new file mode 100755 index 0000000..a7cb5f1 --- /dev/null +++ b/scripts/clean_server.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +# Remove Apache2 & mysql services if exist + +if [[ -n $(which apache2) ]]; then + echo "Uninstall existing Apache web server..." + killall -9 apache2 + service apache2 stop + apt-get --purge remove -y apache2 apache2-doc apache2-utils apache2.2-common apache2.2-bin apache2-mpm-prefork apache2-doc apache2-mpm-worker +fi + +if [[ -n $(which mysql) ]]; then + echo "Uninstall existing MySQL database server..." + killall -9 mysql + service mysqld stop + apt-get --purge remove -y mysql-client mysql-server mysql-common +fi + +apt-get autoremove -y diff --git a/scripts/cleanup_server.sh b/scripts/cleanup_server.sh new file mode 100755 index 0000000..d136069 --- /dev/null +++ b/scripts/cleanup_server.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +# Include decorator +if [ "$(type -t run)" != "function" ]; then + BASEDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd ) + . ${BASEDIR}/decorator.sh +fi + +# Remove Apache2 services if exist +if [[ -n $(which apache2) ]]; then + warning "It seems Apache web server installed on this machine. We should remove it!" + read -t 10 -p "Press [Enter] to continue..." &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 +} diff --git a/scripts/install_letsencrypt.sh b/scripts/install_letsencrypt.sh index e8baad5..d1ab189 100755 --- a/scripts/install_letsencrypt.sh +++ b/scripts/install_letsencrypt.sh @@ -5,25 +5,31 @@ # Last Build : 09/09/2017 # Author : MasEDI.Net (hi@masedi.net) +# Include decorator +if [ "$(type -t run)" != "function" ]; then + BASEDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd ) + . ${BASEDIR}/decorator.sh +fi + # Make sure only root can run this installer script if [ $(id -u) -ne 0 ]; then - echo "This script must be run as root..." - exit 0 + error "This script must be run as root..." + exit 1 fi clear echo "+=========================================================================+" -echo "+ Certbot Let's Encrypt Installer for Ubuntu VPS, Written by MasEDI.Net +" +echo "+ Certbot Let's Encrypt Installer for Ubuntu VPS, Written by ESLabs.ID +" echo "+=========================================================================+" echo "+ A small tool to install Certbot & Let's Enscrypt SSL certificate +" echo "+ +" -echo "+ For more information please visit http://masedi.net/tools/ +" +echo "+ For more information please visit https://ngxtools.eslabs.id +" echo "+=========================================================================+" sleep 1 -add-apt-repository ppa:certbot/certbot -apt-get update -apt-get install certbot +run add-apt-repository ppa:certbot/certbot +run apt-get update +run apt-get install certbot # Add this certbot renew command to cron #15 3 * * * /usr/bin/certbot renew --quiet --renew-hook "/bin/systemctl reload nginx" diff --git a/scripts/install_nginx.sh b/scripts/install_nginx.sh index 12e8ed9..141e8a3 100755 --- a/scripts/install_nginx.sh +++ b/scripts/install_nginx.sh @@ -1,53 +1,64 @@ #!/usr/bin/env bash +# Include decorator +if [ "$(type -t run)" != "function" ]; then + BASEDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd ) + . ${BASEDIR}/decorator.sh +fi + echo "Installing Nginx webserver..." -# Install Nginx custom -apt-get install -y --allow-unauthenticated nginx-stable +if [[ -n $(which nginx) && -d /etc/nginx/sites-available ]]; then + warning "Nginx web server already exists. Installation skipped..." +else + # Install Nginx custom + run apt-get install -y --allow-unauthenticated nginx-stable -# Copy custom Nginx Config -mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.old -cp -f nginx/nginx.conf /etc/nginx/ -cp -f nginx/fastcgi_cache /etc/nginx/ -cp -f nginx/fastcgi_https_map /etc/nginx/ -cp -f nginx/fastcgi_params /etc/nginx/ -cp -f nginx/http_cloudflare_ips /etc/nginx/ -cp -f nginx/http_proxy_ips /etc/nginx/ -cp -f nginx/proxy_cache /etc/nginx/ -cp -f nginx/proxy_params /etc/nginx/ -cp -f nginx/upstream.conf /etc/nginx/ -cp -fr nginx/conf.vhost/ /etc/nginx/ -cp -fr nginx/ssl/ /etc/nginx/ -mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.old -cp -f nginx/sites-available/default /etc/nginx/sites-available/ -cp -f nginx/sites-available/phpmyadmin.conf /etc/nginx/sites-available/ -cp -f nginx/sites-available/sample-wordpress.dev.conf /etc/nginx/sites-available/ -cp -f nginx/sites-available/sample-wordpress-ms.dev.conf /etc/nginx/sites-available/ -cp -f nginx/sites-available/ssl.sample-site.dev.conf /etc/nginx/sites-available/ -unlink /etc/nginx/sites-enabled/default -ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/01-default + # Copy custom Nginx Config + run mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.old + run cp -f nginx/nginx.conf /etc/nginx/ + run cp -f nginx/fastcgi_cache /etc/nginx/ + run cp -f nginx/fastcgi_https_map /etc/nginx/ + run cp -f nginx/fastcgi_params /etc/nginx/ + run cp -f nginx/http_cloudflare_ips /etc/nginx/ + run cp -f nginx/http_proxy_ips /etc/nginx/ + run cp -f nginx/proxy_cache /etc/nginx/ + run cp -f nginx/proxy_params /etc/nginx/ + run cp -f nginx/upstream.conf /etc/nginx/ + run cp -fr nginx/conf.vhost/ /etc/nginx/ + run cp -fr nginx/ssl/ /etc/nginx/ + run mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.old + run cp -f nginx/sites-available/default /etc/nginx/sites-available/ + run cp -f nginx/sites-available/phpmyadmin.conf /etc/nginx/sites-available/ + run cp -f nginx/sites-available/sample-wordpress.dev.conf /etc/nginx/sites-available/ + run cp -f nginx/sites-available/sample-wordpress-ms.dev.conf /etc/nginx/sites-available/ + run cp -f nginx/sites-available/ssl.sample-site.dev.conf /etc/nginx/sites-available/ + run unlink /etc/nginx/sites-enabled/default + run ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/01-default -# Nginx cache directory -if [ ! -d "/var/cache/nginx/" ]; then - mkdir /var/cache/nginx - chown -hR www-data: /var/cache/nginx + # Nginx cache directory + if [ ! -d "/var/cache/nginx/" ]; then + run mkdir /var/cache/nginx + run chown -hR www-data: /var/cache/nginx + fi + + if [ ! -d "/var/cache/nginx/fastcgi_cache" ]; then + run mkdir /var/cache/nginx/fastcgi_cache + run chown -hR www-data: /var/cache/nginx/fastcgi_cache + fi + + if [ ! -d "/var/cache/nginx/proxy_cache" ]; then + run mkdir /var/cache/nginx/proxy_cache + run chown -hR www-data: /var/cache/nginx/proxy_cache + fi + + # Check IP Address + IPAddr=$(curl -s http://ipecho.net/plain) + # Make default server accessible from IP address + run sed -i s@localhost.localdomain@$IPAddr@g /etc/nginx/sites-available/default + + # Restart Nginx server + if [[ $(ps -ef | grep -v grep | grep nginx | wc -l) > 0 ]]; then + run service nginx restart + fi fi -if [ ! -d "/var/cache/nginx/fastcgi_cache" ]; then - mkdir /var/cache/nginx/fastcgi_cache - chown -hR www-data: /var/cache/nginx/fastcgi_cache -fi -if [ ! -d "/var/cache/nginx/proxy_cache" ]; then - mkdir /var/cache/nginx/proxy_cache - chown -hR www-data: /var/cache/nginx/proxy_cache -fi - -# Check IP Address -IPAddr=$(curl -s http://ipecho.net/plain) -# Make default server accessible from IP address -sed -i "s@localhost.localdomain@$IPAddr@g" /etc/nginx/sites-available/default - -# Restart Nginx server -if [[ $(ps -ef | grep -v grep | grep nginx | wc -l) > 0 ]]; then - service nginx restart -fi -