webinoly/plugins/site

666 lines
28 KiB
Text
Raw Normal View History

#!/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, -cache, -ssl, -force-redirect, -yoast-sitemap, -multisite-convert
# Arguments: -cache, -root, -root-path, -subdomain, -ignore-ssl, -wildcard, -raw, -external-db, -revoke, -ssl-crt, -ssl-key, -ssl-ocsp, -subfolder
# shopt is necessary for this kind !(html|22222) of patterns
shopt -s extglob
source /opt/webinoly/lib/sites
check_for_parameters $@
api-events_update sis
check_for_nginx -ask
# Check if site is sub-domain
if [[ -n $domain ]]; 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
# Get variables with sub-domain info
if [[ $subdomflag == 1 ]]; then
main_domain=$(echo "${domain}" | rev | cut -d'.' -f -$count -s | rev)
sub_domain=$(echo "${domain}" | rev | cut -d'.' -f $[$count+1]- -s | rev)
fi
# Overwrite value if defined by user
[[ $subdomain == "true" ]] && subdomflag="1"
[[ $subdomain == "false" ]] && subdomflag="0"
# Evaluate if site to create is WP ( 0 = noWP, 1 = WP ask, 2 = WP default, 3 = WP unattended, 4 = MySQL default, 5 = MySQL unattended )
type="0"
if [[ -n $wp || -n $wpsubdir || -n $wpsubdom || -n $mysql ]]; then
[[ -n $mysql ]] && type="4" || type="2"
# For legacy support
[[ $wp == "default" ]] && wp=true
[[ $wpsubdir == "default" ]] && wpsubdir=true
[[ $wpsubdom == "default" ]] && wpsubdom=true
[[ $mysql == "default" ]] && mysql=true
if [[ ( -n $wp && $wp != true ) || ( -n $wpsubdir && $wpsubdir != true ) || ( -n $wpsubdom && $wpsubdom != true ) || ( -n $mysql && $mysql != true ) ]]; then
if [[ $wp == "custom" || $wpsubdir == "custom" || $wpsubdom == "custom" || $mysql == "custom" ]]; then
[[ -n $mysql ]] && type="0" || type="1"
else
[[ -n $wp && $wp != true ]] && value=$wp
[[ -n $wpsubdir && $wpsubdir != true ]] && value=$wpsubdir
[[ -n $wpsubdom && $wpsubdom != true ]] && value=$wpsubdom
[[ -n $mysql && $mysql != true ]] && value=$mysql
# -wp=[setupmysql,setupwp,dbhost,dbname,dbuser,dbpass,dbpref,extdbuser,extdbpass]
custombegin=$(echo "${value}" | cut -c-1)
customlast=$(echo "${value}" | rev | cut -c-1)
if [[ $custombegin == "[" && $customlast == "]" || $mysql == true ]]; then
wpdata=${value:1:-1}
else
echo "${red}[ERROR] Please enter a valid custom value!${end}"
exit 1
fi
# Because MySQL format not have this parameters at the beggining to match WP format. Insert it!
if [[ -n $mysql && $mysql != true ]]; 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)
[[ ${dbpasscustom,,} == "random" ]] && dbpasscustom=`pwgen -s -1 16`
# Check if we have ALL the parameters.
if [[ $mysql != true && ( -z $setmsqcustom || -z $setpwpcustom || -z $dbhostcustom || -z $dbnamecustom || -z $dbusercustom || -z $dbpasscustom || ( -z $dbprefcustom && -z $mysql && -z $extdata ) || ( -n $exusercustom && -z $expasscustom )) ]]; then
echo "${red}[ERROR] Missing parameters!${end}"
exit 1
fi
if [[ $custombegin == "[" && $customlast == "]" && ((($setmsqcustom == "true" || $setpwpcustom == "true") && -n $dbhostcustom && -n $dbnamecustom && -n $dbusercustom && -n $dbpasscustom && (-n $dbprefcustom || ( -n $mysql && $mysql != true ))) || ($setmsqcustom == "false" && $setpwpcustom == "false")) ]]; then
[[ -n $mysql && $mysql != true ]] && type="5" || type="3"
fi
fi
fi
fi
# Check for valid WP subfolder
[[ -z $subfolder ]] && subfolder=""
if [[ -n $subfolder && ! ( $subfolder =~ ^\/([A-Za-z0-9_\/\-]+)?[^\/]$ ) ]]; then
echo "${red}[ERROR] Please, enter a valid value for subfolder!${end}"
exit 1
fi
if [[ -n $subfolder && $type != [123] && -z $delete && -z $cache && -z $yoast_sitemap && -z $multisite_convert ]]; then
echo "${red}[ERROR] Subfolder option is only supported for WordPress sites!${end}"
exit 1
else
subname=$(echo $subfolder | sed "s/\//_/g")
fi
# Check for stack packages
[[ $type == [123] || -n $php ]] && check_for_php -ask
[[ $type == [24] ]] && check_for_mysql -ask
# List Sites
if [[ -n $list ]]; then
echo ""
for site in "/etc/nginx/sites-available"/*
do
domi=$(echo $site | cut -f 5 -d "/")
if [[ $domi != "default" && $domi != $(conf_read tools-port) ]]; then
[[ -a /var/www/$domi ]] && sign="${gre} +" || sign="${blu} *${gre}"
[[ ! -L /etc/nginx/sites-enabled/$domi ]] && sign="${red} -"
outlist="$sign $domi ${end}"
if [[ $list == "raw" || $raw =~ ^(all|parked|disabled|main|true)$ ]]; then
outlist="$domi"
[[ $raw == "parked" && -a /var/www/$domi ]] && outlist=""
[[ $raw == "disabled" && -L /etc/nginx/sites-enabled/$domi ]] && outlist=""
[[ $raw == "main" && ( ! -L /etc/nginx/sites-enabled/$domi || ! -a /var/www/$domi ) ]] && outlist=""
elif [[ -n $raw ]]; then
echo "${red}[ERROR] Please, enter a valid value for RAW!${end}"
exit 1
fi
if [[ -n $outlist ]]; then
echo "$outlist"
nonemptylist=true
fi
fi
done
[[ -z $nonemptylist && -z $raw && $list != "raw" ]] && echo "${blu}[Empty] No sites were found!${end}"
echo ""
nginx_not="true" # Nginx-Reload not-needed.
# Delete all sites
elif [[ -n $delete_all ]]; then
api-events_update si7
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."
if ! [[ $delete_all =~ ^(force|keep-db)$ ]]; then
echo "${blu}"
echo "Are you sure [y/N]? ${end}"
while read -r -n 1 -s answer; do
answer=${answer:-n}
echo ""
[[ $answer = [YyNn] ]] && break
done
if [[ $answer == [Nn] ]]; then
echo "${red}Action aborted!${end}"
exit 1
fi
fi
# List all remaining sites (parked and proxy)
for site in "/etc/nginx/sites-available"/*
do
[[ $delete_all == "keep-db" ]] && delete="keep-db" || delete="force"
domain=$(echo $site | cut -f 5 -d "/")
[[ $domain != "default" && $domain != $(conf_read tools-port) ]] && deletesite
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 [[ -n $php && -n $domain ]]; then
createsite
# Create MYSQL site
elif [[ -n $mysql && -n $domain ]]; then
# We use WP function to create DB when type=0 WP files are not installed.
createsite
wpinstall
elif [[ -n $mysql && -z $domain ]]; then
# Hack to create a DB easily (without site).
wpinstall
# Create HTML site
elif [[ -n $html && -n $domain ]]; 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 [[ -n $wp && -n $domain ]]; then
if [[ -z $subfolder ]]; then
createsite
sudo sed -i '/locations.conf/i \ include common/wpcommon.conf;' /etc/nginx/sites-available/$domain
[[ $(conf_read wp-admin-auth) == "purged" ]] && sudo httpauth $domain -wp-admin=off > /dev/null 2>&1
[[ $(conf_read yoast-sitemap) != "purged" ]] && sudo site $domain -yoast-sitemap=on > /dev/null 2>&1
else
if [[ -a /etc/nginx/sites-available/$domain && -n $subfolder && $type == [123] && -d /var/www/$domain/htdocs$subfolder ]]; then
echo "${red}[ERROR] Subfolder ${blu}'$subfolder'${red} already exists!${end}"
exit 1
elif [[ -a /etc/nginx/sites-available/$domain && -n $subfolder && $type == [123] && ! -d /var/www/$domain/htdocs$subfolder ]]; then
wpinstall
else
createsite
fi
sudo sed -i "/# WebinolyCustom$/a \ include apps.d/$domain$subname-php.conf;" /etc/nginx/sites-available/$domain
sudo cp /etc/nginx/common/php.conf /etc/nginx/apps.d/$domain$subname-php.conf
sudo cp /etc/nginx/common/wpfc.conf /etc/nginx/apps.d/$domain$subname-wpfc.conf
sudo cp /etc/nginx/common/wpcommon.conf /etc/nginx/apps.d/$domain$subname-wpcommon.conf
sudo cp /etc/nginx/common/wpcommon-noauth.conf /etc/nginx/apps.d/$domain$subname-wpcommon-noauth.conf
sudo cp /etc/nginx/common/locations.conf /etc/nginx/apps.d/$domain$subname-locations.conf
sudo cp /etc/nginx/common/yoast-sitemap.conf /etc/nginx/apps.d/$domain$subname-yoast-sitemap.conf
sudo sed -i "/^location \/ {/,/^}$/{/.*/d}" /etc/nginx/apps.d/$domain$subname-php.conf
sudo sed -i "/^location \/ {/,/^}$/{/.*/d}" /etc/nginx/apps.d/$domain$subname-wpfc.conf
sudo sed -i '/DO NOT MODIFY,/r /opt/webinoly/templates/template-site-wpsubfolder' /etc/nginx/apps.d/$domain$subname-php.conf
sudo cat /opt/webinoly/templates/template-site-wpsubfolder >> /etc/nginx/apps.d/$domain$subname-wpfc.conf
sudo sed -i "s#subfolder#$subfolder#g" /etc/nginx/apps.d/$domain$subname-php.conf
sudo sed -i "s#subfolder#$subfolder#g" /etc/nginx/apps.d/$domain$subname-wpfc.conf
sudo sed -i "s/domain/$domain/g" /etc/nginx/apps.d/$domain$subname-php.conf
sudo sed -i "s/domain/$domain/g" /etc/nginx/apps.d/$domain$subname-wpfc.conf
sudo sed -i "s/subname/$subname/g" /etc/nginx/apps.d/$domain$subname-php.conf
sudo sed -i "s/subname/$subname/g" /etc/nginx/apps.d/$domain$subname-wpfc.conf
sudo sed -i "s#/index.php#$subfolder/index.php#g" /etc/nginx/apps.d/$domain$subname-yoast-sitemap.conf
sed -ri "s/^location(.*)\/([a-z].*)$/location\1subfolder\/\2/" /etc/nginx/apps.d/$domain$subname-locations.conf
sed -ri "s/location(.*) \/(.*) \{$/location\1 subfolder\/\2 \{/" /etc/nginx/apps.d/$domain$subname-wpcommon.conf
sed -ri "s/location(.*) \/(.*) \{$/location\1 subfolder\/\2 \{/" /etc/nginx/apps.d/$domain$subname-wpcommon-noauth.conf
sudo sed -i "s#subfolder#$subfolder#g" /etc/nginx/apps.d/$domain$subname-locations.conf
sudo sed -i "s#subfolder#$subfolder#g" /etc/nginx/apps.d/$domain$subname-wpcommon.conf
sudo sed -i "s#subfolder#$subfolder#g" /etc/nginx/apps.d/$domain$subname-wpcommon-noauth.conf
[[ $(conf_read wp-admin-auth) == "purged" ]] && sudo httpauth $domain -wp-admin=off -subfolder=$subfolder > /dev/null 2>&1
[[ $(conf_read yoast-sitemap) != "purged" ]] && sudo site $domain -yoast-sitemap=on -subfolder=$subfolder > /dev/null 2>&1
# Activate FastCgi cache
if [[ $cache == "on" ]]; then
sudo sed -i "/$domain$subname-php.conf;/c \ include apps.d/$domain$subname-wpfc.conf;" /etc/nginx/sites-available/$domain
wp_cache_plugins
fi
sudo chown -R www-data:www-data /var/www
[[ $(conf_read login-www-data) == "true" ]] && sudo chown root:root /var/www
echo "${gre}WordPress has been successfully installed in${blu} $domain$subfolder ${gre}subfolder!${end}"
fi
# Convert WP single to Multisite
elif [[ -n $multisite_convert && -a /etc/nginx/sites-available/$domain ]]; then
if [[ ( -z $subfolder && $(is_wp $domain) == "false" ) || ( -n $subfolder && $(is_wp $domain $subfolder) == "false" ) ]]; then
echo "${red}[ERROR] Site${blu} $domain$subfolder ${red}is not a WP site!${end}"
exit 1
fi
check=$(is_wp_multisite $domain)
if ! [[ $check =~ ^(subdomain|subdirectory|false)$ ]]; then
echo "${red}[ERROR] Task aborted due to an error!${end}"
exit 1
elif [[ $check =~ ^(subdomain|subdirectory)$ ]]; then
echo "${red}[ERROR] Site${blu} $domain$subfolder ${red}is already in multisite configuration!${end}"
exit 1
fi
[[ -z $subfolder ]] && wpcon="/var/www/$domain/wp-config.php" || wpcon="/var/www/$domain/htdocs$subfolder/wp-config.php"
sudo sed -i "/stop editing/i \define('WP_ALLOW_MULTISITE', true);" $wpcon
echo "${blu}"
echo "In your WordPress dashboard, go to ${end}“Tools -> Network Setup”${blu},"
echo "select the type of WP Multisite installation you want (Subdomain or Subdirectory)"
echo "and then press the ${end}“Install”${blu} button at the bottom."
echo ""
echo "After that, you don't need to add any code lines."
echo "We will do it for you!"
echo ""
echo "${gre}Waiting... we will automatically finish the multisite configuration after you do the install in the WP dashboard!${blu}"
echo "Write ${end}'quit'${blu} and press ${end}<Enter>${blu} to exit and abort this operation."
echo "${end}"
while [[ -z $passed || $passed == "false" ]]; do
if read -t0; then
read pas
[[ $pas == "quit" ]] && break
fi
sleep 2
passed=$(is_wp_multisite $domain)
done
if [[ $passed == "false" && $pas == "quit" ]]; then
sudo sed -i "/WP_ALLOW_MULTISITE/d" $wpcon
echo "${red}[ERROR] Action aborted!${end}"
exit 1
elif [[ $passed =~ ^(subdomain|subdirectory)$ ]]; then
# Multisite wp-config
sudo sed -i "/stop editing/i \define('MULTISITE', true);" $wpcon
sudo sed -i "/stop editing/i \define('DOMAIN_CURRENT_SITE', '$domain');" $wpcon
sudo sed -i "/stop editing/i \define('PATH_CURRENT_SITE', '$subfolder/');" $wpcon
sudo sed -i "/stop editing/i \define('SITE_ID_CURRENT_SITE', 1);" $wpcon
sudo sed -i "/stop editing/i \define('BLOG_ID_CURRENT_SITE', 1);" $wpcon
[[ $pas == "quit" ]] && echo "${red}Can not cancel because the installation was already done!${end}"
if [[ $passed == "subdomain" ]]; then
sudo sed -i "/stop editing/i \define('SUBDOMAIN_INSTALL', true);" $wpcon
sudo sed -i "/server_name /c \ server_name $domain *.$domain;" /etc/nginx/sites-available/$domain
echo "${gre}Subdomain Multisite configuration has been successfully completed!${end}"
elif [[ $passed == "subdirectory" ]]; then
sudo sed -i "/stop editing/i \define('SUBDOMAIN_INSTALL', false);" $wpcon
if [[ -z $subfolder ]]; then
sudo sed -i '/locations.conf/i \ include common/wpsubdir.conf;' /etc/nginx/sites-available/$domain
else
sudo cp /etc/nginx/common/wpsubdir.conf /etc/nginx/apps.d/$domain$subname-wpsubdir.conf
sudo sed -i "/DO NOT MODIFY/a \include apps.d\/$domain$subname-wpsubdir.conf;" /etc/nginx/apps.d/$domain$subname-php.conf
sudo sed -i "/DO NOT MODIFY/a \include apps.d\/$domain$subname-wpsubdir.conf;" /etc/nginx/apps.d/$domain$subname-wpfc.conf
sed -i "s#\^(#\^$subfolder\(#" /etc/nginx/apps.d/$domain$subname-wpsubdir.conf
sed -i "s#\$2#$subfolder\$2#" /etc/nginx/apps.d/$domain$subname-wpsubdir.conf
fi
echo "${gre}Subdirectory Multisite configuration has been successfully completed!${end}"
fi
else
sudo sed -i "/WP_ALLOW_MULTISITE/d" $wpcon
echo "${red}[ERROR] Multisite installation incomplete or not detected!${end}"
exit 1
fi
# Parked Domain
elif [[ -n $parked && -n $domain ]]; then
createsite
[[ $parked != true ]] && mapto=$parked || mapto="NeverMatchAtFirstDotCom"
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 -revoke=off > /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 [[ -n $proxy && -n $domain ]]; then
if [[ $proxy != true ]]; then
firstchar=$(echo "${proxy}" | cut -c-1)
lastchar=$(echo "${proxy}" | rev | cut -c-1)
proxydata=${proxy:1:-1}
rpport=$(echo "${proxydata}" | rev | cut -d':' -f 1 -s | rev)
if [[ $rpport =~ ^[0-9]+$ && $rpport -ge 0 && $rpport -le 65535 ]]; then
rphost=$(echo "${proxydata}" | rev | cut -d':' -f 2- -s | rev)
elif [[ $rpport =~ ^[0-9]+$ ]]; then
echo "${red}[ERROR] Please, enter a valid port number!${end}"
exit 1
else
rphost=$proxydata
rpport=""
fi
if ! [[ -n $proxydata && $firstchar == "[" && $lastchar == "]" && -n $rphost ]]; then
echo "${red}[ERROR] Please, enter a valid host and port!${end}"
exit 1
fi
fi
# Support for custom nginx rules.
sudo mkdir -p /var/www/.proxy
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
sudo sed -i "s/domain.com/$domain/g" /etc/nginx/sites-available/$domain
echo "${gre}"
if [[ $(echo "${rphost}" | cut -d':' -f 1 -s ) =~ ^(http|https)$ ]]; then
sudo sed -i "/proxy_set_header Host/d" /etc/nginx/sites-available/$domain
sudo sed -i "/proxy_set_header X-/d" /etc/nginx/sites-available/$domain
sudo sed -i "/proxy_redirect ws:/d" /etc/nginx/sites-available/$domain
sudo sed -i '/proxy_read_timeout/a \ proxy_intercept_errors on;' /etc/nginx/sites-available/$domain
sudo sed -i '/proxy_read_timeout/a \ proxy_ignore_headers Set-Cookie;' /etc/nginx/sites-available/$domain
sudo sed -i "s#http://localhost#$rphost#g" /etc/nginx/sites-available/$domain
echo " Custom URL host: ${blu}$rphost${gre}"
proxyurl="true"
elif [[ -z $rphost ]]; then
echo " Default host: ${blu}localhost${gre}"
else
sudo sed -i "s#localhost#$rphost#g" /etc/nginx/sites-available/$domain
echo " Custom host: ${blu}$rphost${gre}"
fi
if [[ -n $rpport ]]; then
sudo sed -i "s/8080/$rpport/g" /etc/nginx/sites-available/$domain
echo " Custom port: ${blu}$rpport${gre}"
elif [[ -z $rpport && $proxyurl == "true" ]]; then
sudo sed -i "s/:8080//g" /etc/nginx/sites-available/$domain
else
echo " Default port: ${blu}8080${gre}"
fi
# Check host and port
[[ -z $rphost ]] && rphost="localhost"
[[ -z $rpport ]] && rpport="8080"
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 [[ -n $off && ! -L /etc/nginx/sites-enabled/$domain && -a /etc/nginx/sites-available/$domain ]]; then
echo "${gre}Seems like your site${blu} $domain ${gre}is already disabled!${end}"
elif [[ -n $off && -L /etc/nginx/sites-enabled/$domain ]]; then
api-events_update si5
echo "${gre}Site${blu} $domain ${gre}has been successfully disabled!${end}"
sudo rm /etc/nginx/sites-enabled/$domain
# Site re-enabled
elif [[ -n $on && -L /etc/nginx/sites-enabled/$domain ]]; then
echo "${gre}Site${blu} $domain ${gre}is already enabled!${end}"
elif [[ -n $on && ! -L /etc/nginx/sites-enabled/$domain && -a /etc/nginx/sites-available/$domain ]]; then
api-events_update si4
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 [[ -n $delete && -a /etc/nginx/sites-available/$domain ]]; then
api-events_update si6
if [[ $domain == "default" || $domain == $(conf_read tools-port) ]]; then
echo "${red}[ERROR] System sites can not be removed!${end}"
exit 1
elif [[ -n $subfolder ]]; then
if [[ ! -a /var/www/$domain/htdocs$subfolder/wp-config.php ]]; then
echo "${red}[ERROR] Subfolder not found or is not a WP subsite!${end}"
exit 1
fi
db_delete $domain
sudo rm -rf /var/www/$domain/htdocs$subfolder
sudo rm -rf /etc/nginx/apps.d/$domain$subname*.conf
sudo sed -i "/# WebinolyCustom/,/# WebinolyCustomEnd/{/$domain$subname-php.conf/d}" /etc/nginx/sites-available/$domain
sudo sed -i "/# WebinolyCustom/,/# WebinolyCustomEnd/{/$domain$subname-wpfc.conf/d}" /etc/nginx/sites-available/$domain
echo "${gre}Subfolder${blu} $subfolder ${gre}from${blu} $domain ${gre}has been successfully deleted!${end}"
else
deletesite
fi
# SSL (Letsencrypt)
elif [[ -n $ssl ]]; then
if [[ $ssl == "renew" && -z $domain ]]; then
# Post-Hook removed because we always restart nginx at the end of site command
sudo certbot renew
elif [[ ( $ssl == "on" || $ssl == "force-renewal" ) && -a /etc/nginx/sites-available/$domain ]]; then
api-events_update si10
if [[ $(is_ssl $domain) == "true" && $ssl != "force-renewal" ]]; then
echo "${gre}SSL is already enabled for your site -${blu} $domain ${end}"
elif [[ -n $ssl_crt || -n $ssl_key ]]; then
if [[ -a $ssl_crt && -a $ssl_key ]]; then
ssl_nginx
echo "${gre}Your Custom SSL Cert have been successfully enabled on your site -${blu} $domain${end}"
else
echo "${red}[ERROR] Custom SSL cert files not found! ${end}"
exit 1
fi
elif [[ -n $add_to_wildcard ]]; then
if [[ $add_to_wildcard == true ]]; then
echo "${gre}Please, enter a valid Wildcard SSL Domain! ${end}"
read -p "${blu}Domain: ${end}" add_to_wildcard
add_to_wildcard=${add_to_wildcard:-NeverMatchDotCom}
fi
# Check if domain is a subdomain that can be used in the wildcard cert.
if [[ $domain == *.*."$add_to_wildcard" ]]; then
echo "${red}[ERROR] Wildcard Certs only support first-level subdomains!${end}"
exit 1
elif [[ ! -d /etc/letsencrypt/live/$add_to_wildcard ]]; then
echo "${red}[ERROR] Wildcard Cert not found!${end}"
exit 1
elif [[ $domain == *."$add_to_wildcard" ]]; then
ssl_nginx
echo "${gre}Your site ${blu}- $domain -${gre} have been successfully added to the ${blu}- $add_to_wildcard -${gre} wildcard cert.${end}"
else
echo "${red}[ERROR] Seems like your site is not a valid subdomain!${end}"
exit 1
fi
else
site_ssl_on
fi
elif [[ $ssl == "off" && -a /etc/nginx/sites-available/$domain ]]; then
api-events_update si11
[[ $(is_ssl $domain) == "true" ]] && site_ssl_off || echo "${gre}SSL is already disabled for your site${blu} $domain ${end}"
elif [[ $ssl == "off" && ! -a /etc/nginx/sites-available/$domain && -a /etc/letsencrypt/live/$domain/cert.pem ]]; then
echo "${red}[WARNING] SSL Orphan-Cert found and will be revoked automatically!${end}"
ssl_revoke
else
echo "${red}[ERROR] Invalid value for SSL command! ${end}"
exit 1
fi
# FastCGI Cache
elif [[ -n $cache ]]; then
if [[ $cache == "off" && -a /etc/nginx/sites-available/$domain ]]; then
api-events_update si9
if [[ -n $subfolder && $(is_wp_cache $domain $subfolder) == "true" ]]; then
sudo sed -i "/$domain$subname-wpfc.conf;/c \ include apps.d/$domain$subname-php.conf;" /etc/nginx/sites-available/$domain
echo "${gre}FastCGI Cache in${blu} $domain$subfolder ${gre}has been disabled!${end}"
elif [[ $(is_wp_cache $domain) == "true" ]]; 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
if [[ ( -z $subfolder && $(is_wp $domain) == "false" ) || ( -n $subfolder && $(is_wp $domain $subfolder) == "false" ) ]]; then
echo "${red}[ERROR] Site${blu} $domain$subfolder ${red}is not a WP site!${end}"
exit 1
else
echo "${gre}FastCGI Cache is already disabled on your site${blu} $domain${end}"
fi
fi
elif [[ $cache == "on" && -a /etc/nginx/sites-available/$domain ]]; then
api-events_update si8
if [[ ( -z $subfolder && $(is_php $domain) == "true" && $(is_wp $domain) == "true" ) || ( -n $subfolder && $(is_wp $domain $subfolder) == "true" && $(is_wp_cache $domain $subfolder) == "false" ) ]]; then
[[ -z $subfolder ]] && sudo sed -i '/php.conf/c \ include common/wpfc.conf;' /etc/nginx/sites-available/$domain
[[ -n $subfolder ]] && sudo sed -i "/$domain$subname-php.conf;/c \ include apps.d/$domain$subname-wpfc.conf;" /etc/nginx/sites-available/$domain
if [[ $(is_parked $domain) == "false" && ( ! -d /var/www/$domain/htdocs$subfolder/wp-content/plugins/nginx-helper || ! -d /var/www/$domain/htdocs$subfolder/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$subfolder ${gre}has been successfully enabled! "
echo "${end}"
else
if [[ ( -z $subfolder && $(is_wp $domain) == "false" ) || ( -n $subfolder && $(is_wp $domain $subfolder) == "false" ) ]]; then
echo "${red}[ERROR] Site${blu} $domain$subfolder ${red}is not a WP site!${end}"
exit 1
else
echo "${gre}FastCGI Cache is already enabled on your site${blu} $domain$subfolder${end}"
fi
fi
else
echo "${red}[ERROR] Invalid value for cache! ${end}"
exit 1
fi
# Force www or non-www redirect (www, root, off)
elif [[ -n $force_redirect && -a /etc/nginx/sites-available/$domain ]]; then
force_redirect
# Yoast Sitemap support, per site.
elif [[ -n $yoast_sitemap && -a /etc/nginx/sites-available/$domain ]]; then
if [[ ( -z $subfolder && $(is_wp $domain) == "false" ) || ( -n $subfolder && $(is_wp $domain $subfolder) == "false" ) ]]; then
echo "${red}[ERROR] Site${blu} $domain$subfolder ${red}is not a WP site!${end}"
exit 1
elif [[ -n $subfolder && $(is_wp $domain $subfolder) == "true" ]]; then
ystmap=$( grep -F "yoast-sitemap.conf;" /etc/nginx/apps.d/$domain$subname-php.conf )
else
ystmap=$( grep -F "yoast-sitemap.conf;" /etc/nginx/sites-available/$domain )
fi
if [[ $yoast_sitemap == "on" ]]; then
if [[ -z $ystmap ]]; then
if [[ -z $subfolder ]]; then
sudo sed -i "/# WebinolyCustom$/a \ include common/yoast-sitemap.conf;" /etc/nginx/sites-available/$domain
else
sudo sed -i "/\/index.php?q/a \ include apps.d\/$domain$subname-yoast-sitemap.conf;" /etc/nginx/apps.d/$domain$subname-php.conf
sudo sed -i "/\/index.php?q/a \ include apps.d\/$domain$subname-yoast-sitemap.conf;" /etc/nginx/apps.d/$domain$subname-wpfc.conf
fi
echo "${gre}Yoast Sitemap Support was successfully enabled!${end}"
else
echo "${gre}Yoast Sitemap Support is already enabled!${end}"
fi
elif [[ $yoast_sitemap == "off" ]]; then
if [[ -n $ystmap ]]; then
if [[ -z $subfolder ]]; then
sudo sed -i "/yoast-sitemap.conf;/d" /etc/nginx/sites-available/$domain
else
sudo sed -i "/yoast-sitemap.conf;/d" /etc/nginx/apps.d/$domain$subname-php.conf
sudo sed -i "/yoast-sitemap.conf;/d" /etc/nginx/apps.d/$domain$subname-wpfc.conf
fi
echo "${gre}Yoast Sitemap Support was successfully disabled!${end}"
else
echo "${gre}Yoast Sitemap Support is already disabled!${end}"
fi
else
echo "${red}[ERROR] Please, enter a valid option to enable/disable Yoast Sitemap Support!${end}"
exit 1
fi
# Catch ERROR
elif [[ -n $domain && ! -a /etc/nginx/sites-available/$domain ]]; then
echo "${red}[ERROR] Site${blu} $domain ${red}doesn't exists, please enter a valid domain name!${end}"
exit 1
else
echo "${red}[ERROR] Please, enter a valid option! ${end}"
exit 1
fi
shopt -u extglob
[[ $(conf_read nginx) == "true" && -z $nginx_not ]] && sudo service nginx reload
api-events_update sie