dvlyadmin_pro/backend/install.sh
2025-03-17 18:06:54 +08:00

145 lines
4.9 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
#快速安装项目所需python虚拟环境和requrements.txt依赖
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
LANG=en_US.UTF-8
if [ $(whoami) != "root" ];then
echo "请使用root权限执行本启动命令"
exit 1;
fi
# 获取当前脚本文件的目录(一定要放到项目根目录中)
PROJECT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
#PROJECT_ROOT=$(python3 -c "import os; print(os.path.abspath(os.path.dirname('$0')))")
echo -e "============================================"
echo -e "检查项目python虚拟环境是否存在"
echo -e "!!!若安装异常请手动删除venv虚拟环境目录!!!"
echo -e "============================================"
py_path="/usr/local/lyadmin"
Return_Error(){
echo '=================================================';
printf '\033[1;31;40m%b\033[0m\n' "$@";
exit 1;
}
# 参数:$1 - 要逐个显示的文本
# $2 - 控制字符输出速度的暂停时间(可选,默认为 0.2 秒)
Show_Text_With_Ellipsis() {
text="$1"
pause=${2:-0.1}
echo -n "$text"
sleep 1 # 显示 "这是一个例子" 1秒钟
for ((i=0; i<3; i++)); do
echo -n "."
sleep $pause
done
echo ""
}
Install_Openssl111() {
if command -v openssl >/dev/null 2>&1; then
openssl_version=$(openssl version | awk '{print $2}')
if [ "$(printf '%s\n' "1.1.1" "$openssl_version" | sort -V | head -n1)" = "1.1.1" ]; then
echo "检测到OpenSSL版本为${openssl_version},无需再安装"
return
fi
fi
opensslVersion="1.1.1o"
cd /tmp
wget http://download.bt.cn/src/openssl-${opensslVersion}.tar.gz
tar -zxf openssl-${opensslVersion}.tar.gz
rm -f openssl-${opensslVersion}.tar.gz
cd /tmp/openssl-${opensslVersion}
./config --prefix=/usr/local/openssl111 zlib-dynamic
make -j${cpuCore} -C /tmp/openssl-${opensslVersion}/
make install -C /tmp/openssl-${opensslVersion}/
echo "/usr/local/openssl111/lib" >>/etc/ld.so.conf.d/openssl111.conf
ldconfig
ldconfig /lib64
cd ..
rm -rf openssl-${opensslVersion}
}
Install_Python(){
py_version="3.12.0"
if [ -f ${py_path}/python/bin/python3 ];then
return
fi
mkdir -p ${py_path}
echo "True" > ${py_path}/disk.ly
if [ ! -w ${py_path}/disk.ly ];then
Return_Error "ERROR: Install python fielded." "ERROR: /usr/local/lyadmin目录无法写入请检查目录/用户/磁盘权限!"
fi
cd ${py_path}
py_file="${py_path}/pythonsrc.tar.xz"
wget -O ${py_file} http://download.bt.cn/src/Python-${py_version}.tar.xz
echo "Install python ..."
tar -xJf ${py_file} > /dev/null
mv Python-${py_version} pythonsrc
if [ ${py_version:2:2} -ge 10 ]; then
if command -v openssl >/dev/null 2>&1; then
openssl_version=$(openssl version | awk '{print $2}')
if [ "$(printf '%s\n' "1.1.1" "$openssl_version" | sort -V | head -n1)" = "1.1.1" ]; then
Show_Text_With_Ellipsis "检测到OpenSSL版本为${openssl_version}正在使用此环境" 0.3
sys_openssl_path=$(which openssl)
sys_openssl_dir=$(dirname "$openssl_path")
WITH_SSL="--with-openssl=${sys_openssl_dir}"
else
Show_Text_With_Ellipsis "检测到OpenSSL版本为${openssl_version}正在安装支持Python${py_version}版本的openssl" 0.3
Install_Openssl111
WITH_SSL="--with-openssl=/usr/local/openssl111"
fi
else
Show_Text_With_Ellipsis "检测到OpenSSL未安装正在安装支持Python${py_version}版本的openssl" 0.3
Install_Openssl111
WITH_SSL="--with-openssl=/usr/local/openssl111"
fi
cd ${py_path}/pythonsrc
./configure --prefix=${py_path}/python ${WITH_SSL}
else
cd ${py_path}/pythonsrc
./configure --prefix=${py_path}/python
fi
cd ${py_path}/pythonsrc
make
make install
if [ ! -f ${py_path}/python/bin/python3 ];then
rm -f ${py_file}
Return_Error "ERROR: Install python fielded." "ERROR: 下载python环境失败请尝试重新安装"
fi
${py_path}/python/bin/python3 -V
if [ $? -eq 0 ];then
rm -f ${py_file}
rm -rf ${py_path}/pythonsrc
ln -sf ${py_path}/python/bin/pip3 /usr/bin/lypip
ln -sf ${py_path}/python/bin/python3 /usr/bin/lypython
# source $py_path/python/bin/activate
return
else
rm -f ${py_file}
rm -rf ${py_path}/python
rm -rf ${py_path}/pythonsrc
fi
}
if [ -d "${PROJECT_ROOT}/venv" ]; then
echo "Python 虚拟环境已安装"
exit 1
else
echo "Python 虚拟环境未安装"
Install_Python
cd ${PROJECT_ROOT}
# 创建虚拟环境
${py_path}/python/bin/python3 -m venv ${PROJECT_ROOT}/venv
echo "Python 虚拟环境安装完成"
# 激活虚拟环境
source ${PROJECT_ROOT}/venv/bin/activate
# 安装 Django (可选)
pip install -r ${PROJECT_ROOT}/requirements_linux.txt -i https://mirrors.aliyun.com/pypi/simple/
echo "requrements.txt依赖安装完成"
fi