????

Your IP : 18.116.19.246


Current Path : /usr/src/
Upload File :
Current File : //usr/src/lsws_whm_autoinstaller.sh

#!/bin/sh

# /********************************************************************
# LiteSpeed Web Server autoinstaller for cPanel/WHM
#
# @author LiteSpeed Technologies, Inc. (https://www.litespeedtech.com)
# @copyright (c) 2013-2020
# @version 1.6.2.1
# *********************************************************************/

show_help()
{
    cat <<EOF 

# ***********************************************************************
#
# This auto-installer should only be used for brand new installations.
# For existing installations, users should use
# /usr/local/lsws/admin/misc/lsup.sh to upgrade or switch versions.
#
# Prerequisites:
#    1. cPanel/WHM environment only.
#    2. Apache is running and working properly for all the web sites.
#    3. EasyApache has been run to build proper PHP modules. A matching LSPHP
#       will be built based on EasyApache's source code.
#
# Command:
#    lsws_whm_autoinstaller.sh SERIAL_NO PHP_SUEXEC port_offset admin_user admin_pass admin_email EasyApache_Integration auto_switch_to_lsws deploy_lscwp cpanel_plugin_autoinstall
#
# Example:
#    lsws_whm_autoinstaller.sh TRIAL 2 2000 admin a1234567 root@localhost 1 0 0 1
#
#    For security reasons, it is recommended that you supply all parameters
#    instead of using defaults.
#
# Input parameters:
# 
# 1. (Required) SERIAL_NO
#    This will register the server using the serial number specified and
#    retrieve a license key.
#    Inputting "TRIAL" will cause a trial key to be requested and installed.
#
# 2. (Optional) PHP_SUEXEC
#    Available values are 0 (disable), 1 (enable), 2 (user home directory only).
#    Default is 2.
#
# 3. (Optional) port_offset
#    Controls the Apache port offset. This allows you to run LiteSpeed in
#    parallel with Apache to test it out.
#    For example, if you set port offset to 1000, Apache will continue running
#    on port 80 and LiteSpeed will run on port 1080.
#    If port offset is set to 0, LiteSpeed will be ready to replace Apache as
#    the main web server (after you stop Apache and start LiteSpeed).
#    Default is 1000.
# 
# 4. (Optional) admin_user
#    Admin user name for accessing the LiteSpeed WebAdmin console.
#    Default is "admin".
#
# 5. (Optional) admin_pass
#    Admin user password for accessing the LiteSpeed WebAdmin console.
#    It is recommended that you set your own password and not use the default
#    one.
#    Default is "a1234567"
#
# 6. (Optional) admin_email
#    Admin user's email. This email address will receive important server
#    notices, such as license expiring or server core dumps.
#    Default is "root@localhost"
#
# 7. (Optional) EasyApache_Integration (1 or 0)
#    EasyApache integration. Automatically rebuild matching PHP for LiteSpeed
#    under EasyApache 3. Does nothing under EasyApache 4. Available values
#    are 1 (enable) and 0 (disable).
#    Default is 1.
#
# 8. (Optional) auto_switch_to_lsws (1 or 0)
#    Automatically switch to LiteSpeed at the end of installation. 
#    This setting will only take effect if port_offset is set to 0. 
#    Most users will need to test and verify before they switch, so the default
#    is to not automatically switch. Available values are 1 (enable) and 0 (disable).
#
# 9. (Optional) deploy_lscwp (0 or 1)
#    Automatically scan for any WordPress installations and install + enable
#    the LSCache plugin for WordPress on all found installations.
#    If not already set, the server and virtual host cache root will also be
#    set automatically.
#    This option is disabled by default. Available values are 1 (enable) and 0 (disable).
#
# 10. (Optional) cpanel_plugin_autoinstall (0 or 1)
#     Automatically install the LiteSpeed Web Cache Manager user-end plugin
#     for all cPanel users. This plugin will appear as
#     "LiteSpeed Web Cache Manager" under the 'Advanced' section of a user's
#     cPanel dashboard.
#     This option is enabled by default. Available values are 1 (enable) and 0 (disable).
#   
# ***********************************************************************


EOF

    exit 1
}

# Download files will be in /usr/src/lsws
WGET_TEMP=/usr/src/lsws

