webinoly/plugins/site
Cristhian Martínez Ochoa 5869e4efbe site list improved
Now list include parked and reverse proxy sites.
2018-11-02 15:21:18 -07:00

379 lines
14 KiB
Bash

#!/bin/bash
# Site Manager Plugin (Create, delete and de/activate)
# Syntax: site <domain> <option> <argument>
# Options: -html, -php, -mysql, -wp, -wpsubdir, -wpsubdom, -parked, -proxy, -on, -off, -delete, -delete-all, -list, -nocache, -cache, -ssl-on, ssl-off, -force-redirect
# Arguments: -cache, -root, -root-path, -subdomain, -ignore-ssl, -wildcard
# shopt is necessary for this kind !(html|22222) of patterns
shopt -s extglob
domain=$1
type=$2
cache=$3
source /opt/webinoly/lib/general
source /opt/webinoly/lib/sites
source /opt/webinoly/lib/site-ssl
if [[ ! $(conf_read nginx) == "true" ]]; then
echo ""
echo "${red} NGINX is required to create a site! ${end}"
echo ""
exit 1
fi
# Check if user entered arg before option and correct it.
if [[ -n $cache && $type =~ ^(-cache|-root|-root-path|-subdomain|-ignore-ssl|-wildcard)$ ]]; then
type=$3
cache=$2
fi
# Extract value if exist
if [[ $domain == "-mysql="* ]]; then
value=$(echo "${domain}" | cut -d'=' -f 2 -s)
domain=$(echo "${domain}" | cut -d'=' -f 1 -s)
fi
if [[ $type == "-parked="* || $type == "-proxy="* || $type == "-wp="* || $type == "-wpsubdir="* || $type == "-wpsubdom="* || $type == "-ssl-off="* || $type == "-mysql="* || $type == "-delete="* || $type == "-force-redirect="* ]]; then
value=$(echo "${type}" | cut -d'=' -f 2 -s)
type=$(echo "${type}" | cut -d'=' -f 1 -s)
fi
if [[ $cache == "-root="* || $cache == "-subdomain="* || $cache == "-root-path="* ]]; then
value=$(echo "${cache}" | cut -d'=' -f 2 -s)
cache=$(echo "${cache}" | cut -d'=' -f 1 -s)
fi
# Check if site is sub-domain
if [[ -n $domain && $type =~ ^(-html|-php|-mysql|-wp|-wpsubdir|-wpsubdom|-parked|-proxy|-ssl-on|-force-redirect)$ ]]; then
count=1
while true; do
tld=$(echo "${domain}" | rev | cut -d'.' -f -$count -s | rev)
if ! grep -Fxq "$tld" /opt/webinoly/lib/public_suffix_list.dat; then
break
fi
count=$[$count+1]
done
[[ -n $(echo "$(echo "${domain}" | rev | cut -d'.' -f $count- -s | rev)" | cut -d'.' -f 2 -s) || $count == 1 ]] && subdomflag="1" || subdomflag="0"
fi
# Overwrite value if defined by user
[[ $cache == "-subdomain" && $value == "true" ]] && subdomflag="1"
[[ $cache == "-subdomain" && $value == "false" ]] && subdomflag="0"
# Evaluate if site to create is WP ( 0 = noWP, 1 = WP, 2 = WP with default values, 3 = WP with custom values, 4 = MySQL with default values, 5 = MySQL with custom values )
wp="0"
if [[ $type == "-wp" || $type == "-wpsubdir" || $type == "-wpsubdom" || $type == "-mysql" || $domain == "-mysql" ]]; then
[[ $type == "-mysql" || $domain == "-mysql" ]] || wp="1"
if [[ -n $value ]]; then
if [[ $value == "default" ]]; then
[[ $type == "-mysql" || $domain == "-mysql" ]] && wp="4" || wp="2"
else
# -wp=[setupmysql,setupwp,dbhost,dbname,dbuser,dbpass,dbpref,extdbuser,extdbpass]
custombegin=$(echo "${value}" | cut -c-1)
customlast=$(echo "${value}" | rev | cut -c-1)
wpdata=${value:1:-1}
# Because MySQL format not have this parameters at the beggining to match WP format. Insert it!
if [[ $type == "-mysql" || $domain == "-mysql" ]]; then
wpdata=${wpdata/#/true,false,}
# Insert wp_prefix if external db data is found.
extdata=$(echo "${wpdata,,}" | cut -d',' -f 7- -s)
if [[ -n $extdata ]]; then
wpdata=$(echo "${wpdata,,}" | cut -d',' -f -6 -s)
wpdata=${wpdata/%/,false,}
wpdata=${wpdata/%/$extdata}
fi
fi
# Double coma is for lower case convertion
setmsqcustom=$(echo "${wpdata,,}" | cut -d',' -f 1 -s)
setpwpcustom=$(echo "${wpdata,,}" | cut -d',' -f 2 -s)
dbhostcustom=$(echo "${wpdata}" | cut -d',' -f 3 -s)
dbnamecustom=$(echo "${wpdata}" | cut -d',' -f 4 -s)
dbusercustom=$(echo "${wpdata}" | cut -d',' -f 5 -s)
dbpasscustom=$(echo "${wpdata}" | cut -d',' -f 6 -s)
dbprefcustom=$(echo "${wpdata}" | cut -d',' -f 7 -s)
exusercustom=$(echo "${wpdata}" | cut -d',' -f 8 -s)
expasscustom=$(echo "${wpdata}" | cut -d',' -f 9 -s)
if [[ $custombegin == "[" && $customlast == "]" && ((($setmsqcustom == "true" || $setpwpcustom == "true") && -n $dbhostcustom && -n $dbnamecustom && -n $dbusercustom && -n $dbpasscustom && (-n $dbprefcustom || $type == "-mysql" || $domain == "-mysql")) || ($setmsqcustom == "false" && $setpwpcustom == "false")) ]]; then
[[ $type == "-mysql" || $domain == "-mysql" ]] && wp="5" || wp="3"
fi
fi
fi
fi
# Cache validation
if [[ $cache == "-cache" && $wp == [045] ]]; then
echo "${red}Site${blu} $domain ${red}is not a WP site! ${end}"
exit 1
elif [[ -n $cache && ( $cache != "-cache" && $cache != "-root" && $cache != "-root-path" && $cache != "-subdomain" && $cache != "-ignore-ssl" && $cache != "-wildcard" ) && $type != "-parked" ]]; then
echo "${red} $cache is not a valid argument! ${end}"
exit 1
fi
# List Sites
if [[ "$domain" == "-list" && -z "$type" && -z "$cache" ]]; then
echo ""
for site in "/etc/nginx/sites-available"/*
do
domi=$(echo $site | cut -f 5 -d "/")
[[ -a /var/www/$domi ]] && sign="${gre} -" || sign="${blu} *${gre}"
[[ $domi != "default" && $domi != $(conf_read tools-port) ]] && echo "$sign $domi ${end}"
done
echo ""
# Delete all sites
elif [[ "$domain" == "-delete-all" && -z "$type" && -z "$cache" ]]; then
echo "${red}"
echo "All your sites files will be removed."
echo "This action will only delete Databases attached to a WordPress site, any other Database will not be deleted."
echo "${end}"
# List all sites in /var/www/ folder
for site in "/var/www"/*
do
domi=$(echo $site | cut -f 4 -d "/")
# List sites then will be deleted
if [[ $domi != "html" && $domi != $(conf_read tools-port) ]]; then
# Determina if site is WP (so has DB to delete)
[[ -a $site/wp-config.php || -a $site/htdocs/wp-config.php ]] && db_delete $domi
sudo rm -rf /etc/nginx/sites-available/$domi
sudo rm -rf /etc/nginx/sites-enabled/$domi
sudo rm -rf /var/www/$domi
echo "${gre}Your site ${blu}${domi}${gre} has been successfully deleted! ${end}"
fi
done
# List all remaining sites (parked and proxy)
for site in "/etc/nginx/sites-available"/*
do
domi=$(echo $site | cut -f 5 -d "/")
if [[ $domi != "html" && $domi != "default" && $domi != $(conf_read tools-port) ]]; then
sudo rm -rf /etc/nginx/sites-available/$domi
sudo rm -rf /etc/nginx/sites-enabled/$domi
echo "${gre}Your site ${blu}${domi}${gre} has been successfully deleted! ${end}"
fi
done
# Delete all files - double check!
# -rf is necessary to not generate an error when is empty.
sudo rm -rf /etc/nginx/sites-available/!(default|$(conf_read tools-port))
sudo rm -rf /etc/nginx/sites-enabled/!(default|$(conf_read tools-port))
sudo rm -rf /var/www/!(html|$(conf_read tools-port))
echo ""
echo "${gre}All sites and data has been deleted successfully!${end}"
# Create PHP site
elif [ "$type" == "-php" ]; then
createsite
# Create MYSQL site
elif [ "$type" == "-mysql" ]; then
# We use WP function to create DB when wp=0 WP files are not installed.
createsite
wpinstall
elif [[ "$domain" == "-mysql" && -z "$type" && -z "$cache" ]]; then
# Hack to create a DB easily (without site).
wpinstall
# Create HTML site
elif [ "$type" == "-html" ]; then
createsite
sudo sed -i '/index/c \ index index.html index.htm;' /etc/nginx/sites-available/$domain
sudo sed -i '/index/a \ location / { try_files $uri $uri/ =404; }' /etc/nginx/sites-available/$domain
sudo sed -i '/php.conf/d' /etc/nginx/sites-available/$domain
# Create WP site
elif [ "$type" == "-wp" ]; then
createsite
sudo sed -i '/locations.conf/i \ include common/wpcommon.conf;' /etc/nginx/sites-available/$domain
# Create WP Multisite (Sub-directory)
elif [ "$type" == "-wpsubdir" ]; then
createsite
sudo sed -i '/locations.conf/i \ include common/wpsubdir.conf;' /etc/nginx/sites-available/$domain
sudo sed -i '/locations.conf/i \ include common/wpcommon.conf;' /etc/nginx/sites-available/$domain
# Create WP Multisite (Sub-domain)
elif [ "$type" == "-wpsubdom" ]; then
createsite
sudo sed -i '/locations.conf/i \ include common/wpcommon.conf;' /etc/nginx/sites-available/$domain
# Parked Domain
elif [ "$type" == "-parked" ]; then
createsite
mapto="NeverMatchAtFirstDotCom"
[[ -n $value ]] && mapto="$value"
while [[ ! -a /etc/nginx/sites-available/$mapto ]]
do
echo "${blu}"
read -p "Main site domain: " mapto
mapto=${mapto:-NeverMatchAtFirstDotCom}
echo "${end}"
[[ ! -a /etc/nginx/sites-available/$mapto ]] && echo "${red} Main site domain not found! ${end}"
done
# Nginx conf file for the new parked domain
sername=$(grep -w "server_name .*;" /etc/nginx/sites-available/$domain)
sudo truncate -s 0 /etc/nginx/sites-available/$domain
sudo cat /etc/nginx/sites-available/$mapto > /etc/nginx/sites-available/$domain
# Prevent if SSL is enabled in main site and ssl data is copied to parked site causing an error.
sudo site $domain -ssl-off=force > /dev/null 2>&1 &
sudo sed -i "/server_name/c \ $sername" /etc/nginx/sites-available/$domain
sudo sed -i "s/$mapto/$domain/g" /etc/nginx/sites-available/$domain
sudo sed -i "/root/c \ root /var/www/$mapto/htdocs;" /etc/nginx/sites-available/$domain
cusconl=$( grep -F "include /var/www/$domain/" /etc/nginx/sites-available/$domain | sed "s/$domain/$mapto/" )
sed -i "/include \/var\/www\/$domain/c \ ${cusconl}" /etc/nginx/sites-available/$domain
echo "${gre}"
echo "Parked domain was successfully configured! "
echo "${end}"
# Reverse proxy
elif [[ "$type" == "-proxy" ]]; then
if [[ -n $value ]]; then
firstchar=$(echo "${value}" | cut -c-1)
lastchar=$(echo "${value}" | rev | cut -c-1)
proxydata=${value:1:-1}
rphost=$(echo "${proxydata}" | cut -d':' -f 1 -s)
rpport=$(echo "${proxydata}" | cut -d':' -f 2 -s)
if ! [[ -n $proxydata && $firstchar == "[" && $lastchar == "]" && -n $rphost && -n $rpport && $rpport =~ ^[0-9]+$ && $rpport -ge 0 && $rpport -le 65535 ]]; then
echo "${red}[ERROR] Please, enter a valid host and port!${end}"
exit 1
fi
fi
createsite
sudo sed -i '/include /d' /etc/nginx/sites-available/$domain
sudo sed -i '/root /d' /etc/nginx/sites-available/$domain
sudo sed -i '/index /d' /etc/nginx/sites-available/$domain
sudo sed -i '/error_log /r /opt/webinoly/templates/template-site-proxy' /etc/nginx/sites-available/$domain
echo "${gre}"
if [[ -n $rphost ]]; then
sudo sed -i "s/localhost/$rphost/g" /etc/nginx/sites-available/$domain
echo " Custom host found: $rphost"
else
echo " Default host: localhost"
fi
if [[ -n $rpport ]]; then
sudo sed -i "s/8080/$rpport/g" /etc/nginx/sites-available/$domain
echo " Custom port found: $rpport"
else
echo " Default port: 8080"
fi
# Check host and port
code=$(wget -t 1 --timeout=5 --server-response --spider ${rphost}:${rpport} 2>&1 | awk '/^ HTTP/{print $2}')
code="${code##*$'\n'}"
echo "${red}"
[[ $code != 200 ]] && echo "[WARNING] Seems like your host/port is not responding!"
echo "${end}"
# Site disabled
elif [[ "$type" == "-off" && ! -L /etc/nginx/sites-enabled/$domain ]]; then
echo "${red}Site${blu} $domain ${red}doesn't exist or is already disabled!${end}"
elif [[ "$type" == "-off" && -L /etc/nginx/sites-enabled/$domain ]]; then
echo "${gre}Site${blu} $domain ${gre}has been successfully disabled!${end}"
sudo rm /etc/nginx/sites-enabled/$domain
# Site re-enabled
elif [[ "$type" == "-on" && -L /etc/nginx/sites-enabled/$domain ]]; then
echo "${gre}Site${blu} $domain ${gre}is already enabled!${end}"
elif [[ "$type" == "-on" && ! -L /etc/nginx/sites-enabled/$domain && -a /etc/nginx/sites-available/$domain ]]; then
sudo ln -s /etc/nginx/sites-available/$domain /etc/nginx/sites-enabled/$domain
echo "${gre}Site${blu} $domain ${gre}has been successfully enabled!${end}"
# Delete site
elif [[ "$type" == "-delete" && -a /etc/nginx/sites-available/$domain ]]; then
deletesite
echo "${gre}Site${blu} $domain ${gre}has been successfully deleted!${end}"
# SSL enabled (Letsencrypt)
elif [[ "$type" == "-ssl-on" && -a /etc/nginx/sites-available/$domain ]]; then
isssl=$( grep -F "ssl on;" /etc/nginx/sites-available/$domain )
[[ -z $isssl ]] && site_ssl_on || echo "${red}SSL is already enabled for your site -${blu} $domain ${end}"
# SSL disabled (Letsencrypt)
elif [[ "$type" == "-ssl-off" && -a /etc/nginx/sites-available/$domain ]]; then
isssl=$( grep -F "ssl on;" /etc/nginx/sites-available/$domain )
[[ -n $isssl ]] && site_ssl_off || echo "${red}SSL is already disabled for your site -${blu} $domain ${end}"
# FastCGI Cache disabled
elif [[ "$type" == "-nocache" && -a /etc/nginx/sites-available/$domain ]]; then
isfc=$( grep -F "wpfc.conf" /etc/nginx/sites-available/$domain )
if [[ -n $isfc ]]; then
sudo sed -i '/wpfc.conf/c \ include common/php.conf;' /etc/nginx/sites-available/$domain
echo "${gre}FastCGI Cache in${blu} $domain ${gre}has been disabled!${end}"
else
echo "${red}Site${blu} $domain ${red}is not a WP site or FastCGI were not enabled!${end}"
fi
# FastCGI Cache enabled
elif [[ "$type" == "-cache" && -a /etc/nginx/sites-available/$domain ]]; then
isphp=$( grep -F "php.conf" /etc/nginx/sites-available/$domain )
iswp=$( grep -F "wpcommon.conf" /etc/nginx/sites-available/$domain )
if [[ -n $isphp && -n $iswp ]]; then
sudo sed -i '/php.conf/c \ include common/wpfc.conf;' /etc/nginx/sites-available/$domain
if [[ ! -d /var/www/$domain/htdocs/wp-content/plugins/nginx-helper || ! -d /var/www/$domain/htdocs/wp-content/plugins/redis-cache ]]; then
echo "${gre}"
echo "We recommend Nginx Helper Plugin and Redis Object Cache Plugin as an easy way to manage FastCGI and Redis Cache on your WordPress site."
echo "${blu}Do you want to install it now [Y/n]? ${end}"
while read -r -n 1 -s answer; do
answer=${answer:-y}
if [[ $answer = [YyNn] ]]; then
[[ $answer = [Yy] ]] && wp_cache_plugins
break
fi
done
fi
echo "${gre}"
echo " FastCGI Cache in${blu} $domain ${gre}has been successfully enabled! "
echo "${end}"
else
echo "${red}Site${blu} $domain ${red}is not a WP site or FastCGI is already enabled!${end}"
fi
# Force www or non-www redirect (www, root, off)
elif [[ "$type" == "-force-redirect" && -a /etc/nginx/sites-available/$domain ]]; then
force_redirect
# Catch ERROR
elif [[ ! -a /etc/nginx/sites-available/$domain && $type =~ ^(-nocache|-cache|-delete|-off|-on|-ssl-on|-ssl-off|-force-redirect)$ ]]; then
echo "${red}Site${blu} $domain ${red}doesn't exists!${end}"
else
[[ -z $type ]] && type=$domain
echo "${red} Argument '${type}' is not a valid option! ${end}"
fi
shopt -u extglob
[[ $(conf_read nginx) == "true" ]] && sudo service nginx reload