#!/bin/bash install_nginx(){ echo -e "\e[33m>\e[0m nginx installer \e[33m<\e[0m\n" CNF_ROOT="/etc/nginx" # (1) Check config location # errcount="0"; ls $CNF_ROOT >/dev/null 2>&1; errcount="`expr $errcount + $?`"; ls $CNF_ROOT/sites-available >/dev/null 2>&1; errcount="`expr $errcount + $?`"; ls $CNF_ROOT/sites-enabled >/dev/null 2>&1; errcount="`expr $errcount + $?`"; if [ "$errcount" -gt 0 ]; then echo -e " \e[33m/!\\\\\e[0m Invalid configuration structure in $CNF_ROOT." echo -e " expected \e[33msites-available\e[0m and \e[33msites-enabled\e[0m mechanism." exit 1; fi # (3) Ask server name # read -p "[server name] (ptut.example.com): " server_name; if [ -z "$server_name" ]; then echo -e " \e[33m/!\\\\\e[0m Invalid server name." exit 1; fi; # (3) Copy config # echo "1- build configuration" echo """server { listen 80; server_name $server_name www.$server_name; location / { proxy_set_header Host \$host; proxy_pass http://127.0.0.1:$PORT; } }""" | sudo tee $CNF_ROOT/sites-available/ptut.reverse-proxy > /dev/null # manage error if [ "$?" -ne 0 ]; then echo -e " \e[33m/!\\\\\e[0m You must have 'sudo' permissions." exit 1; fi; # (4) Setup nginx # echo "2- enable configuration" sudo ln -fs ../sites-available/ptut.reverse-proxy $CNF_ROOT/sites-enabled/ptut.reverse-proxy; sudo systemctl reload nginx; echo -e "\e[33m>\e[0m done \e[33m<\e[0m" } install_httpd(){ echo -e "\e[33m>\e[0m httpd installer \e[33m<\e[0m\n" } install_apache(){ echo -e "\e[33m>\e[0m apache installer \e[33m<\e[0m\n" CNF_ROOT="/etc/apache2" # (1) Check config location # errcount="0"; ls $CNF_ROOT >/dev/null 2>&1; errcount="`expr $errcount + $?`"; ls $CNF_ROOT/sites-available >/dev/null 2>&1; errcount="`expr $errcount + $?`"; ls $CNF_ROOT/sites-enabled >/dev/null 2>&1; errcount="`expr $errcount + $?`"; if [ "$errcount" -gt 0 ]; then echo -e " \e[33m/!\\\\\e[0m Invalid configuration structure in $CNF_ROOT." echo -e " expected \e[33msites-available\e[0m and \e[33msites-enabled\e[0m mechanism." exit 1; fi # (3) Ask server name # read -p "[server name] (ptut.example.com): " server_name; if [ -z "$server_name" ]; then echo -e " \e[33m/!\\\\\e[0m Invalid server name." exit 1; fi; # (3) Copy config # echo "1- build configuration" echo """ ServerName $server_name ServerAlias www.$server_name ProxyPreserveHost On ProxyRequests off ProxyPass / http://127.0.0.1:$PORT/ ProxyPassReverse / http://127.0.0.1:$PORT/ """ | sudo tee $CNF_ROOT/sites-available/ptut.reverse-proxy > /dev/null # manage error if [ "$?" -ne 0 ]; then echo -e " \e[33m/!\\\\\e[0m You must have 'sudo' permissions." exit 1; fi; # (4) Setup nginx # echo "2- enable configuration" sudo ln -fs ../sites-available/ptut.reverse-proxy $CNF_ROOT/sites-enabled/ptut.reverse-proxy; sudo systemctl reload apache2; echo -e "\e[33m>\e[0m done \e[33m<\e[0m" } # (1) Set optional port #--------------------------------------------------------# # (1) Default value # PORT="8080"; # (2) If port given as argument[0] # if [ "$#" -ge 1 ]; then # fail if invalid number if [ "`expr $1 + 0 2>/dev/null`" != "$1" ]; then echo -e "\e[33m/!\\\\\e[0m Invalid port (expected a number)" exit 1 fi; PORT="`expr $1 + 0`" fi; # (2) Detect installed/running HTTP server #--------------------------------------------------------# # (1) Check for existing services # is_nginx="$(test `systemctl status nginx 2>/dev/null | wc -l` -gt 1 && echo 1 || echo 0)" is_httpd="$(test `systemctl status httpd 2>/dev/null | wc -l` -gt 1 && echo 1 || echo 0)" is_apache2="$(test `systemctl status apache2 2>/dev/null | wc -l` -gt 1 && echo 1 || echo 0)" candidate_n="`expr $is_nginx + $is_httpd + $is_apache2`" # (2) No candidate # if [ "$candidate_n" -lt 1 ]; then echo -e "\e[33m/!\\\\\e[0m You have no HTTP server installed.\n" echo -e "> install \e[33mnginx\e[0m or \e[33mapache/httpd\e[0m" exit 1 # (3) One candidate -> automatic select # elif [ "$candidate_n" -eq 1 ]; then test "$is_nginx" = "1" && install_nginx; test "$is_httpd" = "1" && install_httpd; test "$is_apache2" = "1" && install_apache2; # (4) Multiple choices # else echo "installation candidate:" test "$is_nginx" = "1" && echo -e " [\e[33mN\e[0m] nginx" test "$is_httpd" = "1" && echo -e " [\e[33mH\e[0m] httpd (apache)" test "$is_apache2" = "1" && echo -e " [\e[33mA\e[0m] apache2" read -p "> " -n1 candidate; echo case $candidate in "n"|"N") if [ "$is_nginx" != "1" ]; then echo -e "\e[33m/!\\\\\e[0m Invalid choice... aborting"; exit 1; fi; install_nginx ;; "h"|"H") if [ "$is_httpd" != "1" ]; then echo -e "\e[33m/!\\\\\e[0m Invalid choice... aborting"; exit 1; fi; install_httpd ;; "a"|"A") if [ "$is_apache2" != "1" ]; then echo -e "\e[33m/!\\\\\e[0m Invalid choice... aborting"; exit 1; fi; install_apache ;; *) echo -e "\e[33m/!\\\\\e[0m Invalid choice... aborting"; exit 1;; esac; fi;