# LiteSpeed will be installed to /usr/local/lsws
LSWS_HOME=/usr/local/lsws

OS=`uname -s`

# check params
if [ $# -eq 0 ] ; then
    echo "Invalid params!"
    show_help
fi

SERIAL="$1"
echo "Serial Number is ${SERIAL}"

##
# Set initial default values for optional params
##
PHP_SUEXEC_INPUT=2
AP_PORT_OFFSET_INPUT=1000
ADMIN_USER="admin"
ADMIN_PASS="a1234567"
ADMIN_EMAIL_INPUT="root@localhost"
ENABLE_EAHOOK=1
AUTO_SWITCH=0
DEPLOY_LSCWP=0
CPANEL_PLUGIN_AUTOINSTALL=1

if [ $# -gt 1 ] ; then
    
    if [ "$2" -eq 0 ] || [ "$2" -eq 1 ] || [ "$2" -eq 2 ] ; then
        PHP_SUEXEC_INPUT="$2"  # 0 or 1 or 2
    else
        echo "Invalid PHP_SUEXEC param!"
        show_help
    fi
fi

PHP_SUEXEC="$PHP_SUEXEC_INPUT"
echo "PHP_SUEXEC = $PHP_SUEXEC"

if [ $# -gt 2 ] ; then
    
    if [ `expr "$3" : '.*[^0-9]'` -gt 0 ] ; then
        echo "Invalid port_offset param!"
        show_help
    else
        AP_PORT_OFFSET_INPUT="$3"
    fi
fi

AP_PORT_OFFSET="$AP_PORT_OFFSET_INPUT"
echo "Apache Port Offset = ${AP_PORT_OFFSET}"

if [ $# -gt 3 ] ; then
    ADMIN_USER="$4"
fi

echo "Admin User = ${ADMIN_USER}"

if [ $# -gt 4 ] ; then
    ADMIN_PASS=`expr length "$5"`
    
    if [ $ADMIN_PASS -lt 6 ] ; then
        echo "admin pass is too short!"
        show_help
    fi
    
    if [ $ADMIN_PASS -gt 64 ] ; then
        echo "admin pass is too long!"
        show_help
    fi

    ADMIN_PASS="$5"
fi

echo "Admin Password = ${ADMIN_PASS}"

if [ $# -gt 5 ] ; then
    
    if [ `expr "$6" : '^[^@]*@[^@]*$'` -gt 0 ] ; then
        ADMIN_EMAIL_INPUT="$6"
    else
        echo "Invalid admin email!"
        show_help
    fi
fi

ADMIN_EMAIL="$ADMIN_EMAIL_INPUT"
echo "Admin Email = ${ADMIN_EMAIL}"

if [ $# -gt 6 ] ; then
    
    if [ "$7" -eq 0 ] || [ "$7" -eq 1 ] ; then
        ENABLE_EAHOOK=$7  # 0 or 1
    else
        echo "Invalid easyapache_integration param!"
        show_help
    fi
fi

echo "EasyApache Integration = ${ENABLE_EAHOOK}"

if [ $# -gt 7 ] ; then
    
    if [ "$8" -eq 0 ] || [ "$8" -eq 1 ] ; then
        AUTO_SWITCH=$8  # 0 or 1 or 2
    else
        echo "Invalid auto_switch_to_lsws param!"
        show_help
    fi
fi

echo "Auto Switch to LiteSpeed = ${AUTO_SWITCH}"

if [ $# -gt 8 ] ; then
    
    if [ "$9" -eq 0 ] || [ "$9" -eq 1 ] ; then
        DEPLOY_LSCWP=$9  # 0 or 1
    else
        echo "Invalid deploy_lscwp param!"
        show_help
    fi
fi

echo "Deploy LSCache for WordPress = ${DEPLOY_LSCWP}"

if [ $# -gt 9 ] ; then

    if [ "${10}" -eq 0 ] || [ "${10}" -eq 1 ] ; then
        CPANEL_PLUGIN_AUTOINSTALL=${10}  # 0 or 1
    else
        echo "Invalid cpanel_plugin_autoinstall param!"
        show_help
    fi
fi

echo "Auto install LiteSpeed Web Cache Manager user-end cPanel plugin = ${CPANEL_PLUGIN_AUTOINSTALL}"

if [ $# -gt 10 ] ; then
    echo "Invalid input params!"
    show_help
fi

echo ""
echo ""

check_errs()
{
  if [ "${1}" -ne "0" ] ; then
    echo "**ERROR** ${2}" 
    exit ${1}
  fi
}

check_permission()
{
    INST_USER=`id`
    INST_USER=`expr "$INST_USER" : 'uid=.*(\(.*\)) gid=.*'`
    
    if [ $INST_USER != "root" ]  ; then
        check_errs 1 "Require root permission to install this plugin. Abort!"
    fi
    
    WHM_PLUGIN_CGIDIR="/usr/local/cpanel/whostmgr/docroot/cgi"
    
    if [ ! -d "$WHM_PLUGIN_CGIDIR" ] ; then
        check_errs 1 "Cannot find cPanel/WHM installation. Abort!"
    fi
}

download_lsws()
{
    PF=`uname -p`

    echo "... Detecting platform ..."

    if [ "x$OS" = "xFreeBSD" ] ; then

        if [ "x$PF" = "xi386" ]; then
            PLATFORM="-i386-freebsd6"
        elif [ "x$PF" = "xamd64" ]; then
            PLATFORM="-x86_64-freebsd6"
        else
            check_errs 1 "unkown platform '$PL' for FreeBSD."
        fi
    elif [ "x$OS" = "xSunOS" ]; then

        if [ "x$PF" = "xi386" ]; then
            BITS=`isainfo -b`

            if [ "x$BITS" = "x64" ]; then
                PLATFORM="-x86_64-solaris"
            else
                PLATFORM="-i386-solaris"
            fi
        else
            check_errs 1 "unkown platform '$PL' for Sun Solaris."
        fi
    elif [ "x$OS" = "xLinux" ]; then
        PF=`uname -m`

        if [ "x$PF" = "xi686" ] || [ "x$PF" = "xi586" ] || [ "x$PF" = "xi486" ] || [ "x$PF" = "xi386" ]; then
            PLATFORM="-i386-linux"
        elif [ "x$PF" = "xx86_64" ]; then
            PLATFORM="-x86_64-linux"
        else
            check_errs 1 "unkown platform '$PL' for Linux."
        fi
    fi

    echo "Platform is $PLATFORM"
    echo ""

    echo "... Query latest release version ..."

    DOWNLOAD_URL="https://data.easyconfig.net/lsws/latest"

    OUTPUT=`wget -q --output-document=- $DOWNLOAD_URL`

    REL_VERSION=`expr "$OUTPUT" : '.*LSWS_STABLE=\([0-9\.]*\)'`
    STABLE_BUILD=''

    if [ "x${REL_VERSION}" != "x" ]
    then
        REL_TYPE='stable'
        STABLE_BUILD=`expr "$OUTPUT" : '.*LSWS_STABLE=[0-9\.]* BUILD \([0-9]*\)'`
    else
        REL_TYPE='edge'
        REL_VERSION=`expr "$OUTPUT" : '.*LSWS=\([0-9\.]*\)'`
    fi

    echo "Lastest ${REL_TYPE} version is ${REL_VERSION}"
    echo ""

    MAJOR_VERSION=`expr $REL_VERSION : '\([0-9]*\)\..*'`
    LOCAL_DIR="lsws-${REL_VERSION}"

    # https://www.litespeedtech.com/packages/3.0/lsws-3.3.12-ent-i386-linux.tar.gz
    DOWNLOAD_URL="https://data.easyconfig.net/lsws/lsws-${REL_VERSION}-ent${PLATFORM}.tar.gz"

    if [ "x${STABLE_BUILD}" != "x" ]
    then
        DOWNLOAD_URL="${DOWNLOAD_URL}"
    fi

    if [ ! -d "$WGET_TEMP" ] ; then
        mkdir -v -p "$WGET_TEMP"
        check_errs $? "error when creating downloading directory ... abort!"
        echo "  Download directory created"
    fi

    if [ -e "$WGET_TEMP/$LOCAL_DIR.tar.gz" ]; then
        /bin/rm -f "$WGET_TEMP/$LOCAL_DIR.tar.gz"
        echo "Package downloaded before, remove the old copy"
    fi

    echo "... Downloading ... $DOWNLOAD_URL"
    wget -q --output-document=$WGET_TEMP/$LOCAL_DIR.tar.gz $DOWNLOAD_URL
    check_errs $? "error when downloading ... abort!"

    echo "Download finished successfully"
}


test_license()
{
    if [ -f "$LSWS_HOME/conf/license.key" ] && [ ! -f "$LSINSTALL_DIR/license.key" ]; then
        cp "$LSWS_HOME/conf/license.key" "$LSINSTALL_DIR/license.key"
    fi

    if [ -f "$LSWS_HOME/conf/serial.no" ] && [ ! -f "$LSINSTALL_DIR/serial.no" ]; then
        cp "$LSWS_HOME/conf/serial.no" "$LSINSTALL_DIR/serial.no"
    fi

    if [ -f "$LSINSTALL_DIR/license.key" ] && [ -f "$LSINSTALL_DIR/serial.no" ]; then
        echo "License key and serial number are available, testing..."
        echo
        $LSINSTALL_DIR/bin/lshttpd -t

        if [ $? -eq 0 ]; then
            LICENSE_OK=1
        fi

        echo
    fi

    if [ "x$LICENSE_OK" = "x" ]; then

        if [ -f "$LSINSTALL_DIR/serial.no" ]; then
            echo "Serial number is available."
            echo "Contacting licensing server ..."

            echo ""
            $LSINSTALL_DIR/bin/lshttpd -r

            if [ $? -eq 0 ]; then
                echo "[OK] License key received."
                $LSINSTALL_DIR/bin/lshttpd -t

                if [ $? -eq 0 ]; then
                        LICENSE_OK=1
                else
                    echo "The license key received does not work."
                fi
            fi
        fi
    fi

    # if [ "x$LICENSE_OK" = "x" ]; then

    #     if [ -f "$LSINSTALL_DIR/trial.key" ]; then
    #         $LSINSTALL_DIR/bin/lshttpd -t
    #         check_errs $? "Invalid license key, abort!"
    #     else
    #         check_errs 1 "Invalid license key, abort!"
    #     fi
    # fi
}

installLicense()
{
    if [ -f $LSINSTALL_DIR/serial.no ]; then
        cp -f $LSINSTALL_DIR/serial.no $LSWS_HOME/conf
        chown "$DIR_OWN" $LSWS_HOME/conf/serial.no
        chmod "$CONF_MOD" $LSWS_HOME/conf/serial.no
    fi
    
    if [ -f $LSINSTALL_DIR/license.key ]; then
        cp -f $LSINSTALL_DIR/license.key $LSWS_HOME/conf
        chown "$DIR_OWN" $LSWS_HOME/conf/license.key
        chmod "$CONF_MOD" $LSWS_HOME/conf/license.key
    fi
    
    if [ -f $LSINSTALL_DIR/trial.key ]; then
        cp -f $LSINSTALL_DIR/trial.key $LSWS_HOME/conf
        chown "$DIR_OWN" $LSWS_HOME/conf/trial.key
        chmod "$CONF_MOD" $LSWS_HOME/conf/trial.key
    fi
}

DownloadTrialKey()
{
    if [ ${OS} = "FreeBSD" ]; then
        fetch -q -T 3 -o ${LSINSTALL_DIR}/trial.key $1
    else
        # -t is tries, -T is timeout
        wget -q -t 1 -T 3 --output-document=${LSINSTALL_DIR}/trial.key $1
    fi
}

GetTrialKey()
{
    local LICENSE_SITE="https://license.litespeedtech.com/reseller/trial.key"
    local LICENSE_SITE_2="https://license2.litespeedtech.com/reseller/trial.key"

    DownloadTrialKey $LICENSE_SITE

    if [ $? -ne 0 ] ; then
        DownloadTrialKey $LICENSE_SITE_2
    fi
}

install_lsws()
{
    cd "$WGET_TEMP/"
    echo "... Extracting... tar -zxf $WGET_TEMP/$LOCAL_DIR.tar.gz"

    LSINSTALL_DIR="${WGET_TEMP}/${LOCAL_DIR}"
    
    if [ -e "${LSINSTALL_DIR}" ]; then
        /bin/rm -rf "${LSINSTALL_DIR}"
    fi

    tar -zxf "$WGET_TEMP/$LOCAL_DIR.tar.gz"
    check_errs $? "Could not extract $LOCAL_DIR.tar.gz"

   if [ "$SERIAL" = "TRIAL" ] ; then
        GetTrialKey
    else
        echo "$SERIAL" > "${LSINSTALL_DIR}/serial.no"
    fi

    echo ""
    echo "Prepare Installing ..."

    echo "
    LSWS_HOME=$LSWS_HOME
    AP_PORT_OFFSET=$AP_PORT_OFFSET
    PHP_SUEXEC=$PHP_SUEXEC
    ADMIN_USER=$ADMIN_USER
    "

    cd "${LSINSTALL_DIR}"
    
    . ./functions.sh 2>/dev/null
    
    if [ $? != 0 ]; then
        . ./functions.sh
        check_errs $? "Can not include 'functions.sh'."
    fi

    init

    ##
    # The variables below must be set after init() call.
    ##
    INSTALL_TYPE="reinstall"
    ADMIN_EMAIL="$ADMIN_EMAIL_INPUT"
    AP_PORT_OFFSET="$AP_PORT_OFFSET_INPUT"
    PHP_SUEXEC="$PHP_SUEXEC_INPUT"

    PHP_SUFFIX=php
    
    SETUP_PHP=1
    ADMIN_PORT=7080
    DEFAULT_PORT=8088
    
    HOST_PANEL="cpanel"
    WS_USER=nobody
    WS_GROUP=nobody
    PANEL_VARY=".ea4"

    if [ 'x$ADMIN_USER' != 'x' ] && [ 'x$PASS_ONE' != 'x' ]; then
        ENCRYPT_PASS=`"$LSINSTALL_DIR/admin/fcgi-bin/admin_php5" -q "$LSINSTALL_DIR/admin/misc/htpasswd.php" $ADMIN_PASS`
        echo "$ADMIN_USER:$ENCRYPT_PASS" > "$LSINSTALL_DIR/admin/conf/htpasswd"
    fi

    configRuby

    if [ ! -e "$LSWS_HOME" ]; then
        mkdir  "$LSWS_HOME"
    fi

    test_license
    
    echo ""
    echo "Installing LiteSpeed web server, please wait... "
    echo ""

    buildApConfigFiles

    installation
    
    installLicense

    echo ""

    $LSWS_HOME/admin/misc/rc-inst.sh
    $LSWS_HOME/admin/misc/fix_cagefs.sh

}



# check if root user and cPanel
check_permission

download_lsws

install_lsws

echo ""
echo ""

if [ -e "/opt/cpanel/ea-php56/root/usr/bin/lsphp" ] ; then

    if [[ `rpm -qf /usr/local/bin/lsphp` != ea-php-cli* ]]; then
        /bin/cp -pf /opt/cpanel/ea-php56/root/usr/bin/lsphp /usr/local/bin/lsphp
    fi

    ln -sf /opt/cpanel/ea-php56/root/usr/bin/lsphp /usr/local/lsws/fcgi-bin/lsphp5
fi

if [ -e "/opt/cpanel/ea-php70/root/usr/bin/lsphp" ] ; then
    echo "link lsphp7 to lsws directory"
    ln -sf /opt/cpanel/ea-php70/root/usr/bin/lsphp /usr/local/lsws/fcgi-bin/lsphp7
fi

echo ""
echo ""

if [ "x${AP_PORT_OFFSET}" = "x0" ] && [ "x${AUTO_SWITCH}" = "x1" ]; then
    echo "Trying to automatically switch to LiteSpeed."
    ${WHM_PLUGIN_CGIDIR}/lsws/bin/lsws_cmd.sh $LSWS_HOME SWITCH_TO_LSWS
else
    echo "You can now login to WHM, go to plugins - LiteSpeed Web Server to switch to LiteSpeed."
fi

echo ""
echo ""

if [ "x${DEPLOY_LSCWP}" = "x1" ] ; then
    echo "Attempting to deploy the LSCache Plugin for WordPress across all WordPress installations."
    ${LSWS_HOME}/admin/misc/lscmctl scan -e
else
    echo "Skipping LSCache Plugin for WordPress deployment..."
fi

echo ""
echo "**LITESPEED AUTOINSTALLER COMPLETE**"