1
0

install.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/bin/bash
  2. red='\033[0;31m'
  3. green='\033[0;32m'
  4. yellow='\033[0;33m'
  5. plain='\033[0m'
  6. cur_dir=$(pwd)
  7. # check root
  8. [[ $EUID -ne 0 ]] && echo -e "${red}Fatal error:${plain} Please run this script with root privilege \n " && exit 1
  9. # check os
  10. if [[ -f /etc/redhat-release ]]; then
  11. release="centos"
  12. elif cat /etc/issue | grep -Eqi "debian"; then
  13. release="debian"
  14. elif cat /etc/issue | grep -Eqi "ubuntu"; then
  15. release="ubuntu"
  16. elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
  17. release="centos"
  18. elif cat /proc/version | grep -Eqi "debian"; then
  19. release="debian"
  20. elif cat /proc/version | grep -Eqi "ubuntu"; then
  21. release="ubuntu"
  22. elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
  23. release="centos"
  24. else
  25. echo -e "${red} Check system OS failed, please contact the author! ${plain}\n" && exit 1
  26. fi
  27. arch=$(arch)
  28. if [[ $arch == "x86_64" || $arch == "x64" || $arch == "amd64" ]]; then
  29. arch="amd64"
  30. elif [[ $arch == "aarch64" || $arch == "arm64" ]]; then
  31. arch="arm64"
  32. else
  33. arch="amd64"
  34. echo -e "${red} Failed to check system arch, will use default arch: ${arch}${plain}"
  35. fi
  36. echo "arch: ${arch}"
  37. if [ $(getconf WORD_BIT) != '32' ] && [ $(getconf LONG_BIT) != '64' ]; then
  38. echo "x-ui dosen't support 32-bit(x86) system, please use 64 bit operating system(x86_64) instead, if there is something wrong, please get in touch with me!"
  39. exit -1
  40. fi
  41. os_version=""
  42. # os version
  43. if [[ -f /etc/os-release ]]; then
  44. os_version=$(awk -F'[= ."]' '/VERSION_ID/{print $3}' /etc/os-release)
  45. fi
  46. if [[ -z "$os_version" && -f /etc/lsb-release ]]; then
  47. os_version=$(awk -F'[= ."]+' '/DISTRIB_RELEASE/{print $2}' /etc/lsb-release)
  48. fi
  49. if [[ x"${release}" == x"centos" ]]; then
  50. if [[ ${os_version} -le 7 ]]; then
  51. echo -e "${red} Please use CentOS 8 or higher ${plain}\n" && exit 1
  52. fi
  53. elif [[ x"${release}" == x"ubuntu" ]]; then
  54. if [[ ${os_version} -lt 20 ]]; then
  55. echo -e "${red} Please use Ubuntu 20 or higher ${plain}\n" && exit 1
  56. fi
  57. elif [[ x"${release}" == x"debian" ]]; then
  58. if [[ ${os_version} -lt 9 ]]; then
  59. echo -e "${red} Please use Debian 10 or higher ${plain}\n" && exit 1
  60. fi
  61. fi
  62. install_base() {
  63. if [[ x"${release}" == x"centos" ]]; then
  64. yum install wget curl tar -y
  65. else
  66. apt install wget curl tar -y
  67. fi
  68. }
  69. #This function will be called when user installed x-ui out of sercurity
  70. config_after_install() {
  71. echo -e "${yellow}Install/update finished! For security it's recommended to modify panel settings ${plain}"
  72. read -p "Do you want to continue with the modification [y/n]? ": config_confirm
  73. if [[ x"${config_confirm}" == x"y" || x"${config_confirm}" == x"Y" ]]; then
  74. read -p "Please set up your username:" config_account
  75. echo -e "${yellow}Your username will be:${config_account}${plain}"
  76. read -p "Please set up your password:" config_password
  77. echo -e "${yellow}Your password will be:${config_password}${plain}"
  78. read -p "Please set up the panel port:" config_port
  79. echo -e "${yellow}Your panel port is:${config_port}${plain}"
  80. echo -e "${yellow}Initializing, please wait...${plain}"
  81. /usr/local/x-ui/x-ui setting -username ${config_account} -password ${config_password}
  82. echo -e "${yellow}Account name and password set successfully!${plain}"
  83. /usr/local/x-ui/x-ui setting -port ${config_port}
  84. echo -e "${yellow}Panel port set successfully!${plain}"
  85. else
  86. echo -e "${red}cancel...${plain}"
  87. if [[ ! -f "/etc/x-ui/x-ui.db" ]]; then
  88. local usernameTemp=$(head -c 6 /dev/urandom | base64)
  89. local passwordTemp=$(head -c 6 /dev/urandom | base64)
  90. /usr/local/x-ui/x-ui setting -username ${usernameTemp} -password ${passwordTemp}
  91. echo -e "this is a fresh installation,will generate random login info for security concerns:"
  92. echo -e "###############################################"
  93. echo -e "${green}user name:${usernameTemp}${plain}"
  94. echo -e "${green}user password:${passwordTemp}${plain}"
  95. echo -e "${red}web port:${portTemp}${plain}"
  96. echo -e "###############################################"
  97. echo -e "${red}if you forgot your login info,you can type x-ui and then type 7 to check after installation${plain}"
  98. else
  99. echo -e "${red} this is your upgrade,will keep old settings,if you forgot your login info,you can type x-ui and then type 7 to check${plain}"
  100. fi
  101. fi
  102. }
  103. install_x-ui() {
  104. systemctl stop x-ui
  105. cd /usr/local/
  106. if [ $# == 0 ]; then
  107. last_version=$(curl -Ls "https://api.github.com/repos/mhsanaei/3x-ui/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
  108. if [[ ! -n "$last_version" ]]; then
  109. echo -e "${red}Failed to fetch x-ui version, it maybe due to Github API restrictions, please try it later${plain}"
  110. exit 1
  111. fi
  112. echo -e "Got x-ui latest version: ${last_version}, beginning the installation..."
  113. wget -N --no-check-certificate -O /usr/local/x-ui-linux-${arch}.tar.gz https://github.com/mhsanaei/3x-ui/releases/download/${last_version}/x-ui-linux-${arch}.tar.gz
  114. if [[ $? -ne 0 ]]; then
  115. echo -e "${red}Downloading x-ui failed, please be sure that your server can access Github ${plain}"
  116. exit 1
  117. fi
  118. else
  119. last_version=$1
  120. url="https://github.com/mhsanaei/3x-ui/releases/download/${last_version}/x-ui-linux-${arch}.tar.gz"
  121. echo -e "Begining to install x-ui $1"
  122. wget -N --no-check-certificate -O /usr/local/x-ui-linux-${arch}.tar.gz ${url}
  123. if [[ $? -ne 0 ]]; then
  124. echo -e "${red}Download x-ui $1 failed,please check the version exists${plain}"
  125. exit 1
  126. fi
  127. fi
  128. if [[ -e /usr/local/x-ui/ ]]; then
  129. rm /usr/local/x-ui/ -rf
  130. fi
  131. tar zxvf x-ui-linux-${arch}.tar.gz
  132. rm x-ui-linux-${arch}.tar.gz -f
  133. cd x-ui
  134. chmod +x x-ui bin/xray-linux-${arch}
  135. cp -f x-ui.service /etc/systemd/system/
  136. wget --no-check-certificate -O /usr/bin/x-ui https://raw.githubusercontent.com/mhsanaei/3x-ui/main/x-ui.sh
  137. chmod +x /usr/local/x-ui/x-ui.sh
  138. chmod +x /usr/bin/x-ui
  139. config_after_install
  140. #echo -e "If it is a new installation, the default web port is ${green}2053${plain}, The username and password are ${green}admin${plain} by default"
  141. #echo -e "Please make sure that this port is not occupied by other procedures,${yellow} And make sure that port 2053 has been released${plain}"
  142. # echo -e "If you want to modify the 2053 to other ports and enter the x-ui command to modify it, you must also ensure that the port you modify is also released"
  143. #echo -e ""
  144. #echo -e "If it is updated panel, access the panel in your previous way"
  145. #echo -e ""
  146. systemctl daemon-reload
  147. systemctl enable x-ui
  148. systemctl start x-ui
  149. echo -e "${green}x-ui ${last_version}${plain} installation finished, it is running now..."
  150. echo -e ""
  151. echo -e "x-ui control menu usages: "
  152. echo -e "----------------------------------------------"
  153. echo -e "x-ui - Enter Admin menu"
  154. echo -e "x-ui start - Start x-ui"
  155. echo -e "x-ui stop - Stop x-ui"
  156. echo -e "x-ui restart - Restart x-ui"
  157. echo -e "x-ui status - Show x-ui status"
  158. echo -e "x-ui enable - Enable x-ui on system startup"
  159. echo -e "x-ui disable - Disable x-ui on system startup"
  160. echo -e "x-ui log - Check x-ui logs"
  161. echo -e "x-ui update - Update x-ui"
  162. echo -e "x-ui install - Install x-ui"
  163. echo -e "x-ui uninstall - Uninstall x-ui"
  164. echo -e "----------------------------------------------"
  165. }
  166. echo -e "${green}Running...${plain}"
  167. install_base
  168. install_x-ui $1