2017-09-16 14:37:13 -06:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Site Manager Plugin (Create, delete and de/activate)
|
|
|
|
# Syntax: site <domain> <option> <argument>
|
2017-12-20 21:43:34 -06:00
|
|
|
# Options: -html, -php, -mysql, -wp, -wpsubdir, -wpsubdom, -parked, -on, -off, -delete, -delete-all, -list, -nocache, -cache, -ssl-on, ssl-off
|
2017-09-16 14:37:13 -06:00
|
|
|
# Arguments: -cache, -root
|
|
|
|
|
|
|
|
# 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 [[ "$type" == "-cache" && -n $cache ]]; then
|
|
|
|
type="$3"
|
|
|
|
cache="$2"
|
|
|
|
fi
|
|
|
|
|
2018-02-14 20:32:43 -07:00
|
|
|
|
2018-02-18 19:59:21 -07:00
|
|
|
# Extract value if exist
|
|
|
|
if [[ $type == "-parked="* || $type == "-proxy="* || $type == "-wp="* || $type == "-wpsubdir="* || $type == "-wpsubdom="* ]]; then
|
|
|
|
value=$(echo "${type}" | cut -d'=' -f 2 -s)
|
|
|
|
type=$(echo "${type}" | cut -d'=' -f 1 -s)
|
2017-09-16 14:37:13 -06:00
|
|
|
fi
|
2018-02-18 19:59:21 -07:00
|
|
|
if [[ $cache == "-root="* ]]; then
|
|
|
|
value=$(echo "${cache}" | cut -d'=' -f 2 -s)
|
|
|
|
cache=$(echo "${cache}" | cut -d'=' -f 1 -s)
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2018-03-27 13:32:48 -07:00
|
|
|
# Check if site is sub-domain
|
|
|
|
if [[ -n $domain && $type =~ ^(-html|-php|-mysql|-wp|-wpsubdir|-wpsubdom|-parked)$ ]]; 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
|
|
|
|
[[ -z $(echo "$(echo "${domain}" | rev | cut -d'.' -f $count- -s | rev)" | cut -d'.' -f 2 -s) ]] && subdomflag="0" || subdomflag="1"
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2018-02-18 19:59:21 -07:00
|
|
|
# Evaluate if site to create is WP ( 0 = noWP, 1 = WP, 2 = WP with default values, 3 = WP with custom values )
|
|
|
|
wp="0"
|
|
|
|
if [[ $type == "-wp" || $type == "-wpsubdir" || $type == "-wpsubdom" ]]; then
|
|
|
|
wp="1"
|
|
|
|
|
|
|
|
if [[ -n $value ]]; then
|
|
|
|
if [[ $value == "default" ]]; then
|
|
|
|
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}
|
|
|
|
|
|
|
|
# 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) || ($setmsqcustom == "false" && $setpwpcustom == "false")) ]]; then
|
|
|
|
wp="3"
|
|
|
|
fi
|
2017-12-29 18:13:59 -06:00
|
|
|
fi
|
2017-12-01 18:15:06 -07:00
|
|
|
fi
|
|
|
|
fi
|
2017-09-16 14:37:13 -06:00
|
|
|
|
|
|
|
# Cache validation
|
|
|
|
if [[ "$cache" == "-cache" && "$wp" == "0" ]]; then
|
|
|
|
echo "${red} Site $domain is not a WP site! ${end}"
|
|
|
|
exit 1
|
|
|
|
elif [[ ( "$cache" != "-cache" && "$cache" != "-root" ) && -n "$cache" && "$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 "/var/www"/*
|
|
|
|
do
|
|
|
|
domi=$(echo $site | cut -f 4 -d "/")
|
|
|
|
echo "${gre} - $domi ${end}"
|
|
|
|
done
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
|
|
|
|
# Delete all sites
|
|
|
|
elif [[ "$domain" == "-delete-all" && -z "$type" && -z "$cache" ]]; then
|
2018-01-25 18:01:58 -07:00
|
|
|
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}"
|
2017-09-16 14:37:13 -06:00
|
|
|
# List all sites in /var/www/ folder
|
|
|
|
for site in "/var/www"/*
|
|
|
|
do
|
2018-01-25 18:01:58 -07:00
|
|
|
domi=$(echo $site | cut -f 4 -d "/")
|
|
|
|
# List sites then will be deleted
|
|
|
|
if [[ $domi != "html" && $domi != $(conf_read tools-port) ]]; then
|
|
|
|
echo "${gre}Your site ${blu}${domi}${gre} has been successfully deleted! ${end}"
|
|
|
|
fi
|
2017-09-16 14:37:13 -06:00
|
|
|
# Determina if site is WP (so has DB to delete)
|
|
|
|
if [[ -a $site/wp-config.php || -a $site/htdocs/wp-config.php ]]; then
|
|
|
|
db_delete $domi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# Delete all files
|
|
|
|
# -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
|
|
|
|
|
2017-12-18 21:26:05 -06:00
|
|
|
|
|
|
|
# Create MYSQL site
|
|
|
|
elif [ "$type" == "-mysql" ]; then
|
|
|
|
# We use WP function to create DB when wp=0 WP files are not installed.
|
|
|
|
createsite
|
|
|
|
wpinstall
|
2018-02-13 19:39:54 -07:00
|
|
|
elif [[ "$domain" == "-mysql" && -z "$type" && -z "$cache" ]]; then
|
|
|
|
# Hack to create a DB easily (without site).
|
|
|
|
wpinstall
|
|
|
|
|
2017-09-16 14:37:13 -06:00
|
|
|
|
|
|
|
# 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
|
2018-02-26 17:53:27 -07:00
|
|
|
# Check for duplicate sites
|
|
|
|
if [[ -a /etc/nginx/sites-available/$domain ]]; then
|
|
|
|
echo "${red}Site $domain could not be created because already exists!${end}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2017-09-16 14:37:13 -06:00
|
|
|
mapto="NeverMatchAtFirstDotCom"
|
2018-02-18 19:59:21 -07:00
|
|
|
[[ -n $value ]] && mapto="$value"
|
2017-09-16 14:37:13 -06:00
|
|
|
|
|
|
|
while [[ ! -a /etc/nginx/sites-available/$mapto ]]
|
|
|
|
do
|
|
|
|
echo "${blu}"
|
|
|
|
read -p "Main site domain: " mapto
|
|
|
|
mapto=${mapto:-NeverMatchAtFirstDotCom}
|
|
|
|
echo "${end}"
|
|
|
|
if [[ ! -a /etc/nginx/sites-available/$mapto ]]; then
|
|
|
|
echo "${red} Main site domain not found! ${end}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
sudo cp /etc/nginx/sites-available/$mapto /etc/nginx/sites-available/$domain
|
|
|
|
sudo chmod 644 /etc/nginx/sites-available/$domain
|
|
|
|
sudo chown root:root /etc/nginx/sites-available/$domain
|
|
|
|
sudo ln -s /etc/nginx/sites-available/$domain /etc/nginx/sites-enabled/$domain
|
|
|
|
|
|
|
|
# Nginx conf file for the new parked 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/conf/nginx" /etc/nginx/sites-available/$domain | sed "s/$domain/$mapto/" )
|
|
|
|
sed -i "/include \/var\/www\/$domain\/conf\/nginx/c \ ${cusconl}" /etc/nginx/sites-available/$domain
|
|
|
|
|
|
|
|
echo "${gre}"
|
|
|
|
echo " Parked domain was successfully configured! "
|
|
|
|
echo "${end}"
|
|
|
|
|
2017-11-28 19:53:21 -07:00
|
|
|
# Reverse proxy
|
2018-02-18 19:59:21 -07:00
|
|
|
elif [[ "$type" == "-proxy" ]]; then
|
2017-11-28 19:53:21 -07:00
|
|
|
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
|
|
|
|
|
2018-02-18 19:59:21 -07:00
|
|
|
if [[ -n $value ]]; then
|
|
|
|
rphost=$(echo "${value}" | cut -d':' -f 1 -s)
|
|
|
|
rpport=$(echo "${value}" | cut -d':' -f 2 -s)
|
|
|
|
fi
|
2017-11-28 19:53:21 -07:00
|
|
|
|
2018-01-16 13:23:24 -07:00
|
|
|
echo "${gre}"
|
2017-11-28 19:53:21 -07:00
|
|
|
if [[ -n $rphost ]]; then
|
|
|
|
sudo sed -i "s/localhost/$rphost/g" /etc/nginx/sites-available/$domain
|
2018-01-16 13:23:24 -07:00
|
|
|
echo " Custom host found: $rphost"
|
|
|
|
else
|
|
|
|
echo " Default host: localhost"
|
2017-11-28 19:53:21 -07:00
|
|
|
fi
|
2018-01-16 13:23:24 -07:00
|
|
|
if [[ -n $rpport && $rpport -ge 0 && $rpport -le 65535 && $rpport =~ ^[0-9]+$ ]]; then
|
2017-11-28 19:53:21 -07:00
|
|
|
sudo sed -i "s/8080/$rpport/g" /etc/nginx/sites-available/$domain
|
2018-01-16 13:23:24 -07:00
|
|
|
echo " Custom port found: $rpport"
|
|
|
|
else
|
|
|
|
echo " Default port: 8080"
|
2017-11-28 19:53:21 -07:00
|
|
|
fi
|
2018-01-16 13:23:24 -07:00
|
|
|
echo "${end}"
|
2017-11-28 19:53:21 -07:00
|
|
|
|
|
|
|
|
2017-09-16 14:37:13 -06:00
|
|
|
# Site disabled
|
|
|
|
elif [[ "$type" == "-off" && ! -L /etc/nginx/sites-enabled/$domain ]]; then
|
|
|
|
echo "${red}Site $domain doesn't exist or is already disabled!${end}"
|
|
|
|
elif [[ "$type" == "-off" && -L /etc/nginx/sites-enabled/$domain ]]; then
|
|
|
|
echo "${gre}Site $domain 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 $domain is already enabled!${end}"
|
|
|
|
elif [[ "$type" == "-on" && ! -L /etc/nginx/sites-enabled/$domain ]]; then
|
|
|
|
sudo ln -s /etc/nginx/sites-available/$domain /etc/nginx/sites-enabled/$domain
|
|
|
|
echo "${gre}Site $domain has been successfully enabled!${end}"
|
|
|
|
|
|
|
|
|
|
|
|
# Delete site
|
|
|
|
elif [[ "$type" == "-delete" && -a /etc/nginx/sites-available/$domain ]]; then
|
|
|
|
deletesite
|
|
|
|
echo "${gre}Site $domain 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 )
|
|
|
|
if [[ -z $isssl ]]; then
|
|
|
|
site_ssl_on
|
|
|
|
else
|
|
|
|
echo "${red}SSL is already enabled for site $domain!${end}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# SSL disabled (Letsencrypt)
|
|
|
|
elif [[ "$type" == "-ssl-off" && -a /etc/nginx/sites-available/$domain ]]; then
|
|
|
|
isssl=$( grep -F "ssl on;" /etc/nginx/sites-available/$domain )
|
|
|
|
if [[ -n $isssl ]]; then
|
|
|
|
site_ssl_off
|
|
|
|
echo "${gre}SSL have been successfully disabled for site $domain!${end}"
|
|
|
|
else
|
|
|
|
echo "${red}SSL is already disabled for site $domain!${end}"
|
|
|
|
fi
|
2017-11-28 19:53:21 -07:00
|
|
|
|
|
|
|
|
2017-09-16 14:37:13 -06:00
|
|
|
# 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 $domain has been disabled!${end}"
|
|
|
|
else
|
|
|
|
echo "${red} Site $domain 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
|
2018-03-19 14:32:37 -07:00
|
|
|
if [[ ! -d /var/www/$domain/htdocs/wp-content/plugins/nginx-helper || ! -d /var/www/$domain/htdocs/wp-content/plugins/redis-cache ]]; then
|
2017-09-16 14:37:13 -06:00
|
|
|
echo "${gre}"
|
2018-03-19 14:32:37 -07:00
|
|
|
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}"
|
2017-09-16 14:37:13 -06:00
|
|
|
while read -r -n 1 -s answer; do
|
|
|
|
answer=${answer:-y}
|
|
|
|
if [[ $answer = [YyNn] ]]; then
|
2018-03-19 14:32:37 -07:00
|
|
|
[[ $answer = [Yy] ]] && wp_cache_plugins
|
2017-09-16 14:37:13 -06:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
echo "${gre}"
|
|
|
|
echo " FastCGI Cache in $domain has been successfully enabled! "
|
|
|
|
echo "${end}"
|
|
|
|
else
|
|
|
|
echo "${red} Site $domain is not a WP site or FastCGI is already enabled!${end}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Catch ERROR
|
|
|
|
elif [[ ! -a /etc/nginx/sites-available/$domain && ( "$type" == "-nocache" || "$type" == "-cache" || "$type" == "-delete" || "$type" == "-on" || "$type" == "-ssl-on" || "$type" == "-ssl-off" ) ]]; then
|
|
|
|
echo "${red}Site $domain doesn't exists!${end}"
|
|
|
|
else
|
|
|
|
echo "${red} Argument '${type}' is not a valid option! ${end}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
shopt -u extglob
|
|
|
|
|
|
|
|
|
|
|
|
if [[ $(conf_read nginx) == "true" ]]; then
|
|
|
|
sudo service nginx reload
|
|
|
|
fi
|