403Webshell
Server IP : 138.197.107.151  /  Your IP : 216.73.217.7
Web Server : Apache/2.4.58 (Ubuntu)
System : Linux BloxBy-Builder 6.8.0-71-generic #71-Ubuntu SMP PREEMPT_DYNAMIC Tue Jul 22 16:52:38 UTC 2025 x86_64
User : wpbetasites_mrakzqskir ( 1022)
PHP Version : 8.3.6
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : OFF
Directory :  /proc/3492927/root/var/bloxbycommands/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /proc/3492927/root/var/bloxbycommands/attach_domain.sh
#!/bin/bash
# ======================================================
# attach_domain.sh
# Attach a new domain to an existing WP site
# If SSL fails, automatically detaches domain
# Outputs only a single-line message for Laravel integration
# ======================================================

TEMP_DOMAIN=$1
NEW_DOMAIN=$2
WEB_ROOT=$3

APACHE_CONF_DIR="/etc/apache2/sites-available"
DOCROOT="$WEB_ROOT"
WP_PATH="$DOCROOT"

# Check arguments
if [ -z "$TEMP_DOMAIN" ] || [ -z "$NEW_DOMAIN" ] || [ -z "$WEB_ROOT" ]; then
    echo "ERROR: Missing arguments. Usage: $0 tempdomain.com newdomain.com /var/www/html"
    exit 1
fi

# Strip leading www
CLEAN_DOMAIN=${NEW_DOMAIN#www.}

# Step 1: Check if domain exists in any vhost
EXISTING_CONF=$(grep -rlE "ServerName[[:space:]]+$CLEAN_DOMAIN|ServerAlias[[:space:]]+.*$CLEAN_DOMAIN" $APACHE_CONF_DIR || true)
if [ -n "$EXISTING_CONF" ]; then
    EXISTING_ROOT=$(grep -i "DocumentRoot" "$EXISTING_CONF" | awk '{print $2}')
    if [ "$EXISTING_ROOT" != "$DOCROOT" ]; then
        echo "ERROR: $CLEAN_DOMAIN already attached to $EXISTING_ROOT"
        exit 1
    else
        echo "INFO: $CLEAN_DOMAIN already attached to same docroot ($DOCROOT), skipping vhost"
        exit 0
    fi
fi

# Step 2: Detect root domain or subdomain
if [[ "$CLEAN_DOMAIN" =~ ^[a-z0-9-]+\.[a-z]{2,}$ ]]; then
    IS_ROOT=true
else
    IS_ROOT=false
fi

# Step 3: Create Apache vhost
VHOST_FILE="$APACHE_CONF_DIR/$CLEAN_DOMAIN.conf"
cat > "$VHOST_FILE" <<EOF
<VirtualHost *:80>
    ServerName $CLEAN_DOMAIN
EOF

if [ "$IS_ROOT" = true ]; then
cat >> "$VHOST_FILE" <<EOF
    ServerAlias www.$CLEAN_DOMAIN
EOF
fi

cat >> "$VHOST_FILE" <<EOF

    DocumentRoot $DOCROOT

    <Directory $DOCROOT>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog \${APACHE_LOG_DIR}/$CLEAN_DOMAIN-error.log
    CustomLog \${APACHE_LOG_DIR}/$CLEAN_DOMAIN-access.log combined
</VirtualHost>
EOF

a2ensite "$CLEAN_DOMAIN.conf" >/dev/null 2>&1
systemctl reload apache2 >/dev/null 2>&1

# Step 4: Request SSL
if [ "$IS_ROOT" = true ]; then
    certbot --apache -d $CLEAN_DOMAIN -d www.$CLEAN_DOMAIN --non-interactive --agree-tos -m admin@$CLEAN_DOMAIN >/dev/null 2>&1
else
    certbot --apache -d $CLEAN_DOMAIN --non-interactive --agree-tos -m admin@$CLEAN_DOMAIN >/dev/null 2>&1
fi

CERTBOT_EXIT=$?
if [ $CERTBOT_EXIT -ne 0 ]; then
    # Detach domain automatically
    a2dissite "$CLEAN_DOMAIN.conf" >/dev/null 2>&1
    systemctl reload apache2 >/dev/null 2>&1
    rm -f "$APACHE_CONF_DIR/$CLEAN_DOMAIN.conf"

    # Optional: remove docroot (uncomment if you want)
    # rm -rf "$DOCROOT"

    if grep -q "too many certificates" /var/log/letsencrypt/letsencrypt.log; then
        echo "ERROR: SSL request failed for $CLEAN_DOMAIN due to rate limit; domain detached"
    else
        echo "ERROR: SSL request failed for $CLEAN_DOMAIN (Certbot exit $CERTBOT_EXIT); domain detached"
    fi
    exit 1
fi

# Step 5: Update WordPress
wp option update home "https://$CLEAN_DOMAIN" --path=$WP_PATH --allow-root >/dev/null 2>&1
wp option update siteurl "https://$CLEAN_DOMAIN" --path=$WP_PATH --allow-root >/dev/null 2>&1
wp search-replace "https://$TEMP_DOMAIN" "https://$CLEAN_DOMAIN" --all-tables --precise --path=$WP_PATH --allow-root >/dev/null 2>&1

echo "SUCCESS: Domain $CLEAN_DOMAIN attached and SSL installed"
exit 0

Youez - 2016 - github.com/yon3zu
LinuXploit