You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

858 lines
29 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #!/bin/bash
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  3. export PATH
  4. LANG=en_US.UTF-8
  5. Btapi_Url='http://www.example.com'
  6. Check_Api=$(curl -Ss --connect-timeout 5 -m 2 $Btapi_Url/api/SetupCount)
  7. if [ "$Check_Api" != 'ok' ];then
  8. Red_Error "此宝塔第三方云端无法连接,因此安装过程已中止!";
  9. fi
  10. if [ $(whoami) != "root" ];then
  11. echo "请使用root权限执行宝塔安装命令!"
  12. exit 1;
  13. fi
  14. is64bit=$(getconf LONG_BIT)
  15. if [ "${is64bit}" != '64' ];then
  16. Red_Error "抱歉, 当前面板版本不支持32位系统, 请使用64位系统或安装宝塔5.9!";
  17. fi
  18. Centos6Check=$(cat /etc/redhat-release | grep ' 6.' | grep -iE 'centos|Red Hat')
  19. if [ "${Centos6Check}" ];then
  20. echo "Centos6不支持安装宝塔面板,请更换Centos7/8安装宝塔面板"
  21. exit 1
  22. fi
  23. UbuntuCheck=$(cat /etc/issue|grep Ubuntu|awk '{print $2}'|cut -f 1 -d '.')
  24. if [ "${UbuntuCheck}" ] && [ "${UbuntuCheck}" -lt "16" ];then
  25. echo "Ubuntu ${UbuntuCheck}不支持安装宝塔面板,建议更换Ubuntu18/20安装宝塔面板"
  26. exit 1
  27. fi
  28. cd ~
  29. setup_path="/www"
  30. python_bin=$setup_path/server/panel/pyenv/bin/python
  31. cpu_cpunt=$(cat /proc/cpuinfo|grep processor|wc -l)
  32. if [ "$1" ];then
  33. IDC_CODE=$1
  34. fi
  35. GetSysInfo(){
  36. if [ -s "/etc/redhat-release" ];then
  37. SYS_VERSION=$(cat /etc/redhat-release)
  38. elif [ -s "/etc/issue" ]; then
  39. SYS_VERSION=$(cat /etc/issue)
  40. fi
  41. SYS_INFO=$(uname -a)
  42. SYS_BIT=$(getconf LONG_BIT)
  43. MEM_TOTAL=$(free -m|grep Mem|awk '{print $2}')
  44. CPU_INFO=$(getconf _NPROCESSORS_ONLN)
  45. echo -e ${SYS_VERSION}
  46. echo -e Bit:${SYS_BIT} Mem:${MEM_TOTAL}M Core:${CPU_INFO}
  47. echo -e ${SYS_INFO}
  48. echo -e "请截图以上报错信息发帖至论坛www.bt.cn/bbs求助"
  49. }
  50. Red_Error(){
  51. echo '=================================================';
  52. printf '\033[1;31;40m%b\033[0m\n' "$@";
  53. GetSysInfo
  54. exit 1;
  55. }
  56. Lock_Clear(){
  57. if [ -f "/etc/bt_crack.pl" ];then
  58. chattr -R -ia /www
  59. chattr -ia /etc/init.d/bt
  60. \cp -rpa /www/backup/panel/vhost/* /www/server/panel/vhost/
  61. mv /www/server/panel/BTPanel/__init__.bak /www/server/panel/BTPanel/__init__.py
  62. rm -f /etc/bt_crack.pl
  63. fi
  64. }
  65. Install_Check(){
  66. if [ "${INSTALL_FORCE}" ];then
  67. return
  68. fi
  69. echo -e "----------------------------------------------------"
  70. echo -e "检查已有其他Web/mysql环境,安装宝塔可能影响现有站点及数据"
  71. echo -e "Web/mysql service is alreday installed,Can't install panel"
  72. echo -e "----------------------------------------------------"
  73. echo -e "已知风险/Enter yes to force installation"
  74. read -p "输入yes强制安装: " yes;
  75. if [ "$yes" != "yes" ];then
  76. echo -e "------------"
  77. echo "取消安装"
  78. exit;
  79. fi
  80. INSTALL_FORCE="true"
  81. }
  82. System_Check(){
  83. MYSQLD_CHECK=$(ps -ef |grep mysqld|grep -v grep|grep -v /www/server/mysql)
  84. PHP_CHECK=$(ps -ef|grep php-fpm|grep master|grep -v /www/server/php)
  85. NGINX_CHECK=$(ps -ef|grep nginx|grep master|grep -v /www/server/nginx)
  86. HTTPD_CHECK=$(ps -ef |grep -E 'httpd|apache'|grep -v /www/server/apache|grep -v grep)
  87. if [ "${PHP_CHECK}" ] || [ "${MYSQLD_CHECK}" ] || [ "${NGINX_CHECK}" ] || [ "${HTTPD_CHECK}" ];then
  88. Install_Check
  89. fi
  90. }
  91. Get_Pack_Manager(){
  92. if [ -f "/usr/bin/yum" ] && [ -d "/etc/yum.repos.d" ]; then
  93. PM="yum"
  94. elif [ -f "/usr/bin/apt-get" ] && [ -f "/usr/bin/dpkg" ]; then
  95. PM="apt-get"
  96. fi
  97. }
  98. Auto_Swap()
  99. {
  100. swap=$(free |grep Swap|awk '{print $2}')
  101. if [ "${swap}" -gt 1 ];then
  102. echo "Swap total sizse: $swap";
  103. return;
  104. fi
  105. if [ ! -d /www ];then
  106. mkdir /www
  107. fi
  108. swapFile="/www/swap"
  109. dd if=/dev/zero of=$swapFile bs=1M count=1025
  110. mkswap -f $swapFile
  111. swapon $swapFile
  112. echo "$swapFile swap swap defaults 0 0" >> /etc/fstab
  113. swap=`free |grep Swap|awk '{print $2}'`
  114. if [ $swap -gt 1 ];then
  115. echo "Swap total sizse: $swap";
  116. return;
  117. fi
  118. sed -i "/\/www\/swap/d" /etc/fstab
  119. rm -f $swapFile
  120. }
  121. Service_Add(){
  122. if [ "${PM}" == "yum" ] || [ "${PM}" == "dnf" ]; then
  123. chkconfig --add bt
  124. chkconfig --level 2345 bt on
  125. Centos9Check=$(cat /etc/redhat-release |grep ' 9')
  126. if [ "${Centos9Check}" ];then
  127. wget -O /usr/lib/systemd/system/btpanel.service ${download_Url}/init/systemd/btpanel.service
  128. systemctl enable btpanel
  129. fi
  130. elif [ "${PM}" == "apt-get" ]; then
  131. update-rc.d bt defaults
  132. fi
  133. }
  134. Set_Centos_Repo(){
  135. HUAWEI_CHECK=$(cat /etc/motd |grep "Huawei Cloud")
  136. if [ "${HUAWEI_CHECK}" ] && [ "${is64bit}" == "64" ];then
  137. \cp -rpa /etc/yum.repos.d/ /etc/yumBak
  138. sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*.repo
  139. sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.epel.cloud|g' /etc/yum.repos.d/CentOS-*.repo
  140. rm -f /etc/yum.repos.d/epel.repo
  141. rm -f /etc/yum.repos.d/epel-*
  142. fi
  143. ALIYUN_CHECK=$(cat /etc/motd|grep "Alibaba Cloud ")
  144. if [ "${ALIYUN_CHECK}" ] && [ "${is64bit}" == "64" ] && [ ! -f "/etc/yum.repos.d/Centos-vault-8.5.2111.repo" ];then
  145. rename '.repo' '.repo.bak' /etc/yum.repos.d/*.repo
  146. wget https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo -O /etc/yum.repos.d/Centos-vault-8.5.2111.repo
  147. wget https://mirrors.aliyun.com/repo/epel-archive-8.repo -O /etc/yum.repos.d/epel-archive-8.repo
  148. sed -i 's/mirrors.cloud.aliyuncs.com/url_tmp/g' /etc/yum.repos.d/Centos-vault-8.5.2111.repo && sed -i 's/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g' /etc/yum.repos.d/Centos-vault-8.5.2111.repo && sed -i 's/url_tmp/mirrors.aliyun.com/g' /etc/yum.repos.d/Centos-vault-8.5.2111.repo
  149. sed -i 's/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g' /etc/yum.repos.d/epel-archive-8.repo
  150. fi
  151. MIRROR_CHECK=$(cat /etc/yum.repos.d/CentOS-Linux-AppStream.repo |grep "[^#]mirror.centos.org")
  152. if [ "${MIRROR_CHECK}" ] && [ "${is64bit}" == "64" ];then
  153. \cp -rpa /etc/yum.repos.d/ /etc/yumBak
  154. sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*.repo
  155. sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.epel.cloud|g' /etc/yum.repos.d/CentOS-*.repo
  156. fi
  157. }
  158. get_node_url(){
  159. if [ ! -f /bin/curl ];then
  160. if [ "${PM}" = "yum" ]; then
  161. yum install curl -y
  162. elif [ "${PM}" = "apt-get" ]; then
  163. apt-get install curl -y
  164. fi
  165. fi
  166. if [ -f "/www/node.pl" ];then
  167. download_Url=$(cat /www/node.pl)
  168. echo "Download node: $download_Url";
  169. echo '---------------------------------------------';
  170. return
  171. fi
  172. echo '---------------------------------------------';
  173. echo "Selected download node...";
  174. nodes=(http://dg2.bt.cn http://dg1.bt.cn http://125.90.93.52:5880 http://36.133.1.8:5880 http://123.129.198.197 http://38.34.185.130 http://116.213.43.206:5880 http://128.1.164.196);
  175. tmp_file1=/dev/shm/net_test1.pl
  176. tmp_file2=/dev/shm/net_test2.pl
  177. [ -f "${tmp_file1}" ] && rm -f ${tmp_file1}
  178. [ -f "${tmp_file2}" ] && rm -f ${tmp_file2}
  179. touch $tmp_file1
  180. touch $tmp_file2
  181. for node in ${nodes[@]};
  182. do
  183. NODE_CHECK=$(curl --connect-timeout 3 -m 3 2>/dev/null -w "%{http_code} %{time_total}" ${node}/net_test|xargs)
  184. RES=$(echo ${NODE_CHECK}|awk '{print $1}')
  185. NODE_STATUS=$(echo ${NODE_CHECK}|awk '{print $2}')
  186. TIME_TOTAL=$(echo ${NODE_CHECK}|awk '{print $3 * 1000 - 500 }'|cut -d '.' -f 1)
  187. if [ "${NODE_STATUS}" == "200" ];then
  188. if [ $TIME_TOTAL -lt 100 ];then
  189. if [ $RES -ge 1500 ];then
  190. echo "$RES $node" >> $tmp_file1
  191. fi
  192. else
  193. if [ $RES -ge 1500 ];then
  194. echo "$TIME_TOTAL $node" >> $tmp_file2
  195. fi
  196. fi
  197. i=$(($i+1))
  198. if [ $TIME_TOTAL -lt 100 ];then
  199. if [ $RES -ge 3000 ];then
  200. break;
  201. fi
  202. fi
  203. fi
  204. done
  205. NODE_URL=$(cat $tmp_file1|sort -r -g -t " " -k 1|head -n 1|awk '{print $2}')
  206. if [ -z "$NODE_URL" ];then
  207. NODE_URL=$(cat $tmp_file2|sort -g -t " " -k 1|head -n 1|awk '{print $2}')
  208. if [ -z "$NODE_URL" ];then
  209. NODE_URL='http://download.bt.cn';
  210. fi
  211. fi
  212. rm -f $tmp_file1
  213. rm -f $tmp_file2
  214. download_Url=$NODE_URL
  215. echo "Download node: $download_Url";
  216. echo '---------------------------------------------';
  217. }
  218. Remove_Package(){
  219. local PackageNmae=$1
  220. if [ "${PM}" == "yum" ];then
  221. isPackage=$(rpm -q ${PackageNmae}|grep "not installed")
  222. if [ -z "${isPackage}" ];then
  223. yum remove ${PackageNmae} -y
  224. fi
  225. elif [ "${PM}" == "apt-get" ];then
  226. isPackage=$(dpkg -l|grep ${PackageNmae})
  227. if [ "${PackageNmae}" ];then
  228. apt-get remove ${PackageNmae} -y
  229. fi
  230. fi
  231. }
  232. Install_RPM_Pack(){
  233. yumPath=/etc/yum.conf
  234. Centos8Check=$(cat /etc/redhat-release | grep ' 8.' | grep -iE 'centos|Red Hat')
  235. if [ "${Centos8Check}" ];then
  236. Set_Centos_Repo
  237. fi
  238. isExc=$(cat $yumPath|grep httpd)
  239. if [ "$isExc" = "" ];then
  240. echo "exclude=httpd nginx php mysql mairadb python-psutil python2-psutil" >> $yumPath
  241. fi
  242. #SYS_TYPE=$(uname -a|grep x86_64)
  243. #yumBaseUrl=$(cat /etc/yum.repos.d/CentOS-Base.repo|grep baseurl=http|cut -d '=' -f 2|cut -d '$' -f 1|head -n 1)
  244. #[ "${yumBaseUrl}" ] && checkYumRepo=$(curl --connect-timeout 5 --head -s -o /dev/null -w %{http_code} ${yumBaseUrl})
  245. #if [ "${checkYumRepo}" != "200" ] && [ "${SYS_TYPE}" ];then
  246. # curl -Ss --connect-timeout 3 -m 60 http://download.bt.cn/install/yumRepo_select.sh|bash
  247. #fi
  248. #尝试同步时间(从bt.cn)
  249. echo 'Synchronizing system time...'
  250. getBtTime=$(curl -sS --connect-timeout 3 -m 60 http://www.bt.cn/api/index/get_time)
  251. if [ "${getBtTime}" ];then
  252. date -s "$(date -d @$getBtTime +"%Y-%m-%d %H:%M:%S")"
  253. fi
  254. if [ -z "${Centos8Check}" ]; then
  255. yum install ntp -y
  256. rm -rf /etc/localtime
  257. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  258. #尝试同步国际时间(从ntp服务器)
  259. ntpdate 0.asia.pool.ntp.org
  260. setenforce 0
  261. fi
  262. startTime=`date +%s`
  263. sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
  264. #yum remove -y python-requests python3-requests python-greenlet python3-greenlet
  265. yumPacks="libcurl-devel wget tar gcc make zip unzip openssl openssl-devel gcc libxml2 libxml2-devel libxslt* zlib zlib-devel libjpeg-devel libpng-devel libwebp libwebp-devel freetype freetype-devel lsof pcre pcre-devel vixie-cron crontabs icu libicu-devel c-ares libffi-devel bzip2-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel"
  266. yum install -y ${yumPacks}
  267. for yumPack in ${yumPacks}
  268. do
  269. rpmPack=$(rpm -q ${yumPack})
  270. packCheck=$(echo ${rpmPack}|grep not)
  271. if [ "${packCheck}" ]; then
  272. yum install ${yumPack} -y
  273. fi
  274. done
  275. if [ -f "/usr/bin/dnf" ]; then
  276. dnf install -y redhat-rpm-config
  277. fi
  278. ALI_OS=$(cat /etc/redhat-release |grep "Alibaba Cloud Linux release 3")
  279. if [ -z "${ALI_OS}" ];then
  280. yum install epel-release -y
  281. fi
  282. }
  283. Install_Deb_Pack(){
  284. ln -sf bash /bin/sh
  285. apt-get update -y
  286. apt-get install ruby -y
  287. apt-get install lsb-release -y
  288. #apt-get install ntp ntpdate -y
  289. #/etc/init.d/ntp stop
  290. #update-rc.d ntp remove
  291. #cat >>~/.profile<<EOF
  292. #TZ='Asia/Shanghai'; export TZ
  293. #EOF
  294. #rm -rf /etc/localtime
  295. #cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  296. #echo 'Synchronizing system time...'
  297. #ntpdate 0.asia.pool.ntp.org
  298. #apt-get upgrade -y
  299. LIBCURL_VER=$(dpkg -l|grep libcurl4|awk '{print $3}')
  300. if [ "${LIBCURL_VER}" == "7.68.0-1ubuntu2.8" ];then
  301. apt-get remove libcurl4 -y
  302. apt-get install curl -y
  303. fi
  304. debPacks="wget curl libcurl4-openssl-dev gcc make zip unzip tar openssl libssl-dev gcc libxml2 libxml2-dev zlib1g zlib1g-dev libjpeg-dev libpng-dev lsof libpcre3 libpcre3-dev cron net-tools swig build-essential libffi-dev libbz2-dev libncurses-dev libsqlite3-dev libreadline-dev tk-dev libgdbm-dev libdb-dev libdb++-dev libpcap-dev xz-utils git";
  305. apt-get install -y $debPacks --force-yes
  306. for debPack in ${debPacks}
  307. do
  308. packCheck=$(dpkg -l ${debPack})
  309. if [ "$?" -ne "0" ] ;then
  310. apt-get install -y $debPack
  311. fi
  312. done
  313. if [ ! -d '/etc/letsencrypt' ];then
  314. mkdir -p /etc/letsencryp
  315. mkdir -p /var/spool/cron
  316. if [ ! -f '/var/spool/cron/crontabs/root' ];then
  317. echo '' > /var/spool/cron/crontabs/root
  318. chmod 600 /var/spool/cron/crontabs/root
  319. fi
  320. fi
  321. }
  322. Get_Versions(){
  323. redhat_version_file="/etc/redhat-release"
  324. deb_version_file="/etc/issue"
  325. if [ -f $redhat_version_file ];then
  326. os_type='el'
  327. is_aliyunos=$(cat $redhat_version_file|grep Aliyun)
  328. if [ "$is_aliyunos" != "" ];then
  329. return
  330. fi
  331. os_version=$(cat $redhat_version_file|grep CentOS|grep -Eo '([0-9]+\.)+[0-9]+'|grep -Eo '^[0-9]')
  332. if [ "${os_version}" = "5" ];then
  333. os_version=""
  334. fi
  335. if [ -z "${os_version}" ];then
  336. os_version=$(cat /etc/redhat-release |grep Stream|grep -oE 8)
  337. fi
  338. else
  339. os_type='ubuntu'
  340. os_version=$(cat $deb_version_file|grep Ubuntu|grep -Eo '([0-9]+\.)+[0-9]+'|grep -Eo '^[0-9]+')
  341. if [ "${os_version}" = "" ];then
  342. os_type='debian'
  343. os_version=$(cat $deb_version_file|grep Debian|grep -Eo '([0-9]+\.)+[0-9]+'|grep -Eo '[0-9]+')
  344. if [ "${os_version}" = "" ];then
  345. os_version=$(cat $deb_version_file|grep Debian|grep -Eo '[0-9]+')
  346. fi
  347. if [ "${os_version}" = "8" ];then
  348. os_version=""
  349. fi
  350. if [ "${is64bit}" = '32' ];then
  351. os_version=""
  352. fi
  353. else
  354. if [ "$os_version" = "14" ];then
  355. os_version=""
  356. fi
  357. if [ "$os_version" = "12" ];then
  358. os_version=""
  359. fi
  360. if [ "$os_version" = "19" ];then
  361. os_version=""
  362. fi
  363. if [ "$os_version" = "21" ];then
  364. os_version=""
  365. fi
  366. if [ "$os_version" = "20" ];then
  367. os_version2004=$(cat /etc/issue|grep 20.04)
  368. if [ -z "${os_version2004}" ];then
  369. os_version=""
  370. fi
  371. fi
  372. fi
  373. fi
  374. }
  375. Install_Python_Lib(){
  376. curl -Ss --connect-timeout 3 -m 60 $download_Url/install/pip_select.sh|bash
  377. pyenv_path="/www/server/panel"
  378. if [ -f $pyenv_path/pyenv/bin/python ];then
  379. is_ssl=$($python_bin -c "import ssl" 2>&1|grep cannot)
  380. $pyenv_path/pyenv/bin/python3.7 -V
  381. if [ $? -eq 0 ] && [ -z "${is_ssl}" ];then
  382. chmod -R 700 $pyenv_path/pyenv/bin
  383. is_package=$($python_bin -m psutil 2>&1|grep package)
  384. if [ "$is_package" = "" ];then
  385. wget -O $pyenv_path/pyenv/pip.txt $download_Url/install/pyenv/pip.txt -T 5
  386. $pyenv_path/pyenv/bin/pip install -U pip
  387. $pyenv_path/pyenv/bin/pip install -U setuptools
  388. $pyenv_path/pyenv/bin/pip install -r $pyenv_path/pyenv/pip.txt
  389. fi
  390. source $pyenv_path/pyenv/bin/activate
  391. chmod -R 700 $pyenv_path/pyenv/bin
  392. return
  393. else
  394. rm -rf $pyenv_path/pyenv
  395. fi
  396. fi
  397. is_loongarch64=$(uname -a|grep loongarch64)
  398. if [ "$is_loongarch64" != "" ] && [ -f "/usr/bin/yum" ];then
  399. yumPacks="python3-devel python3-pip python3-psutil python3-gevent python3-pyOpenSSL python3-paramiko python3-flask python3-rsa python3-requests python3-six python3-websocket-client"
  400. yum install -y ${yumPacks}
  401. for yumPack in ${yumPacks}
  402. do
  403. rpmPack=$(rpm -q ${yumPack})
  404. packCheck=$(echo ${rpmPack}|grep not)
  405. if [ "${packCheck}" ]; then
  406. yum install ${yumPack} -y
  407. fi
  408. done
  409. pip3 install -U pip
  410. pip3 install Pillow psutil pyinotify pycryptodome upyun oss2 pymysql qrcode qiniu redis pymongo Cython configparser cos-python-sdk-v5 supervisor gevent-websocket pyopenssl
  411. pip3 install flask==1.1.4
  412. pip3 install Pillow -U
  413. pyenv_bin=/www/server/panel/pyenv/bin
  414. mkdir -p $pyenv_bin
  415. ln -sf /usr/local/bin/pip3 $pyenv_bin/pip
  416. ln -sf /usr/local/bin/pip3 $pyenv_bin/pip3
  417. ln -sf /usr/local/bin/pip3 $pyenv_bin/pip3.7
  418. if [ -f "/usr/bin/python3.7" ];then
  419. ln -sf /usr/bin/python3.7 $pyenv_bin/python
  420. ln -sf /usr/bin/python3.7 $pyenv_bin/python3
  421. ln -sf /usr/bin/python3.7 $pyenv_bin/python3.7
  422. elif [ -f "/usr/bin/python3.6" ]; then
  423. ln -sf /usr/bin/python3.6 $pyenv_bin/python
  424. ln -sf /usr/bin/python3.6 $pyenv_bin/python3
  425. ln -sf /usr/bin/python3.6 $pyenv_bin/python3.7
  426. fi
  427. echo > $pyenv_bin/activate
  428. return
  429. fi
  430. py_version="3.7.8"
  431. mkdir -p $pyenv_path
  432. echo "True" > /www/disk.pl
  433. if [ ! -w /www/disk.pl ];then
  434. Red_Error "ERROR: Install python env fielded." "ERROR: /www目录无法写入,请检查目录/用户/磁盘权限!"
  435. fi
  436. os_type='el'
  437. os_version='7'
  438. is_export_openssl=0
  439. Get_Versions
  440. echo "OS: $os_type - $os_version"
  441. is_aarch64=$(uname -a|grep aarch64)
  442. if [ "$is_aarch64" != "" ];then
  443. is64bit="aarch64"
  444. fi
  445. if [ -f "/www/server/panel/pymake.pl" ];then
  446. os_version=""
  447. rm -f /www/server/panel/pymake.pl
  448. fi
  449. if [ "${os_version}" != "" ];then
  450. pyenv_file="/www/pyenv.tar.gz"
  451. wget -O $pyenv_file $download_Url/install/pyenv/pyenv-${os_type}${os_version}-x${is64bit}.tar.gz -T 10
  452. tmp_size=$(du -b $pyenv_file|awk '{print $1}')
  453. if [ $tmp_size -lt 703460 ];then
  454. rm -f $pyenv_file
  455. echo "ERROR: Download python env fielded."
  456. else
  457. echo "Install python env..."
  458. tar zxvf $pyenv_file -C $pyenv_path/ > /dev/null
  459. chmod -R 700 $pyenv_path/pyenv/bin
  460. if [ ! -f $pyenv_path/pyenv/bin/python ];then
  461. rm -f $pyenv_file
  462. Red_Error "ERROR: Install python env fielded." "ERROR: 下载宝塔运行环境失败,请尝试重新安装!"
  463. fi
  464. $pyenv_path/pyenv/bin/python3.7 -V
  465. if [ $? -eq 0 ];then
  466. rm -f $pyenv_file
  467. ln -sf $pyenv_path/pyenv/bin/pip3.7 /usr/bin/btpip
  468. ln -sf $pyenv_path/pyenv/bin/python3.7 /usr/bin/btpython
  469. source $pyenv_path/pyenv/bin/activate
  470. return
  471. else
  472. rm -f $pyenv_file
  473. rm -rf $pyenv_path/pyenv
  474. fi
  475. fi
  476. fi
  477. cd /www
  478. python_src='/www/python_src.tar.xz'
  479. python_src_path="/www/Python-${py_version}"
  480. wget -O $python_src $download_Url/src/Python-${py_version}.tar.xz -T 5
  481. tmp_size=$(du -b $python_src|awk '{print $1}')
  482. if [ $tmp_size -lt 10703460 ];then
  483. rm -f $python_src
  484. Red_Error "ERROR: Download python source code fielded." "ERROR: 下载宝塔运行环境失败,请尝试重新安装!"
  485. fi
  486. tar xvf $python_src
  487. rm -f $python_src
  488. cd $python_src_path
  489. ./configure --prefix=$pyenv_path/pyenv
  490. make -j$cpu_cpunt
  491. make install
  492. if [ ! -f $pyenv_path/pyenv/bin/python3.7 ];then
  493. rm -rf $python_src_path
  494. Red_Error "ERROR: Make python env fielded." "ERROR: 编译宝塔运行环境失败!"
  495. fi
  496. cd ~
  497. rm -rf $python_src_path
  498. wget -O $pyenv_path/pyenv/bin/activate $download_Url/install/pyenv/activate.panel -T 5
  499. wget -O $pyenv_path/pyenv/pip.txt $download_Url/install/pyenv/pip-3.7.8.txt -T 5
  500. ln -sf $pyenv_path/pyenv/bin/pip3.7 $pyenv_path/pyenv/bin/pip
  501. ln -sf $pyenv_path/pyenv/bin/python3.7 $pyenv_path/pyenv/bin/python
  502. ln -sf $pyenv_path/pyenv/bin/pip3.7 /usr/bin/btpip
  503. ln -sf $pyenv_path/pyenv/bin/python3.7 /usr/bin/btpython
  504. chmod -R 700 $pyenv_path/pyenv/bin
  505. $pyenv_path/pyenv/bin/pip install -U pip
  506. $pyenv_path/pyenv/bin/pip install -U setuptools
  507. $pyenv_path/pyenv/bin/pip install -U wheel==0.34.2
  508. $pyenv_path/pyenv/bin/pip install -r $pyenv_path/pyenv/pip.txt
  509. source $pyenv_path/pyenv/bin/activate
  510. is_gevent=$($python_bin -m gevent 2>&1|grep -oE package)
  511. is_psutil=$($python_bin -m psutil 2>&1|grep -oE package)
  512. if [ "${is_gevent}" != "${is_psutil}" ];then
  513. Red_Error "ERROR: psutil/gevent install failed!"
  514. fi
  515. }
  516. Install_Bt(){
  517. panelPort="8888"
  518. if [ -f ${setup_path}/server/panel/data/port.pl ];then
  519. panelPort=$(cat ${setup_path}/server/panel/data/port.pl)
  520. else
  521. RE_NUM=$(expr $RANDOM % 5)
  522. if [ "${RE_NUM}" == "1" ];then
  523. panelPort=$(expr $RANDOM % 55535 + 10000)
  524. fi
  525. fi
  526. mkdir -p ${setup_path}/server/panel/logs
  527. mkdir -p ${setup_path}/server/panel/vhost/apache
  528. mkdir -p ${setup_path}/server/panel/vhost/nginx
  529. mkdir -p ${setup_path}/server/panel/vhost/rewrite
  530. mkdir -p ${setup_path}/server/panel/install
  531. mkdir -p /www/server
  532. mkdir -p /www/wwwroot
  533. mkdir -p /www/wwwlogs
  534. mkdir -p /www/backup/database
  535. mkdir -p /www/backup/site
  536. if [ ! -d "/etc/init.d" ];then
  537. mkdir -p /etc/init.d
  538. fi
  539. if [ -f "/etc/init.d/bt" ]; then
  540. /etc/init.d/bt stop
  541. sleep 1
  542. fi
  543. wget -O /etc/init.d/bt ${download_Url}/install/src/bt6.init -T 10
  544. wget -O /www/server/panel/install/public.sh ${Btapi_Url}/install/public.sh -T 10
  545. wget -O panel.zip ${Btapi_Url}/install/src/panel6.zip -T 10
  546. if [ -f "${setup_path}/server/panel/data/default.db" ];then
  547. if [ -d "/${setup_path}/server/panel/old_data" ];then
  548. rm -rf ${setup_path}/server/panel/old_data
  549. fi
  550. mkdir -p ${setup_path}/server/panel/old_data
  551. d_format=$(date +"%Y%m%d_%H%M%S")
  552. \cp -arf ${setup_path}/server/panel/data/default.db ${setup_path}/server/panel/data/default_backup_${d_format}.db
  553. mv -f ${setup_path}/server/panel/data/default.db ${setup_path}/server/panel/old_data/default.db
  554. mv -f ${setup_path}/server/panel/data/system.db ${setup_path}/server/panel/old_data/system.db
  555. mv -f ${setup_path}/server/panel/data/port.pl ${setup_path}/server/panel/old_data/port.pl
  556. mv -f ${setup_path}/server/panel/data/admin_path.pl ${setup_path}/server/panel/old_data/admin_path.pl
  557. fi
  558. if [ ! -f "/usr/bin/unzip" ]; then
  559. if [ "${PM}" = "yum" ]; then
  560. yum install unzip -y
  561. elif [ "${PM}" = "apt-get" ]; then
  562. apt-get update
  563. apt-get install unzip -y
  564. fi
  565. fi
  566. unzip -o panel.zip -d ${setup_path}/server/ > /dev/null
  567. if [ -d "${setup_path}/server/panel/old_data" ];then
  568. mv -f ${setup_path}/server/panel/old_data/default.db ${setup_path}/server/panel/data/default.db
  569. mv -f ${setup_path}/server/panel/old_data/system.db ${setup_path}/server/panel/data/system.db
  570. mv -f ${setup_path}/server/panel/old_data/port.pl ${setup_path}/server/panel/data/port.pl
  571. mv -f ${setup_path}/server/panel/old_data/admin_path.pl ${setup_path}/server/panel/data/admin_path.pl
  572. if [ -d "/${setup_path}/server/panel/old_data" ];then
  573. rm -rf ${setup_path}/server/panel/old_data
  574. fi
  575. fi
  576. if [ ! -f ${setup_path}/server/panel/tools.py ] || [ ! -f ${setup_path}/server/panel/BT-Panel ];then
  577. ls -lh panel.zip
  578. Red_Error "ERROR: Failed to download, please try install again!" "ERROR: 下载宝塔失败,请尝试重新安装!"
  579. fi
  580. rm -f panel.zip
  581. rm -f ${setup_path}/server/panel/class/*.pyc
  582. rm -f ${setup_path}/server/panel/*.pyc
  583. chmod +x /etc/init.d/bt
  584. chmod -R 600 ${setup_path}/server/panel
  585. chmod -R +x ${setup_path}/server/panel/script
  586. ln -sf /etc/init.d/bt /usr/bin/bt
  587. echo "${panelPort}" > ${setup_path}/server/panel/data/port.pl
  588. wget -O /etc/init.d/bt ${download_Url}/install/src/bt7.init -T 10
  589. wget -O /www/server/panel/init.sh ${download_Url}/install/src/bt7.init -T 10
  590. wget -O /www/server/panel/data/softList.conf ${download_Url}/install/conf/softList.conf
  591. rm -f /www/server/panel/class/*.so
  592. if [ ! -f /www/server/panel/data/not_workorder.pl ]; then
  593. echo "True" > /www/server/panel/data/not_workorder.pl
  594. fi
  595. if [ ! -f /www/server/panel/data/userInfo.json ]; then
  596. echo "{\"uid\":1,\"username\":\"Administrator\",\"address\":\"127.0.0.1\",\"serverid\":\"1\",\"access_key\":\"test\",\"secret_key\":\"123456\",\"ukey\":\"123456\",\"state\":1}" > /www/server/panel/data/userInfo.json
  597. fi
  598. }
  599. Set_Bt_Panel(){
  600. Run_User="www"
  601. wwwUser=$(cat /etc/passwd|cut -d ":" -f 1|grep ^www$)
  602. if [ "${wwwUser}" != "www" ];then
  603. groupadd ${Run_User}
  604. useradd -s /sbin/nologin -g ${Run_User} ${Run_User}
  605. fi
  606. password=$(cat /dev/urandom | head -n 16 | md5sum | head -c 8)
  607. sleep 1
  608. admin_auth="/www/server/panel/data/admin_path.pl"
  609. if [ ! -f ${admin_auth} ];then
  610. auth_path=$(cat /dev/urandom | head -n 16 | md5sum | head -c 8)
  611. echo "/${auth_path}" > ${admin_auth}
  612. fi
  613. chmod -R 700 $pyenv_path/pyenv/bin
  614. /www/server/panel/pyenv/bin/pip3 install flask -U
  615. /www/server/panel/pyenv/bin/pip3 install flask-sock
  616. auth_path=$(cat ${admin_auth})
  617. cd ${setup_path}/server/panel/
  618. /etc/init.d/bt start
  619. $python_bin -m py_compile tools.py
  620. $python_bin tools.py username
  621. username=$($python_bin tools.py panel ${password})
  622. cd ~
  623. echo "${password}" > ${setup_path}/server/panel/default.pl
  624. chmod 600 ${setup_path}/server/panel/default.pl
  625. sleep 3
  626. /etc/init.d/bt restart
  627. sleep 3
  628. isStart=$(ps aux |grep 'BT-Panel'|grep -v grep|awk '{print $2}')
  629. LOCAL_CURL=$(curl 127.0.0.1:8888/login 2>&1 |grep -i html)
  630. if [ -z "${isStart}" ] && [ -z "${LOCAL_CURL}" ];then
  631. /etc/init.d/bt 22
  632. cd /www/server/panel/pyenv/bin
  633. touch t.pl
  634. ls -al python3.7 python
  635. lsattr python3.7 python
  636. Red_Error "ERROR: The BT-Panel service startup failed." "ERROR: 宝塔启动失败"
  637. fi
  638. }
  639. Set_Firewall(){
  640. sshPort=$(cat /etc/ssh/sshd_config | grep 'Port '|awk '{print $2}')
  641. if [ "${PM}" = "apt-get" ]; then
  642. apt-get install -y ufw
  643. if [ -f "/usr/sbin/ufw" ];then
  644. ufw allow 20/tcp
  645. ufw allow 21/tcp
  646. ufw allow 22/tcp
  647. ufw allow 80/tcp
  648. ufw allow 443/tcp
  649. ufw allow 888/tcp
  650. ufw allow ${panelPort}/tcp
  651. ufw allow ${sshPort}/tcp
  652. ufw allow 39000:40000/tcp
  653. ufw_status=`ufw status`
  654. echo y|ufw enable
  655. ufw default deny
  656. ufw reload
  657. fi
  658. else
  659. if [ -f "/etc/init.d/iptables" ];then
  660. iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 20 -j ACCEPT
  661. iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT
  662. iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  663. iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
  664. iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
  665. iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport ${panelPort} -j ACCEPT
  666. iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport ${sshPort} -j ACCEPT
  667. iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 39000:40000 -j ACCEPT
  668. #iptables -I INPUT -p tcp -m state --state NEW -m udp --dport 39000:40000 -j ACCEPT
  669. iptables -A INPUT -p icmp --icmp-type any -j ACCEPT
  670. iptables -A INPUT -s localhost -d localhost -j ACCEPT
  671. iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  672. iptables -P INPUT DROP
  673. service iptables save
  674. sed -i "s#IPTABLES_MODULES=\"\"#IPTABLES_MODULES=\"ip_conntrack_netbios_ns ip_conntrack_ftp ip_nat_ftp\"#" /etc/sysconfig/iptables-config
  675. iptables_status=$(service iptables status | grep 'not running')
  676. if [ "${iptables_status}" == '' ];then
  677. service iptables restart
  678. fi
  679. else
  680. AliyunCheck=$(cat /etc/redhat-release|grep "Aliyun Linux")
  681. [ "${AliyunCheck}" ] && return
  682. yum install firewalld -y
  683. [ "${Centos8Check}" ] && yum reinstall python3-six -y
  684. systemctl enable firewalld
  685. systemctl start firewalld
  686. firewall-cmd --set-default-zone=public > /dev/null 2>&1
  687. firewall-cmd --permanent --zone=public --add-port=20/tcp > /dev/null 2>&1
  688. firewall-cmd --permanent --zone=public --add-port=21/tcp > /dev/null 2>&1
  689. firewall-cmd --permanent --zone=public --add-port=22/tcp > /dev/null 2>&1
  690. firewall-cmd --permanent --zone=public --add-port=80/tcp > /dev/null 2>&1
  691. firewall-cmd --permanent --zone=public --add-port=443/tcp > /dev/null 2>&1
  692. firewall-cmd --permanent --zone=public --add-port=${panelPort}/tcp > /dev/null 2>&1
  693. firewall-cmd --permanent --zone=public --add-port=${sshPort}/tcp > /dev/null 2>&1
  694. firewall-cmd --permanent --zone=public --add-port=39000-40000/tcp > /dev/null 2>&1
  695. #firewall-cmd --permanent --zone=public --add-port=39000-40000/udp > /dev/null 2>&1
  696. firewall-cmd --reload
  697. fi
  698. fi
  699. }
  700. Get_Ip_Address(){
  701. getIpAddress=""
  702. getIpAddress=$(curl -sS --connect-timeout 10 -m 60 https://www.bt.cn/Api/getIpAddress)
  703. if [ -z "${getIpAddress}" ] || [ "${getIpAddress}" = "0.0.0.0" ]; then
  704. isHosts=$(cat /etc/hosts|grep 'www.bt.cn')
  705. if [ -z "${isHosts}" ];then
  706. echo "" >> /etc/hosts
  707. echo "116.213.43.206 www.bt.cn" >> /etc/hosts
  708. getIpAddress=$(curl -sS --connect-timeout 10 -m 60 https://www.bt.cn/Api/getIpAddress)
  709. if [ -z "${getIpAddress}" ];then
  710. sed -i "/bt.cn/d" /etc/hosts
  711. fi
  712. fi
  713. fi
  714. ipv4Check=$($python_bin -c "import re; print(re.match('^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$','${getIpAddress}'))")
  715. if [ "${ipv4Check}" == "None" ];then
  716. ipv6Address=$(echo ${getIpAddress}|tr -d "[]")
  717. ipv6Check=$($python_bin -c "import re; print(re.match('^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$','${ipv6Address}'))")
  718. if [ "${ipv6Check}" == "None" ]; then
  719. getIpAddress="SERVER_IP"
  720. else
  721. echo "True" > ${setup_path}/server/panel/data/ipv6.pl
  722. sleep 1
  723. /etc/init.d/bt restart
  724. fi
  725. fi
  726. if [ "${getIpAddress}" != "SERVER_IP" ];then
  727. echo "${getIpAddress}" > ${setup_path}/server/panel/data/iplist.txt
  728. fi
  729. LOCAL_IP=$(ip addr | grep -E -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -E -v "^127\.|^255\.|^0\." | head -n 1)
  730. }
  731. Setup_Count(){
  732. curl -sS --connect-timeout 10 -m 60 https://www.bt.cn/Api/SetupCount?type=Linux\&o=$1 > /dev/null 2>&1
  733. if [ "$1" != "" ];then
  734. echo $1 > /www/server/panel/data/o.pl
  735. cd /www/server/panel
  736. $python_bin tools.py o
  737. fi
  738. echo /www > /var/bt_setupPath.conf
  739. }
  740. Install_Main(){
  741. startTime=`date +%s`
  742. Lock_Clear
  743. System_Check
  744. Get_Pack_Manager
  745. get_node_url
  746. MEM_TOTAL=$(free -g|grep Mem|awk '{print $2}')
  747. if [ "${MEM_TOTAL}" -le "1" ];then
  748. Auto_Swap
  749. fi
  750. if [ "${PM}" = "yum" ]; then
  751. Install_RPM_Pack
  752. elif [ "${PM}" = "apt-get" ]; then
  753. Install_Deb_Pack
  754. fi
  755. Install_Python_Lib
  756. Install_Bt
  757. Set_Bt_Panel
  758. Service_Add
  759. Set_Firewall
  760. Get_Ip_Address
  761. Setup_Count ${IDC_CODE}
  762. }
  763. echo "
  764. +----------------------------------------------------------------------
  765. | Bt-WebPanel FOR CentOS/Ubuntu/Debian
  766. +----------------------------------------------------------------------
  767. | Copyright © 2015-2099 BT-SOFT(http://www.bt.cn) All rights reserved.
  768. +----------------------------------------------------------------------
  769. | The WebPanel URL will be http://SERVER_IP:8888 when installed.
  770. +----------------------------------------------------------------------
  771. "
  772. while [ "$go" != 'y' ] && [ "$go" != 'n' ]
  773. do
  774. read -p "Do you want to install Bt-Panel to the $setup_path directory now?(y/n): " go;
  775. done
  776. if [ "$go" == 'n' ];then
  777. exit;
  778. fi
  779. Install_Main
  780. echo > /www/server/panel/data/bind.pl
  781. echo -e "=================================================================="
  782. echo -e "\033[32mCongratulations! Installed successfully!\033[0m"
  783. echo -e "=================================================================="
  784. echo "外网面板地址: http://${getIpAddress}:${panelPort}${auth_path}"
  785. echo "内网面板地址: http://${LOCAL_IP}:${panelPort}${auth_path}"
  786. echo -e "username: $username"
  787. echo -e "password: $password"
  788. echo -e "\033[33mIf you cannot access the panel,\033[0m"
  789. echo -e "\033[33mrelease the following panel port [${panelPort}] in the security group\033[0m"
  790. echo -e "\033[33m若无法访问面板,请检查防火墙/安全组是否有放行面板[${panelPort}]端口\033[0m"
  791. echo -e "=================================================================="
  792. endTime=`date +%s`
  793. ((outTime=($endTime-$startTime)/60))
  794. echo -e "Time consumed:\033[32m $outTime \033[0mMinute!"