Files
ols-docker-env/bin/container/appinstallctl.sh
T

276 lines
7.3 KiB
Bash
Raw Normal View History

2019-12-30 14:43:53 -05:00
#!/bin/bash
DEFAULT_VH_ROOT='/var/www/vhosts'
VH_DOC_ROOT=''
2020-01-08 16:49:52 -05:00
VHNAME=''
2019-12-30 14:43:53 -05:00
APP_NAME=''
DOMAIN=''
WWW_UID=''
WWW_GID=''
2025-05-07 10:59:53 +08:00
WPCONSTCONF=''
2020-01-10 11:47:15 -05:00
PUB_IP=$(curl -s http://checkip.amazonaws.com)
2020-01-10 16:12:22 -05:00
DB_HOST='mysql'
2020-01-09 16:59:38 -05:00
PLUGINLIST="litespeed-cache.zip"
THEME='twentytwenty'
2020-03-06 12:07:12 -05:00
EPACE=' '
echow(){
FLAG=${1}
shift
echo -e "\033[1m${EPACE}${FLAG}\033[0m${@}"
}
2019-12-30 14:43:53 -05:00
help_message(){
2020-03-06 12:07:12 -05:00
echo -e "\033[1mOPTIONS\033[0m"
echow '-A, -app [wordpress] -D, --domain [DOMAIN_NAME]'
echo "${EPACE}${EPACE}Example: appinstallctl.sh --app wordpress --domain example.com"
echow '-H, --help'
echo "${EPACE}${EPACE}Display help and exit."
exit 0
2019-12-30 14:43:53 -05:00
}
check_input(){
if [ -z "${1}" ]; then
help_message
exit 1
fi
}
linechange(){
LINENUM=$(grep -n "${1}" ${2} | cut -d: -f 1)
if [ -n "${LINENUM}" ] && [ "${LINENUM}" -eq "${LINENUM}" ] 2>/dev/null; then
sed -i "${LINENUM}d" ${2}
sed -i "${LINENUM}i${3}" ${2}
fi
}
2020-01-09 16:59:38 -05:00
ck_ed(){
2020-01-10 11:47:15 -05:00
if [ ! -f /bin/ed ]; then
2020-02-14 11:21:38 -05:00
echo "Install ed package.."
2020-01-09 16:59:38 -05:00
apt-get install ed -y > /dev/null 2>&1
fi
}
ck_unzip(){
2020-01-10 11:47:15 -05:00
if [ ! -f /usr/bin/unzip ]; then
2020-02-14 11:21:38 -05:00
echo "Install unzip package.."
2020-01-09 16:59:38 -05:00
apt-get install unzip -y > /dev/null 2>&1
fi
}
2019-12-30 14:43:53 -05:00
get_owner(){
2020-01-08 20:04:10 -05:00
WWW_UID=$(stat -c "%u" ${DEFAULT_VH_ROOT})
WWW_GID=$(stat -c "%g" ${DEFAULT_VH_ROOT})
2019-12-30 14:43:53 -05:00
if [ ${WWW_UID} -eq 0 ] || [ ${WWW_GID} -eq 0 ]; then
WWW_UID=1000
WWW_GID=1000
2020-02-14 11:21:38 -05:00
echo "Set owner to ${WWW_UID}"
2019-12-30 14:43:53 -05:00
fi
}
get_db_pass(){
2020-01-08 16:49:52 -05:00
if [ -f ${DEFAULT_VH_ROOT}/${1}/.db_pass ]; then
2020-01-09 12:18:41 -05:00
SQL_DB=$(grep -i Database ${VH_ROOT}/.db_pass | awk -F ':' '{print $2}' | tr -d '"')
SQL_USER=$(grep -i Username ${VH_ROOT}/.db_pass | awk -F ':' '{print $2}' | tr -d '"')
SQL_PASS=$(grep -i Password ${VH_ROOT}/.db_pass | awk -F ':' '{print $2}' | tr -d '"')
2020-01-08 16:49:52 -05:00
else
2020-01-10 16:12:22 -05:00
echo 'db pass file can not locate, skip wp-config pre-config.'
fi
}
2019-12-30 14:43:53 -05:00
set_vh_docroot(){
2020-01-08 16:49:52 -05:00
if [ "${VHNAME}" != '' ]; then
2020-01-09 12:18:41 -05:00
VH_ROOT="${DEFAULT_VH_ROOT}/${VHNAME}"
2020-01-08 16:49:52 -05:00
VH_DOC_ROOT="${DEFAULT_VH_ROOT}/${VHNAME}/html"
2025-05-07 10:59:53 +08:00
WPCONSTCONF="${VH_DOC_ROOT}/wp-content/plugins/litespeed-cache/data/const.default.json"
2020-01-08 16:49:52 -05:00
elif [ -d ${DEFAULT_VH_ROOT}/${1}/html ]; then
2020-01-09 12:18:41 -05:00
VH_ROOT="${DEFAULT_VH_ROOT}/${1}"
2019-12-30 14:43:53 -05:00
VH_DOC_ROOT="${DEFAULT_VH_ROOT}/${1}/html"
2025-05-07 10:59:53 +08:00
WPCONSTCONF="${VH_DOC_ROOT}/wp-content/plugins/litespeed-cache/data/const.default.json"
2019-12-30 14:43:53 -05:00
else
echo "${DEFAULT_VH_ROOT}/${1}/html does not exist, please add domain first! Abort!"
exit 1
fi
}
check_sql_native(){
2020-01-08 16:49:52 -05:00
local COUNTER=0
2019-12-30 14:43:53 -05:00
local LIMIT_NUM=100
2021-06-09 15:56:26 +08:00
until [ "$(curl -v mysql:3306 2>&1 | grep -i 'native\|Connected')" ]; do
2020-01-08 16:49:52 -05:00
echo "Counter: ${COUNTER}/${LIMIT_NUM}"
2019-12-30 14:43:53 -05:00
COUNTER=$((COUNTER+1))
if [ ${COUNTER} = 10 ]; then
echo '--- MySQL is starting, please wait... ---'
elif [ ${COUNTER} = ${LIMIT_NUM} ]; then
2020-01-08 16:49:52 -05:00
echo '--- MySQL is timeout, exit! ---'
2019-12-30 14:43:53 -05:00
exit 1
fi
sleep 1
done
}
2020-01-09 16:59:38 -05:00
install_wp_plugin(){
for PLUGIN in ${PLUGINLIST}; do
wget -q -P ${VH_DOC_ROOT}/wp-content/plugins/ https://downloads.wordpress.org/plugin/${PLUGIN}
2020-01-10 16:12:22 -05:00
if [ ${?} = 0 ]; then
2020-01-09 16:59:38 -05:00
ck_unzip
unzip -qq -o ${VH_DOC_ROOT}/wp-content/plugins/${PLUGIN} -d ${VH_DOC_ROOT}/wp-content/plugins/
else
echo "${PLUGINLIST} FAILED to download"
fi
done
rm -f ${VH_DOC_ROOT}/wp-content/plugins/*.zip
}
set_htaccess(){
if [ ! -f ${VH_DOC_ROOT}/.htaccess ]; then
touch ${VH_DOC_ROOT}/.htaccess
fi
cat << EOM > ${VH_DOC_ROOT}/.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
EOM
}
2020-04-30 15:48:14 -04:00
get_theme_name(){
THEME_NAME=$(grep WP_DEFAULT_THEME ${VH_DOC_ROOT}/wp-includes/default-constants.php | grep -v '!' | awk -F "'" '{print $4}')
echo "${THEME_NAME}" | grep 'twenty' >/dev/null 2>&1
if [ ${?} = 0 ]; then
THEME="${THEME_NAME}"
fi
}
2020-01-09 16:59:38 -05:00
set_lscache(){
2025-05-07 10:59:53 +08:00
wget -q -O ${WPCONSTCONF} https://raw.githubusercontent.com/litespeedtech/lscache_wp/refs/heads/master/data/const.default.json
if [ -f ${WPCONSTCONF} ]; then
sed -ie 's/"object": .*"/"object": '\"true\"'/g' ${WPCONSTCONF}
sed -ie 's/"object-kind": .*"/"object-kind": '\"true\"'/g' ${WPCONSTCONF}
sed -ie 's/"object-host": .*"/"object-host": '\"redis\"'/g' ${WPCONSTCONF}
sed -ie 's/"object-port": .*"/"object-port": '\"6379\"'/g' ${WPCONSTCONF}
fi
2022-12-06 15:51:48 +08:00
THEME_PATH="${VH_DOC_ROOT}/wp-content/themes/${THEME}"
if [ ! -f ${THEME_PATH}/functions.php ]; then
cat >> "${THEME_PATH}/functions.php" <<END
<?php
require_once( WP_CONTENT_DIR.'/../wp-admin/includes/plugin.php' );
\$path = 'litespeed-cache/litespeed-cache.php' ;
if (!is_plugin_active( \$path )) {
activate_plugin( \$path ) ;
rename( __FILE__ . '.bk', __FILE__ );
}
END
elif [ ! -f ${THEME_PATH}/functions.php.bk ]; then
cp ${THEME_PATH}/functions.php ${THEME_PATH}/functions.php.bk
2020-01-09 16:59:38 -05:00
ck_ed
2022-12-06 15:51:48 +08:00
ed ${THEME_PATH}/functions.php << END >>/dev/null 2>&1
2020-01-09 16:59:38 -05:00
2i
require_once( WP_CONTENT_DIR.'/../wp-admin/includes/plugin.php' );
\$path = 'litespeed-cache/litespeed-cache.php' ;
if (!is_plugin_active( \$path )) {
activate_plugin( \$path ) ;
rename( __FILE__ . '.bk', __FILE__ );
}
.
w
q
END
fi
}
preinstall_wordpress(){
2020-01-09 12:18:41 -05:00
if [ "${VHNAME}" != '' ]; then
get_db_pass ${VHNAME}
else
get_db_pass ${DOMAIN}
fi
if [ ! -f ${VH_DOC_ROOT}/wp-config.php ] && [ -f ${VH_DOC_ROOT}/wp-config-sample.php ]; then
cp ${VH_DOC_ROOT}/wp-config-sample.php ${VH_DOC_ROOT}/wp-config.php
NEWDBPWD="define('DB_PASSWORD', '${SQL_PASS}');"
linechange 'DB_PASSWORD' ${VH_DOC_ROOT}/wp-config.php "${NEWDBPWD}"
NEWDBPWD="define('DB_USER', '${SQL_USER}');"
linechange 'DB_USER' ${VH_DOC_ROOT}/wp-config.php "${NEWDBPWD}"
NEWDBPWD="define('DB_NAME', '${SQL_DB}');"
linechange 'DB_NAME' ${VH_DOC_ROOT}/wp-config.php "${NEWDBPWD}"
2020-01-10 16:12:22 -05:00
#NEWDBPWD="define('DB_HOST', '${PUB_IP}');"
NEWDBPWD="define('DB_HOST', '${DB_HOST}');"
2020-01-09 12:18:41 -05:00
linechange 'DB_HOST' ${VH_DOC_ROOT}/wp-config.php "${NEWDBPWD}"
elif [ -f ${VH_DOC_ROOT}/wp-config.php ]; then
2020-01-08 16:49:52 -05:00
echo "${VH_DOC_ROOT}/wp-config.php already exist, exit !"
exit 1
else
2020-01-08 16:49:52 -05:00
echo 'Skip!'
exit 2
2020-01-09 16:59:38 -05:00
fi
}
2019-12-30 14:43:53 -05:00
app_wordpress_dl(){
if [ ! -f "${VH_DOC_ROOT}/wp-config.php" ] && [ ! -f "${VH_DOC_ROOT}/wp-config-sample.php" ]; then
wp core download \
--allow-root \
2020-01-09 12:18:41 -05:00
--quiet
2020-01-10 11:47:15 -05:00
else
2020-02-14 11:21:38 -05:00
echo 'wordpress already exist, abort!'
2020-01-10 11:47:15 -05:00
exit 1
fi
}
change_owner(){
2020-01-09 12:18:41 -05:00
if [ "${VHNAME}" != '' ]; then
chown -R ${WWW_UID}:${WWW_GID} ${DEFAULT_VH_ROOT}/${VHNAME}
else
chown -R ${WWW_UID}:${WWW_GID} ${DEFAULT_VH_ROOT}/${DOMAIN}
fi
2019-12-30 14:43:53 -05:00
}
main(){
2020-01-08 20:04:10 -05:00
set_vh_docroot ${DOMAIN}
2019-12-30 14:43:53 -05:00
get_owner
cd ${VH_DOC_ROOT}
if [ "${APP_NAME}" = 'wordpress' ] || [ "${APP_NAME}" = 'wp' ]; then
2020-01-08 16:49:52 -05:00
check_sql_native
app_wordpress_dl
preinstall_wordpress
2020-01-09 16:59:38 -05:00
install_wp_plugin
set_htaccess
2020-04-30 15:48:14 -04:00
get_theme_name
2020-01-09 16:59:38 -05:00
set_lscache
2020-01-10 11:47:15 -05:00
change_owner
2019-12-30 14:43:53 -05:00
exit 0
else
2020-01-08 16:49:52 -05:00
echo "APP: ${APP_NAME} not support, exit!"
2019-12-30 14:43:53 -05:00
exit 1
fi
}
2020-01-10 11:47:15 -05:00
check_input ${1}
2019-12-30 14:43:53 -05:00
while [ ! -z "${1}" ]; do
2020-01-08 16:49:52 -05:00
case ${1} in
-[hH] | -help | --help)
help_message
;;
2020-03-06 12:07:12 -05:00
-[aA] | -app | --app) shift
2020-01-08 16:49:52 -05:00
check_input "${1}"
APP_NAME="${1}"
;;
2020-03-06 12:07:12 -05:00
-[dD] | -domain | --domain) shift
2020-01-08 16:49:52 -05:00
check_input "${1}"
DOMAIN="${1}"
;;
2020-03-06 12:07:12 -05:00
-vhname | --vhname) shift
2020-01-08 16:49:52 -05:00
VHNAME="${1}"
;;
*)
help_message
;;
esac
shift
2019-12-30 14:43:53 -05:00
done
main