2017-09-16 20:37:13 +00:00
#!/bin/bash
# This is a library for Site Manager Plugin
# Functions for site creation and delete
source /opt/webinoly/lib/general
2018-03-19 21:32:37 +00:00
wp_cache_plugins() {
if [[ ! -d /var/www/$domain/htdocs/wp-content/plugins/nginx-helper ]]; then
# Download Nginx Helper Plugin
2018-10-27 03:30:46 +00:00
sudo wget --timeout=15 -t 1 -qrO /var/www/$domain/htdocs/nginx-helper-plugin.zip https://downloads.wordpress.org/plugin/nginx-helper.latest-stable.zip
2018-03-19 21:32:37 +00:00
sudo unzip -qq /var/www/$domain/htdocs/nginx-helper-plugin.zip -d /var/www/$domain/htdocs/wp-content/plugins/
sudo rm /var/www/$domain/htdocs/nginx-helper-plugin.zip
echo ""
echo "${gre} Nginx Helper Plugin has been installed!"
echo " Please, activate this plugin for a better experience with FastCgi Cache."
echo "${blu} ** IMPORTANT - Plugin Settings **"
echo "- The “Caching Method” should be set to “Nginx FastCgi Cache”."
echo "- Support for purge/url method has been disabled because it is considered a security risk."
echo "- The Purge Method should be set to “Delete local server cache file” "
echo "${end}"
else
echo "${red}Nginx Helper Plugin is already installed!${end}"
fi
if [[ ! -d /var/www/$domain/htdocs/wp-content/plugins/redis-cache ]]; then
# Download Redis Object Cache Plugin
2018-10-27 03:30:46 +00:00
sudo wget --timeout=15 -t 1 -qrO /var/www/$domain/htdocs/redis-cache-plugin.zip https://downloads.wordpress.org/plugin/redis-cache.latest-stable.zip
2018-03-19 21:32:37 +00:00
sudo unzip -qq /var/www/$domain/htdocs/redis-cache-plugin.zip -d /var/www/$domain/htdocs/wp-content/plugins/
sudo rm /var/www/$domain/htdocs/redis-cache-plugin.zip
echo ""
echo "${gre} Redis Object Cache Plugin has been installed!"
echo " Please, activate this plugin for a better experience with WordPress Object Cache."
echo "${blu} ** IMPORTANT - Plugin Settings **"
echo "- Go to “Menu > Settings > Redis” and be sure that Object Cache is enabled. "
echo "${end}"
else
echo "${red}Redis Object Cache Plugin is already installed!${end}"
fi
2018-10-27 04:03:48 +00:00
sudo chown -R www-data:www-data /var/www/$domain/htdocs/wp-content/plugins
2017-09-16 20:37:13 +00:00
}
2018-04-26 01:59:40 +00:00
domain_check() {
local din="$1"
# Only numerals 0-9, basic Latin letters, both lowercase and uppercase, hyphen.
[[ $din =~ ^[\.0-9A-Za-z\-]+$ ]] || domfail="true"
# Check Lenght
[[ ${#din} -gt 67 ]] && domfail="true"
# Can not start or end with a hyphen
[[ $(echo "${din}" | cut -c-1) == "-" || $(echo "${din}" | rev | cut -c-1) == "-" ]] && domfail="true"
2018-11-02 04:42:27 +00:00
# Can not contain two points together and can not start or end with a point
[[ $din == *..* || $(echo "${din}" | cut -c-1) == "." || $(echo "${din}" | rev | cut -c-1) == "." ]] && domfail="true"
2018-04-26 01:59:40 +00:00
[[ $domfail == "true" ]] && echo "${red}[WARNING] Domain names can only contain letters, numbers or a hyphen; can not start or end with a hyphen and can be up to 67 characters long.${end}"
}
2017-09-16 20:37:13 +00:00
2018-02-14 04:31:16 +00:00
dbword_check() {
local win="$1"
local RANDOM_NAME=`pwgen -s -1`
# Reserved words - https://mariadb.com/kb/en/library/reserved-words/
# https://mariadb.com/kb/en/library/identifier-names/
2018-02-28 00:51:05 +00:00
# Trim start/end spaces and quotes
win=$(echo ${win//\'})
win=$(echo ${win//\"} | xargs)
2018-04-26 00:56:44 +00:00
# Check Lenght
[[ ( ${#win} -gt 80 && $2 == "user" ) || ( ${#win} -gt 64 && $2 != "user" ) ]] && win="Webinoly_$RANDOM_NAME"
2018-02-28 00:51:05 +00:00
# Reserved words
2018-04-26 00:56:44 +00:00
shopt -s nocasematch
2018-02-28 00:51:05 +00:00
if [[ $win =~ ^(ACCESSIBLE|ADD|ALL|ALTER|ANALYZE|AND|AS|ASC|ASENSITIVE|BEFORE|BETWEEN|BIGINT|BINARY|BLOB|BOTH|BY|CALL|CASCADE|CASE|CHANGE|CHAR|CHARACTER|CHECK|COLLATE|COLUMN|CONDITION|CONSTRAINT|CONTINUE|CONVERT|CREATE|CROSS|CURRENT_DATE|CURRENT_TIME|CURRENT_TIMESTAMP|CURRENT_USER|CURSOR|DATABASE|DATABASES|DAY_HOUR|DAY_MICROSECOND|DAY_MINUTE|DAY_SECOND|DEC|DECIMAL|DECLARE|DEFAULT|DELAYED|DELETE|DESC|DESCRIBE|DETERMINISTIC|DISTINCT|DISTINCTROW|DIV|DOUBLE|DROP|DUAL|EACH|ELSE|ELSEIF|ENCLOSED|ESCAPED|EXISTS|EXIT|EXPLAIN|FALSE|FETCH|FLOAT|FLOAT4|FLOAT8|FOR|FORCE|FOREIGN|FROM|FULLTEXT|GENERAL|GRANT|GROUP|HAVING|HIGH_PRIORITY|HOUR_MICROSECOND|HOUR_MINUTE|HOUR_SECOND|IF|IGNORE|IGNORE_SERVER_IDS|IN|INDEX|INFILE|INNER|INOUT|INSENSITIVE|INSERT|INT|INT1|INT2|INT3|INT4|INT8|INTEGER|INTERVAL|INTO|IS|ITERATE|JOIN|KEY|KEYS|KILL|LEADING|LEAVE|LEFT|LIKE|LIMIT|LINEAR|LINES|LOAD|LOCALTIME|LOCALTIMESTAMP|LOCK|LONG|LONGBLOB|LONGTEXT|LOOP|LOW_PRIORITY|MASTER_HEARTBEAT_PERIOD|MASTER_SSL_VERIFY_SERVER_CERT|MATCH|MAXVALUE|MEDIUMBLOB|MEDIUMINT|MEDIUMTEXT|MIDDLEINT|MINUTE_MICROSECOND|MINUTE_SECOND|MOD|MODIFIES|NATURAL|NOT|NO_WRITE_TO_BINLOG|NULL|NUMERIC|ON|OPTIMIZE|OPTION|OPTIONALLY|OR|ORDER|OUT|OUTER|OUTFILE|PARTITION|PRECISION|PRIMARY|PROCEDURE|PURGE|RANGE|READ|READS|READ_WRITE|REAL|RECURSIVE|REFERENCES|REGEXP|RELEASE|RENAME|REPEAT|REPLACE|REQUIRE|RESIGNAL|RESTRICT|RETURN|REVOKE|RIGHT|RLIKE|ROWS|SCHEMA|SCHEMAS|SECOND_MICROSECOND|SELECT|SENSITIVE|SEPARATOR|SET|SHOW|SIGNAL|SLOW|SMALLINT|SPATIAL|SPECIFIC|SQL|SQLEXCEPTION|SQLSTATE|SQLWARNING|SQL_BIG_RESULT|SQL_CALC_FOUND_ROWS|SQL_SMALL_RESULT|SSL|STARTING|STRAIGHT_JOIN|TABLE|TERMINATED|THEN|TINYBLOB|TINYINT|TINYTEXT|TO|TRAILING|TRIGGER|TRUE|UNDO|UNION|UNIQUE|UNLOCK|UNSIGNED|UPDATE|USAGE|USE|USING|UTC_DATE|UTC_TIME|UTC_TIMESTAMP|VALUES|VARBINARY|VARCHAR|VARCHARACTER|VARYING|WHEN|WHERE|WHILE|WINDOW|WITH|WRITE|XOR|YEAR_MONTH|ZEROFILL)$ ]]; then
2018-02-14 04:31:16 +00:00
win="Webinoly_$RANDOM_NAME"
fi
shopt -u nocasematch
2018-02-28 00:51:05 +00:00
# Only numerals 0-9, basic Latin letters, both lowercase and uppercase, dollar sign, underscore.
[[ $win =~ ^[0-9A-Za-z\$_]+$ ]] || win="Webinoly_$RANDOM_NAME"
# Can not contain only numbers
[[ $win =~ ^[0-9]+$ ]] && win="Webinoly_$RANDOM_NAME"
# Floating point number confusing
2018-04-26 00:56:44 +00:00
[[ ${win:0:1} =~ ^[0-9]+$ && ${win:1:1} == "e" ]] && win="Webinoly_$RANDOM_NAME"
2018-02-28 00:51:05 +00:00
2018-02-14 04:31:16 +00:00
echo $win
}
2017-09-16 20:37:13 +00:00
wpinstall() {
2018-05-14 00:38:52 +00:00
[[ $domain == "-mysql" ]] && local dom="Webinoly_$(pwgen -s -1)" || local dom=$(echo $domain | sed "s/[^0-9A-Za-z]/_/g")
2018-03-01 00:17:44 +00:00
local ROOT_PASS=$( echo $(conf_read mysql-root) | openssl enc -d -a -salt )
2017-12-02 01:15:06 +00:00
local AUTOGENPASS_WPDB=`pwgen -s -1`
local setupmysql="y"
local setupwp="y"
local dbhost="localhost"
local dbname=$dom
local dbuser=$dom
local dbpass=$AUTOGENPASS_WPDB
local dbpref="wp_"
2017-12-30 00:13:59 +00:00
# WP with Custom Data
2018-04-04 01:44:59 +00:00
if [[ $wp == [35] ]]; then
2017-12-30 00:13:59 +00:00
[[ $setmsqcustom == "false" ]] && setupmysql="n"
[[ $setpwpcustom == "false" ]] && setupwp="n"
dbhost="$dbhostcustom"
dbname="$dbnamecustom"
dbuser="$dbusercustom"
dbpass="$dbpasscustom"
dbpref="$dbprefcustom"
dburoot="$exusercustom"
dbproot="$expasscustom"
fi
2018-04-04 01:44:59 +00:00
# Print MySQL default data
if [[ $wp == 4 ]]; then
echo "${blu}"
echo "Host: $dbhost"
echo "Database Name: $dbname"
echo "Database User: $dbuser"
echo "Password: $dbpass"
echo "${end}"
fi
2017-09-16 20:37:13 +00:00
echo "${blu}"
2017-12-19 03:26:05 +00:00
if [[ $wp == 1 ]]; then
2017-12-02 01:15:06 +00:00
echo "Do you need to setup new MySQL database? [Y/n]"
while read -r -n 1 -s setupmysql; do
setupmysql=${setupmysql:-y}
[[ $setupmysql = [YyNn] ]] && break
done
echo "Create WP-Config file automatically? [Y/n]"
while read -r -n 1 -s setupwp; do
setupwp=${setupwp:-y}
[[ $setupwp = [YyNn] ]] && break
done
fi
2017-09-16 20:37:13 +00:00
2017-12-19 03:26:05 +00:00
# Just create DB, not install WP
2018-04-04 01:44:59 +00:00
[[ $wp == [045] ]] && setupwp="n"
2017-12-19 03:26:05 +00:00
2017-09-16 20:37:13 +00:00
# Download WP (latest version)
2017-12-30 00:13:59 +00:00
if [[ $wp == [123] ]]; then
2018-10-27 03:30:46 +00:00
sudo wget --timeout=15 -t 1 -qrO /var/www/$domain/htdocs/wp.tar.gz https://wordpress.org/latest.tar.gz
2017-12-19 03:26:05 +00:00
sudo tar -xf /var/www/$domain/htdocs/wp.tar.gz -C /var/www/$domain/htdocs/
sudo mv /var/www/$domain/htdocs/wordpress/* /var/www/$domain/htdocs/
sudo rm -rf /var/www/$domain/htdocs/wordpress
sudo rm /var/www/$domain/htdocs/wp.tar.gz
fi
2017-09-16 20:37:13 +00:00
# Generate and auto-fill wp-config.php and also create database
2018-03-01 00:17:44 +00:00
if [[ $setupmysql == [yY] || $setupwp == [yY] ]] ; then
2017-09-16 20:37:13 +00:00
local done="0"
while [[ $done != "1" ]]
do
done="1"
# Ask DB data or suggest default values
echo ""
2018-04-04 01:44:59 +00:00
[[ $wp == [2345] ]] || read -p "Database Host [localhost]:" dbhost
2017-12-02 01:15:06 +00:00
dbhost=${dbhost:-localhost}
2017-09-16 20:37:13 +00:00
2018-04-04 01:44:59 +00:00
if [[ $dbhost == "localhost" && $setupmysql == [yY] && $(conf_read mysql) != "true" ]]; then
2017-09-16 20:37:13 +00:00
echo "${red} [ERROR] MySQL is not installed or localhost was not found!${blu}"
done="0"
2018-10-27 23:19:26 +00:00
[[ $wp == 1 ]] && continue 1 || exit 1
2017-09-16 20:37:13 +00:00
fi
# Check if localhost or external DB
2018-04-04 01:44:59 +00:00
if [[ $dbhost != "localhost" && $setupmysql == [yY] ]]; then
2017-09-16 20:37:13 +00:00
if [[ $(conf_read mysql-client) != "true" ]]; then
2017-11-08 21:43:39 +00:00
echo "${gre}MySQL Client is not installed and we need it to stablish a connection with your external server."
2017-09-16 20:37:13 +00:00
echo "Wait while we install MySQL Client... installing!!!${end}"
mysql_client_install > /dev/null 2>&1 &
echo "${gre}MySQL Client has been successfully installed!${end}"
fi
echo "${blu}"
2017-12-30 00:13:59 +00:00
2018-04-04 01:44:59 +00:00
if [[ $wp == 1 || ($wp == [35] && ( -z $dburoot || -z $dbproot )) ]]; then
2017-12-30 00:13:59 +00:00
read -p "External DB root username [root]: " dburoot
read -p "External DB root password: " dbproot
echo ""
local dburoot=${dburoot:-root}
local dbproot=${dbproot:-nodefined}
fi
2017-09-16 20:37:13 +00:00
local dburl=$(echo "$dbhost" | cut -f 1 -d ':')
local dbport=$(echo "$dbhost" | cut -f 2 -d ':')
fi
2018-03-01 00:17:44 +00:00
# ************* Ask for DB Name and validate data *************** #
2018-02-14 04:31:16 +00:00
dom=$(dbword_check $dom)
2018-04-04 01:44:59 +00:00
[[ $wp == [2345] ]] || read -p "Database Name [$dom]:" dbname
2017-12-02 01:15:06 +00:00
dbname=${dbname:-$dom}
2018-03-01 00:17:44 +00:00
2017-09-16 20:37:13 +00:00
# Check for duplicate database names, if already exists ask for another dbname to create the new db
2018-03-01 00:17:44 +00:00
if [[ "$setupmysql" == [yY] ]]; then
2017-09-16 20:37:13 +00:00
local newdbname="$dbname"
2018-03-01 00:17:44 +00:00
while [[ $dbname == $newdbname && $dbreuse != [yY] ]]; do
2017-09-16 20:37:13 +00:00
# Chech connection to DB first
if [[ $dbhost == "localhost" ]]; then
2017-11-08 21:43:39 +00:00
sudo mysql --connect-timeout=10 --user=root -p$ROOT_PASS -e "quit"
2018-02-27 23:29:24 +00:00
if [[ $? != "0" ]]; then
done="0"
2018-04-27 00:51:06 +00:00
# Remove the Unattended feature and ask for values.
[[ $wp == [2345] ]] && wp=1
2018-02-27 23:29:24 +00:00
echo "${red}============================================"
echo " [Error]: Database conection failed."
echo "============================================${blu}"
echo ""
continue 2;
fi
2017-09-16 20:37:13 +00:00
else
2017-11-08 21:43:39 +00:00
sudo mysql --connect-timeout=10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" -e "quit"
2018-02-27 23:29:24 +00:00
if [[ $? != "0" ]]; then
done="0"
2018-04-27 00:51:06 +00:00
[[ $wp == [2345] ]] && wp=1
2018-02-27 23:29:24 +00:00
echo "${red}============================================"
echo " [Error]: Database conection failed."
echo "============================================${blu}"
echo ""
continue 2;
fi
2017-09-16 20:37:13 +00:00
fi
if [[ $dbhost == "localhost" ]]; then
2017-11-08 21:43:39 +00:00
# https://stackoverflow.com/questions/7364709/bash-script-check-if-mysql-database-exists-perform-action-based-on-result
local newdbname=$(sudo mysqlshow --user=root -p$ROOT_PASS | grep -ow $dbname)
2017-09-16 20:37:13 +00:00
else
2017-11-08 21:43:39 +00:00
local newdbname=$(sudo mysqlshow -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" | grep -ow $dbname)
2017-09-16 20:37:13 +00:00
fi
2018-02-14 04:31:16 +00:00
if [[ $newdbname == $dbname || $dbname != $(dbword_check $dbname) ]]; then
2017-09-16 20:37:13 +00:00
echo ""
2018-02-14 04:31:16 +00:00
if [[ $newdbname == $dbname ]]; then
echo "${red}Database $dbname already exists!${blu}"
echo "Do you want to use this DB in your new site? [y/N]"
while read -r -n 1 -s dbreuse; do
local dbreuse=${dbreuse:-n}
2018-03-01 00:17:44 +00:00
[[ $dbreuse == [YyNn] ]] && break
2018-02-14 04:31:16 +00:00
done
fi
if [[ $dbname != $(dbword_check $dbname) ]]; then
2018-05-09 21:25:50 +00:00
if [[ $wp == [24] ]]; then
newdbname="Webinoly_$(pwgen -s -1)"
dbname="$newdbname"
continue 1;
fi
2018-02-28 00:51:05 +00:00
echo "${red}The DB Name can not be a reserved word or should only contain allowed characters!${blu}"
2018-02-14 04:31:16 +00:00
dbreuse="n"
fi
2017-09-16 20:37:13 +00:00
2018-03-01 00:17:44 +00:00
if [[ $dbreuse != [yY] ]]; then
2017-09-16 20:37:13 +00:00
echo ""
read -p "Please enter a new DB_NAME for your Database: " newdbname
2018-03-01 00:17:44 +00:00
[[ -z "$newdbname" ]] && newdbname="$dbname"
2017-09-16 20:37:13 +00:00
dbname="$newdbname"
2018-03-01 00:17:44 +00:00
elif [[ $dbreuse == [yY] ]]; then
2017-09-16 20:37:13 +00:00
# If you want to use the DB that already exist, abort DB creation.
setupmysql="n"
fi
fi
done
fi
2018-03-01 00:17:44 +00:00
# ************* Ask for DB User and validate data *************** #
2018-04-04 01:44:59 +00:00
[[ $wp == [2345] ]] || read -p "Database User [$dom]:" dbuser
2018-03-01 00:17:44 +00:00
dbuser=${dbuser:-$dom}
# Check for duplicate database users, if already exists ask for another dbuser to create the new user
if [[ "$setupmysql" == [yY] ]]; then
local newdbuser="$dbuser"
while [[ $dbuser == $newdbuser && $dbureuse != [yY] ]]; do
# Chech connection to DB first
if [[ $dbhost == "localhost" ]]; then
sudo mysql --connect-timeout=10 --user=root -p$ROOT_PASS -e "quit"
if [[ $? != "0" ]]; then
done="0"
echo "${red}============================================"
echo " [Error]: Database conection failed."
echo "============================================${blu}"
echo ""
continue 2;
fi
else
sudo mysql --connect-timeout=10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" -e "quit"
if [[ $? != "0" ]]; then
done="0"
echo "${red}============================================"
echo " [Error]: Database conection failed."
echo "============================================${blu}"
echo ""
continue 2;
fi
fi
if [[ $dbhost == "localhost" ]]; then
# https://stackoverflow.com/questions/7364709/bash-script-check-if-mysql-database-exists-perform-action-based-on-result
local newdbuser=$(sudo mysql --connect-timeout=10 --user=root -p$ROOT_PASS -e "SELECT User FROM mysql.user;" | grep -ow $dbuser)
else
local newdbuser=$(sudo mysql --connect-timeout=10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" -e "SELECT User FROM mysql.user;" | grep -ow $dbuser)
fi
2018-04-26 00:56:44 +00:00
if [[ $newdbuser == $dbuser || $dbuser != $(dbword_check $dbuser user) ]]; then
2018-03-01 00:17:44 +00:00
echo ""
if [[ $newdbuser == $dbuser ]]; then
echo "${red}User $dbuser already exists!${blu}"
echo "Do you want to use this DB User for your new site? [y/N]"
while read -r -n 1 -s dbureuse; do
local dbureuse=${dbureuse:-n}
[[ $dbureuse == [YyNn] ]] && break
done
fi
2018-04-26 00:56:44 +00:00
if [[ $dbuser != $(dbword_check $dbuser user) ]]; then
2018-05-09 21:25:50 +00:00
if [[ $wp == [24] ]]; then
newdbuser="Webinoly_$(pwgen -s -1)"
dbuser="$newdbuser"
continue 1;
fi
2018-03-01 00:17:44 +00:00
echo "${red}The DB User can not be a reserved word or should only contain allowed characters!${blu}"
dbureuse="n"
fi
if [[ $dbureuse != [yY] ]]; then
echo ""
read -p "Please enter a new DB_User for your Database: " newdbuser
[[ -z "$newdbuser" ]] && newdbuser="$dbuser"
dbuser="$newdbuser"
elif [[ $dbureuse == [yY] ]]; then
# If you want to use the User that already exist, abort DB creation.
setupmysql="n"
fi
fi
done
fi
2018-04-04 01:44:59 +00:00
if [[ $wp != [2345] ]]; then
2017-12-02 01:15:06 +00:00
read -p "Database Password [$AUTOGENPASS_WPDB]:" dbpass
2017-12-19 03:26:05 +00:00
# Not ask for wp_prefix when wp=0 (mysql only site)
2018-04-04 01:44:59 +00:00
[[ $wp == [045] ]] || read -p "Database Prefix [wp_]:" dbpref
2017-12-02 01:15:06 +00:00
echo "${end}"
# If empty, assign defalut values
dbpass=${dbpass:-$AUTOGENPASS_WPDB}
dbpref=${dbpref:-wp_}
fi
2017-09-16 20:37:13 +00:00
# DB Creation
2018-03-01 00:17:44 +00:00
if [[ "$setupmysql" == [yY] ]] ; then
2017-09-16 20:37:13 +00:00
if [[ $dbhost == "localhost" ]]; then
local dbsetup="CREATE DATABASE $dbname;GRANT ALL PRIVILEGES ON $dbname.* TO $dbuser@$dbhost IDENTIFIED BY '$dbpass';FLUSH PRIVILEGES;"
2017-11-08 21:43:39 +00:00
sudo mysql --connect-timeout=10 --user=root -p$ROOT_PASS -e "$dbsetup"
2018-02-27 23:29:24 +00:00
if [ $? != "0" ]; then
done="0"
echo "${red}============================================"
echo " [Error]: Database creation failed."
echo "============================================${end}"
echo ""
fi
2017-09-16 20:37:13 +00:00
else
2017-11-08 21:43:39 +00:00
sudo mysql --connect-timeout=10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" <<_EOF_
2017-09-16 20:37:13 +00:00
CREATE DATABASE ${dbname};
grant usage on ${dbname}.* to ${dbuser}@\`%\` identified by '${dbpass}';
grant all privileges on ${dbname}.* to ${dbuser}@\`%\`;
FLUSH PRIVILEGES;
_EOF_
2018-02-27 23:29:24 +00:00
if [ $? != "0" ]; then
done="0"
echo "${red}============================================"
echo " [Error]: Database creation failed."
echo "============================================${end}"
echo ""
fi
2017-09-16 20:37:13 +00:00
fi
fi
if [[ $done != "1" ]]; then
echo "${red} Some error ocurred during Database Configuration."
2017-11-08 21:43:39 +00:00
echo "${blu} Retry [Y/n]?"
2017-09-16 20:37:13 +00:00
while read -r -n 1 -s done; do
done=${done:-y}
2018-03-01 00:17:44 +00:00
[[ $done = [YyNn] ]] && break
2017-09-16 20:37:13 +00:00
done
2018-03-01 00:17:44 +00:00
[[ $done == [nN] ]] && done="1"
2017-09-16 20:37:13 +00:00
fi
done
fi
2017-11-08 21:43:39 +00:00
echo "${end}"
2017-09-16 20:37:13 +00:00
#WP-Config.php auto-setup
2018-03-01 00:17:44 +00:00
if [[ $setupwp == [yY] ]] ; then
2017-09-16 20:37:13 +00:00
# Generate random salt keys
local SALT_AUTHK=`pwgen -s -1 64`
local SALT_SECUR=`pwgen -s -1 64`
local SALT_LOGGE=`pwgen -s -1 64`
local SALT_NONCE=`pwgen -s -1 64`
local SALT_AUTHS=`pwgen -s -1 64`
local SALT_SECUS=`pwgen -s -1 64`
local SALT_LOGGS=`pwgen -s -1 64`
local SALT_NONCS=`pwgen -s -1 64`
cp /var/www/$domain/htdocs/wp-config-sample.php /var/www/$domain/wp-config.php
# Fill new wp-config with data
sudo sed -i "/DB_NAME/c \define('DB_NAME', '$dbname');" /var/www/$domain/wp-config.php
sudo sed -i "/DB_USER/c \define('DB_USER', '$dbuser');" /var/www/$domain/wp-config.php
sudo sed -i "/DB_PASSWORD/c \define('DB_PASSWORD', '$dbpass');" /var/www/$domain/wp-config.php
sudo sed -i "/DB_HOST/c \define('DB_HOST', '$dbhost');" /var/www/$domain/wp-config.php
sudo sed -i "/table_prefix/c \$table_prefix = '$dbpref';" /var/www/$domain/wp-config.php
sudo sed -i "/'AUTH_KEY'/c \define('AUTH_KEY', '$SALT_AUTHK');" /var/www/$domain/wp-config.php
sudo sed -i "/'SECURE_AUTH_KEY'/c \define('SECURE_AUTH_KEY', '$SALT_SECUR');" /var/www/$domain/wp-config.php
sudo sed -i "/'LOGGED_IN_KEY'/c \define('LOGGED_IN_KEY', '$SALT_LOGGE');" /var/www/$domain/wp-config.php
sudo sed -i "/'NONCE_KEY'/c \define('NONCE_KEY', '$SALT_NONCE');" /var/www/$domain/wp-config.php
sudo sed -i "/'AUTH_SALT'/c \define('AUTH_SALT', '$SALT_AUTHS');" /var/www/$domain/wp-config.php
sudo sed -i "/'SECURE_AUTH_SALT'/c \define('SECURE_AUTH_SALT','$SALT_SECUS');" /var/www/$domain/wp-config.php
sudo sed -i "/'LOGGED_IN_SALT'/c \define('LOGGED_IN_SALT', '$SALT_LOGGS');" /var/www/$domain/wp-config.php
sudo sed -i "/'NONCE_SALT'/c \define('NONCE_SALT', '$SALT_NONCS');" /var/www/$domain/wp-config.php
2018-04-25 20:00:22 +00:00
# Redis Conf
sudo sed -i "/stop editing/i \define('WP_REDIS_SELECTIVE_FLUSH', true);" /var/www/$domain/wp-config.php
2018-04-29 04:01:23 +00:00
sudo sed -i "/stop editing/i \define('WP_CACHE_KEY_SALT', 'redis_object_cache_for_wp_$domain');" /var/www/$domain/wp-config.php
2018-04-25 20:00:22 +00:00
2017-09-16 20:37:13 +00:00
# Multisite wp-config
if [[ "$type" == "-wpsubdir" || "$type" == "-wpsubdom" ]]; then
sudo sed -i "/stop editing/i \define('WP_ALLOW_MULTISITE', true);" /var/www/$domain/wp-config.php
sudo sed -i "/stop editing/i \#define('MULTISITE', true);" /var/www/$domain/wp-config.php
sudo sed -i "/stop editing/i \#define('DOMAIN_CURRENT_SITE', '$domain');" /var/www/$domain/wp-config.php
sudo sed -i "/stop editing/i \#define('PATH_CURRENT_SITE', '/');" /var/www/$domain/wp-config.php
sudo sed -i "/stop editing/i \#define('SITE_ID_CURRENT_SITE', 1);" /var/www/$domain/wp-config.php
sudo sed -i "/stop editing/i \#define('BLOG_ID_CURRENT_SITE', 1);" /var/www/$domain/wp-config.php
fi
if [ "$type" == "-wpsubdir" ]; then
sudo sed -i "/stop editing/i \#define('SUBDOMAIN_INSTALL', false);" /var/www/$domain/wp-config.php
elif [ "$type" == "-wpsubdom" ]; then
sudo sed -i "/stop editing/i \#define('SUBDOMAIN_INSTALL', true);" /var/www/$domain/wp-config.php
sudo sed -i "/server_name /c \ server_name $domain *.$domain;" /etc/nginx/sites-available/$domain
fi
fi
}
deletesite() {
2018-04-04 18:49:24 +00:00
if [[ -n $value ]]; then
dbdata=${value:1:-1}
local dbdatahost=$(echo "${dbdata}" | cut -d',' -f 1 -s)
[[ $dbdatahost == "external" ]] && dbdatadel=$(echo "${dbdata}" | cut -d',' -f 2 -s)
[[ $dbdatadel =~ ^(true|false)$ || $value == "force" ]] && local skip="true"
fi
2018-01-16 21:40:33 +00:00
# Determine if site is WP, so you should delete the DB too.
2017-09-16 20:37:13 +00:00
if [[ -a /var/www/$domain/wp-config.php || -a /var/www/$domain/htdocs/wp-config.php ]]; then
2018-04-04 18:49:24 +00:00
if [[ $skip != "true" ]]; then
2018-04-04 17:10:32 +00:00
echo "${blu}Delete Database [Y/n]?${end}"
while read -r -n 1 -s dbdel; do
local dbdel=${dbdel:-y}
[[ $dbdel = [YyNn] ]] && break
done
else
2018-04-04 18:49:24 +00:00
[[ $dbdatadel != "false" ]] && dbdel="Y"
2018-04-04 17:10:32 +00:00
fi
2017-09-16 20:37:13 +00:00
fi
2018-11-02 18:40:12 +00:00
[[ $dbdel == [yY] ]] && db_delete $domain
2017-09-16 20:37:13 +00:00
# Delete site files
sudo rm /etc/nginx/sites-available/$domain
2018-07-28 23:45:24 +00:00
# Check to avoid error when site is disabled.
[[ -L /etc/nginx/sites-enabled/$domain ]] && sudo rm /etc/nginx/sites-enabled/$domain
2017-09-16 20:37:13 +00:00
sudo rm -rf /var/www/$domain
}
createsite() {
2018-05-14 00:38:52 +00:00
# Check domain and then prevent reserved characters for file names.
2018-04-26 01:59:40 +00:00
domain_check $domain
2018-11-02 04:42:27 +00:00
domain=$(echo $domain | sed "s/[^\.0-9A-Za-z\-]/_/g" | sed "s/^[\.]/_/g" | sed "s/\.\./_/g")
2018-05-14 00:38:52 +00:00
2018-02-15 04:22:15 +00:00
if [[ $(conf_read php) != "true" && ("$wp" == [123] || "$type" == "-php") ]]; then
2017-09-16 20:37:13 +00:00
echo "${red}"
echo " [ERROR] PHP must be installed before you can create a WP site!"
echo "${end}"
exit 1
fi
# Check for duplicate sites
if [[ -a /etc/nginx/sites-available/$domain ]]; then
2018-11-02 19:47:11 +00:00
echo "${red}Site${blu} $domain ${red}could not be created because already exists!${end}"
2017-09-16 20:37:13 +00:00
exit 1
fi
sudo cp /opt/webinoly/templates/template-site-php /etc/nginx/sites-available/$domain
2018-02-15 04:22:15 +00:00
# Remove www support for subdomains - only main domain will support both www and non-www.
2018-03-01 00:17:44 +00:00
[[ $subdomflag == 1 ]] && sudo sed -i "s/ www.domain.com;/;/g" /etc/nginx/sites-available/$domain
2017-09-16 20:37:13 +00:00
# Nginx conf file for the new site (-php conf is default)
sudo sed -i "s/domain.com/$domain/g" /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
# Create data folder for new site
2018-04-26 22:15:44 +00:00
if [[ $type != "-proxy" && $type != "-parked" ]]; then
if [[ ! -d /var/www/$domain/htdocs || ! -d /var/www/$domain ]]; then
2017-09-16 20:37:13 +00:00
sudo mkdir -p /var/www/$domain/htdocs
2018-03-01 00:17:44 +00:00
[[ "$wp" == [123] ]] && wpinstall
2018-04-26 22:15:44 +00:00
else
echo "${blu}"
2018-11-02 19:47:11 +00:00
echo " We found a folder with${end} $domain ${blu}site data, do you want to use it [Y/n]? "
2018-04-26 22:15:44 +00:00
while read -r -n 1 -s wwwexist; do
wwwexist=${wwwexist:-y}
[[ $wwwexist == [YyNn] ]] && break
done
echo ""
if [[ $wwwexist == [nN] ]]; then
sudo rm -rf /var/www/$domain/htdocs
sudo mkdir -p /var/www/$domain/htdocs
[[ "$wp" == [123] ]] && wpinstall
fi
2017-09-16 20:37:13 +00:00
fi
fi
2018-10-27 03:30:46 +00:00
2017-09-16 20:37:13 +00:00
# Activate FastCgi cache
2017-12-30 00:13:59 +00:00
if [[ "$cache" == "-cache" && "$wp" == [123] ]]; then
2017-09-16 20:37:13 +00:00
sudo sed -i '/php.conf/c \ include common/wpfc.conf;' /etc/nginx/sites-available/$domain
2018-03-19 21:32:37 +00:00
wp_cache_plugins
2017-09-16 20:37:13 +00:00
fi
2018-10-27 03:30:46 +00:00
sudo chown -R www-data:www-data /var/www
[[ $(conf_read login-www-data) == "true" ]] && sudo chown root:root /var/www
2018-11-02 19:47:11 +00:00
echo "${gre}Site${blu} $domain ${gre}has been successfully created!${end}"
2017-09-16 20:37:13 +00:00
}
2018-05-02 21:41:31 +00:00
force_redirect() {
if ! [[ $value =~ ^(www|root|off)$ ]]; then
echo "${red}Please, enter a valid argument!${end}"
exit 1
fi
if [[ $subdomflag == 1 ]]; then
echo "${red}Please, use the main/root domain, Force-Redirection should not be used in Subdomains!${end}"
exit 1
fi
local rediron=$( grep -F "WebinolyWWWredirectStart" /etc/nginx/sites-available/$domain )
2018-05-14 01:13:31 +00:00
local sername="server_name $domain www.$domain;"
2018-05-02 21:41:31 +00:00
cp /opt/webinoly/templates/template-site-wwwredirect /tmp/template-site-wwwredirect
case $value in
"www")
[[ -n $rediron ]] && sudo sed -i '/WebinolyWWWredirectStart/,/WebinolyWWWredirectEnd/{/.*/d}' /etc/nginx/sites-available/$domain
sudo sed -i "/#server_name;/c \ server_name $domain;" /tmp/template-site-wwwredirect
sudo sed -i "/#return 301;/c \ return 301 \$scheme://www.${domain}\$request_uri;" /tmp/template-site-wwwredirect
sudo sed -i "/server_name /c \ server_name www.$domain;" /etc/nginx/sites-available/$domain
echo "${gre}Force-Redirection to WWW has been successfully enabled!${end}"
;;
"root")
[[ -n $rediron ]] && sudo sed -i '/WebinolyWWWredirectStart/,/WebinolyWWWredirectEnd/{/.*/d}' /etc/nginx/sites-available/$domain
sudo sed -i "/#server_name;/c \ server_name www.$domain;" /tmp/template-site-wwwredirect
sudo sed -i "/#return 301;/c \ return 301 \$scheme://${domain}\$request_uri;" /tmp/template-site-wwwredirect
sudo sed -i "/server_name /c \ server_name $domain;" /etc/nginx/sites-available/$domain
echo "${gre}Force-Redirection to non-WWW has been successfully enabled!${end}"
;;
"off")
if [[ -n $rediron ]]; then
sudo sed -i '/WebinolyWWWredirectStart/,/WebinolyWWWredirectEnd/{/.*/d}' /etc/nginx/sites-available/$domain
2018-05-14 01:13:31 +00:00
sudo sed -i "/server_name /c \ $sername" /etc/nginx/sites-available/$domain
2018-05-02 21:41:31 +00:00
echo "${gre}Force-Redirection has been successfully disabled!${end}"
else
2018-11-02 19:47:11 +00:00
echo "${red}Force-Redirect is already disabled for site${blu} $domain ${end}"
2018-05-02 21:41:31 +00:00
fi
;;
esac
# If SSL is enabled insert after that - First redirect should be to HTTPS due to HSTS.
isssl=$( grep -F "ssl on;" /etc/nginx/sites-available/$domain )
if [[ -z $isssl && $value =~ ^(www|root)$ ]]; then
sudo sed -i '1r /tmp/template-site-wwwredirect' /etc/nginx/sites-available/$domain
elif [[ -n $isssl && $value =~ ^(www|root)$ ]]; then
sudo sed -i '/listen 80/c \ listen 443 ssl http2;' /tmp/template-site-wwwredirect
sudo sed -i '/listen \[::\]:80/c \ listen [::]:443 ssl http2;' /tmp/template-site-wwwredirect
2018-05-18 19:28:32 +00:00
if [[ $cache != "-ignore-ssl" ]]; then
sudo sed -i '/headers-http.conf/a \ include common/headers-https.conf;' /tmp/template-site-wwwredirect
sudo sed -i '/server_name /r /opt/webinoly/templates/template-site-ssl' /tmp/template-site-wwwredirect
sudo sed -i "/WebinolySSLstart/,/WebinolySSLend/{s/domain.com/$domain/}" /tmp/template-site-wwwredirect
fi
2018-05-02 21:41:31 +00:00
sudo sed -i "/WebinolySSLredirectStart/,/WebinolySSLredirectEnd/s/\(server_name\).*$/$sername/" /etc/nginx/sites-available/$domain
sudo sed -i '/WebinolySSLredirectEnd/r /tmp/template-site-wwwredirect' /etc/nginx/sites-available/$domain
fi
sudo rm /tmp/template-site-wwwredirect
}