Skip to content

Commit

Permalink
subfolders fixed
Browse files Browse the repository at this point in the history
Some severe issues with subfolders were fixed.
Site delete keep-db option not working with subfolders.
  • Loading branch information
QROkes committed Sep 27, 2020
1 parent d5995f0 commit d4b4083
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 35 deletions.
15 changes: 7 additions & 8 deletions lib/general
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,14 @@ wp_dbdata() {
# wp_dbdata example.com
# wp_dbdata example.com false
# $2 is set to false if you want to skip external_db questions.
local domain="$1"

# Get dbname and dbuser of a WP site
if [[ -f /var/www/$domain/htdocs$subfolder/wp-config.php ]]; then
wpconfpath="/var/www/$domain/htdocs$subfolder/wp-config.php"
elif [[ -f /var/www/$domain/wp-config.php ]]; then
wpconfpath="/var/www/$domain/wp-config.php"
elif [[ $(is_wp $domain) == "true" && $(is_parked $domain) == "true" ]]; then
wpconfpath="$(grep -G "root .*;" /etc/nginx/sites-available/$domain | sed -r 's/^.*root (.*)htdocs;$/\1/')wp-config.php"
if [[ -f /var/www/$1/htdocs$subfolder/wp-config.php ]]; then
wpconfpath="/var/www/$1/htdocs$subfolder/wp-config.php"
elif [[ -f /var/www/$1/wp-config.php ]]; then
wpconfpath="/var/www/$1/wp-config.php"
elif [[ $(is_wp $1) == "true" && $(is_parked $1) == "true" ]]; then
wpconfpath="$(grep -G "root .*;" /etc/nginx/sites-available/$1 | sed -r 's/^.*root (.*)htdocs;$/\1/')wp-config.php"
else
return
fi
Expand Down Expand Up @@ -256,7 +255,7 @@ wp_dbdata() {
echo "${blu}External DB credentials found!${end}" >&2
elif [[ -z $external_db && -n $wp_dbhost && $wp_dbhost != "localhost" ]]; then
echo "" >&2
echo "${gre}External DB${blu} '${wp_dburl}:${wp_dbport}' ${gre}found in${blu} $domain ${gre}(Press 'Enter' key twice to skip)" >&2
echo "${gre}External DB${blu} '${wp_dburl}:${wp_dbport}' ${gre}found in${blu} $1 ${gre}(Press 'Enter' key twice to skip)" >&2
read -p "${blu}External DB username: " wp_uroot
read -p "External DB password: " wp_proot

Expand Down
43 changes: 34 additions & 9 deletions lib/sites
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ createsite() {
edit_wp_db_url() {
#Example: edit_wp_db_url example.com "http://${domain}${subfolder}"
wp_dbdata $1
if [[ -n $1 && -n $2 && $(is_wp $1) == "true" && $(is_wp_installed $1) == "true" ]]; then
if [[ -n $1 && -n $2 && $(is_wp $1 $subfolder) == "true" && $(is_wp_installed $1) == "true" ]]; then
if [[ $wp_dbhost == "localhost" && $(check_mysql_connection localhost) == "true" ]]; then
sudo mysql --connect-timeout=10 --user=admin -p$ADMIN_PASS <<_EOF_
USE $wp_dbname;
Expand Down Expand Up @@ -1227,6 +1227,8 @@ clone_wp_site() {
sudo sed -i "s/$clone_from/$domain/g" /etc/nginx/apps.d/$domain$subname-php.conf
sudo sed -i "s/$clone_from/$domain/g" /etc/nginx/apps.d/$domain$subname-wpfc.conf

[[ ! -f /etc/nginx/sites-available/$domain ]] && create_empty_site

local dest=$( echo $subfolder | rev | cut -f 2- -d "/" -s | rev )
sudo mkdir -p /var/www/$domain/htdocs$dest
sudo cp -r /var/www/$clone_from/htdocs$subfolder /var/www/$domain/htdocs$dest
Expand All @@ -1237,9 +1239,9 @@ clone_wp_site() {

sudo chown -R www-data:www-data /var/www/$domain

if [[ -f /etc/nginx/sites-available/$domain ]]; then
if [[ -n $subfolder ]]; then
sudo sed -i "/# WebinolyCustom$/a \ include apps.d/$domain$subname-php.conf;" /etc/nginx/sites-available/$domain
else
elif [[ -z $subfolder && ! -f /etc/nginx/sites-available/$domain ]]; then
sudo cp /etc/nginx/sites-available/$clone_from /etc/nginx/sites-available/$domain
sudo ln -s /etc/nginx/sites-available/$domain /etc/nginx/sites-enabled/$domain
sudo sed -i "s/$clone_from/$domain/g" /etc/nginx/sites-available/$domain
Expand All @@ -1252,11 +1254,40 @@ clone_wp_site() {
# Fix server_name according to new domain/site.
[[ $subdomain == "true" ]] && local sername="server_name $domain;" || local sername="server_name $domain www.$domain;"
sudo sed -i "/server_name /c \ $sername" /etc/nginx/sites-available/$domain

# Clone Subfolder conf files from apps.d
for appfile in /etc/nginx/apps.d/$clone_from*.conf
do
if [[ -s $appfile ]]; then
local newfile=$(echo $appfile | sed "s/$clone_from/$domain/")
sudo cp $appfile $newfile

# Check if site contains WP in subfolders.
if [[ $appfile == "/etc/nginx/apps.d/$domain_"*-wpcommon.conf ]]; then
local wpsub="/$(echo $appfile | cut -f 2- -d "_" -s | cut -f -1 -d "-" -s | sed "s/_/\//g")"
if [[ -n $wpsub && -f /var/www/$domain/htdocs$wpsub/wp-config.php ]]; then
echo "${gre}${dim}WordPress installed in a subfolder${blu} '${wpsub}' ${gre}was found, we will clone it too...${end}"
sudo site $domain -subfolder=$wpsub -delete=keep-db > /dev/null 2>&1
sudo site $domain -clone-from=$clone_from -subfolder=$wpsub
fi
fi
fi
done

else
echo "${red}[ERROR] Unexpected error, site you're trying to clone already exists!${end}"
exit 1
fi

local dom=$(echo $domain$subfolder | sed "s/[^0-9A-Za-z]/_/g")
local AUTOGENPASS_WPDB=`pwgen -s -1 16`

sudo sed -i "/DB_NAME/c \define('DB_NAME', '$dom');" $wpcon
sudo sed -i "/DB_USER/c \define('DB_USER', '$dom');" $wpcon
sudo sed -i "/DB_PASSWORD/c \define('DB_PASSWORD', '$AUTOGENPASS_WPDB');" $wpcon
sudo sed -i "/'WP_CACHE_KEY_SALT'/d" $wpcon
sudo sed -i "/stop editing/i \define('WP_CACHE_KEY_SALT', 'wp_$domain$(echo $subfolder | sed "s/\//_/g")');" $wpcon

if [[ $wp_dbhost == "localhost" ]]; then
site -mysql=[localhost,$dom,$dom,$AUTOGENPASS_WPDB] > /dev/null 2>&1
mysqldump -u admin -p$ADMIN_PASS $wp_dbname | mysql -u admin -p$ADMIN_PASS $dom
Expand All @@ -1267,12 +1298,6 @@ clone_wp_site() {
edit_wp_db_url $domain "http://${domain}${subfolder}"
fi

sudo sed -i "/DB_NAME/c \define('DB_NAME', '$dom');" $wpcon
sudo sed -i "/DB_USER/c \define('DB_USER', '$dom');" $wpcon
sudo sed -i "/DB_PASSWORD/c \define('DB_PASSWORD', '$AUTOGENPASS_WPDB');" $wpcon
sudo sed -i "/'WP_CACHE_KEY_SALT'/d" $wpcon
sudo sed -i "/stop editing/i \define('WP_CACHE_KEY_SALT', 'wp_$domain$(echo $subfolder | sed "s/\//_/g")');" $wpcon

if [[ $replace_content != "off" ]]; then
replace_content="[http://$clone_from,http://$domain]" && wp_replace_content > /dev/null 2>&1
replace_content="[https://$clone_from,https://$domain]" && wp_replace_content > /dev/null 2>&1
Expand Down
42 changes: 24 additions & 18 deletions plugins/site
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,16 @@ if [[ -n $delete_all ]]; then
# Create PHP or MySQL site
elif [[ -n $mysql || ( -n $php && -n $domain ) ]]; then
if [[ -n $domain ]]; then
# Temporary FIX
if [[ $(is_proxy $domain) == "true" || ( -f /etc/nginx/sites-available/$domain && -n $(grep -F "proxy.conf;" /etc/nginx/sites-available/$domain)) ]]; then
echo "${red}[ERROR] Incompatible with your current Reverse-Proxy site! (We will fix this issue soon)${end}"
exit 1
fi

if [[ -f /etc/nginx/sites-available/$domain && -n $subfolder && $(is_subfolder $domain $subfolder) != "false" ]]; then
echo "${red}[ERROR] Subfolder${blu} $subfolder ${red}already exists!${end}"
exit 1
elif [[ -n $subfolder ]]; then

# Temporary FIX
if [[ $(is_proxy $domain) == "true" ]]; then
echo "${red}[ERROR] Subfolders are not supported when main site is Reverse Proxy! (We will fix this issue soon)${end}"
exit 1
fi

elif [[ -n $subfolder ]]; then
[[ ! -f /etc/nginx/sites-available/$domain ]] && create_empty_site
php_subfolder
sudo mkdir -p /var/www/$domain/htdocs$subfolder
Expand All @@ -164,8 +163,8 @@ elif [[ -n $mysql || ( -n $php && -n $domain ) ]]; then
elif [[ -n $html && -n $domain ]]; then

# Temporary FIX
if [[ $(is_proxy $domain) == "true" ]]; then
echo "${red}[ERROR] Subfolders are not supported when main site is Reverse Proxy! (We will fix this issue soon)${end}"
if [[ $(is_proxy $domain) == "true" || ( -f /etc/nginx/sites-available/$domain && -n $(grep -F "proxy.conf;" /etc/nginx/sites-available/$domain)) ]]; then
echo "${red}[ERROR] Incompatible with your current Reverse-Proxy site! (We will fix this issue soon)${end}"
exit 1
fi

Expand All @@ -192,6 +191,12 @@ elif [[ -n $html && -n $domain ]]; then

# Create WP site
elif [[ -n $wp && -n $domain ]]; then
# Temporary FIX
if [[ $(is_proxy $domain) == "true" || ( -f /etc/nginx/sites-available/$domain && -n $(grep -F "proxy.conf;" /etc/nginx/sites-available/$domain)) ]]; then
echo "${red}[ERROR] Incompatible with your current Reverse-Proxy site! (We will fix this issue soon)${end}"
exit 1
fi

if [[ -z $subfolder ]]; then
if [[ $(is_empty_root_site $domain) == "true" ]]; then
wpinstall
Expand All @@ -205,12 +210,6 @@ elif [[ -n $wp && -n $domain ]]; then
[[ $(conf_read yoast-sitemap) == "true" ]] && sudo site $domain -yoast-sitemap=on > /dev/null 2>&1
[[ -n $env ]] && sudo site $domain -env=$env
else
# Temporary FIX
if [[ $(is_proxy $domain) == "true" ]]; then
echo "${red}[ERROR] Subfolders are not supported when main site is Reverse Proxy! (We will fix this issue soon)${end}"
exit 1
fi

if [[ -f /etc/nginx/sites-available/$domain && -n $subfolder && $type == [123] && $(is_subfolder $domain $subfolder) != "false" ]]; then
echo "${red}[ERROR] Subfolder${blu} $subfolder ${red}already exists!${end}"
exit 1
Expand Down Expand Up @@ -254,6 +253,13 @@ elif [[ -n $parked && -n $domain ]]; then

# Reverse proxy
elif [[ -n $proxy && -n $domain ]]; then

# Temporary FIX
if [[ -n $subfolder && -f /etc/nginx/sites-available/$domain && $(is_proxy $domain) != "true" ]]; then
echo "${red}[ERROR] Reverse Proxy subfolders only can be installed when main site is reverse proxy! (We will fix this issue soon)${end}"
exit 1
fi

reverse_proxy


Expand Down Expand Up @@ -296,7 +302,7 @@ elif [[ -n $delete && -f /etc/nginx/sites-available/$domain ]]; then
if [[ $(is_subfolder $domain $subfolder) == "false" ]]; then
echo "${red}[ERROR] Subfolder site not found!${end}"
exit 1
elif [[ -f /var/www/$domain/htdocs$subfolder/wp-config.php ]]; then
elif [[ -f /var/www/$domain/htdocs$subfolder/wp-config.php && $delete != "keep-db" ]]; then
db_delete $domain
fi

Expand Down Expand Up @@ -405,7 +411,7 @@ elif [[ -n $clone_from ]]; then
fi

if [[ $(is_wp_installed $clone_from) != "true" ]]; then
echo "${red}[ERROR] Seems like WordPress is still not configured in your${blu} '$clone_from' ${red}site!${end}"
echo "${red}[ERROR] Seems like WordPress is still not configured in your${blu} '${clone_from}${subfolder}' ${red}site!${end}"
exit 1
else
clone_wp_site
Expand Down

0 comments on commit d4b4083

Please sign in to comment.