install.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #!/usr/bin/env bash
  2. # You can set this variable whatever you want in shell session right before running this script by issuing:
  3. # export DATA_PATH='/usr/local/etc/nginx-ui'
  4. DataPath=${DATA_PATH:-/usr/local/etc/nginx-ui}
  5. # Service Path
  6. ServicePath="/etc/systemd/system/nginx-ui.service"
  7. # Latest release version
  8. RELEASE_LATEST=''
  9. # install
  10. INSTALL='0'
  11. # remove
  12. REMOVE='0'
  13. # help
  14. HELP='0'
  15. # --local ?
  16. LOCAL_FILE=''
  17. # --proxy ?
  18. PROXY=''
  19. # --reverse-proxy ?
  20. # You can set this variable whatever you want in shell session right before running this script by issuing:
  21. # export GH_PROXY='https://cloud.nginxui.com/'
  22. RPROXY=$GH_PROXY
  23. # --purge
  24. PURGE='0'
  25. # Font color
  26. FontBlack="\033[30m";
  27. FontRed="\033[31m";
  28. FontGreen="\033[32m";
  29. FontYellow="\033[33m";
  30. FontBlue="\033[34m";
  31. FontPurple="\033[35m";
  32. FontSkyBlue="\033[36m";
  33. FontWhite="\033[37m";
  34. FontSuffix="\033[0m";
  35. curl() {
  36. $(type -P curl) -L -q --retry 5 --retry-delay 10 --retry-max-time 60 "$@"
  37. }
  38. ## Demo function for processing parameters
  39. judgment_parameters() {
  40. while [[ "$#" -gt '0' ]]; do
  41. case "$1" in
  42. 'install')
  43. INSTALL='1'
  44. ;;
  45. 'remove')
  46. REMOVE='1'
  47. ;;
  48. 'help')
  49. HELP='1'
  50. ;;
  51. '-l' | '--local')
  52. if [[ -z "$2" ]]; then
  53. echo "error: Please specify the correct local file."
  54. exit 1
  55. fi
  56. LOCAL_FILE="$2"
  57. shift
  58. ;;
  59. '-r' | '--reverse-proxy')
  60. if [[ -z "$2" ]]; then
  61. echo -e "${FontRed}error: Please specify the reverse proxy server address.${FontSuffix}"
  62. exit 1
  63. fi
  64. RPROXY="$2"
  65. shift
  66. ;;
  67. '-p' | '--proxy')
  68. if [[ -z "$2" ]]; then
  69. echo -e "${FontRed}error: Please specify the proxy server address.${FontSuffix}"
  70. exit 1
  71. fi
  72. PROXY="$2"
  73. shift
  74. ;;
  75. '--purge')
  76. PURGE='1'
  77. ;;
  78. *)
  79. echo -e "${FontRed}$0: unknown option $1${FontSuffix}"
  80. exit 1
  81. ;;
  82. esac
  83. shift
  84. done
  85. if ((INSTALL+HELP+REMOVE==0)); then
  86. INSTALL='1'
  87. elif ((INSTALL+HELP+REMOVE>1)); then
  88. echo 'You can only choose one action.'
  89. exit 1
  90. fi
  91. }
  92. cat_file_with_name() {
  93. while [[ "$#" -gt '0' ]]; do
  94. echo -e "${FontSkyBlue}# $1${FontSuffix}\n"
  95. cat "$1"
  96. echo ''
  97. shift
  98. done
  99. }
  100. systemd_cat_config() {
  101. if systemd-analyze --help | grep -qw 'cat-config'; then
  102. systemd-analyze --no-pager cat-config "$@"
  103. echo
  104. else
  105. cat_file_with_name "$@" "$1".d/*
  106. echo -e "${FontYellow}warning: The systemd version on the current operating system is too low."
  107. echo -e "${FontYellow}warning: Please consider to upgrade the systemd or the operating system.${FontSuffix}"
  108. echo
  109. fi
  110. }
  111. check_if_running_as_root() {
  112. # If you want to run as another user, please modify $EUID to be owned by this user
  113. if [[ "$EUID" -ne '0' ]]; then
  114. echo -e "${FontRed}error: You must run this script as root!${FontSuffix}"
  115. exit 1
  116. fi
  117. }
  118. identify_the_operating_system_and_architecture() {
  119. if [[ "$(uname)" == 'Linux' ]]; then
  120. case "$(uname -m)" in
  121. 'i386' | 'i686')
  122. MACHINE='32'
  123. ;;
  124. 'amd64' | 'x86_64')
  125. MACHINE='64'
  126. ;;
  127. 'armv5tel')
  128. MACHINE='arm32-v5'
  129. ;;
  130. 'armv6l')
  131. MACHINE='arm32-v6'
  132. grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
  133. ;;
  134. 'armv7' | 'armv7l')
  135. MACHINE='arm32-v7a'
  136. grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
  137. ;;
  138. 'armv8' | 'aarch64')
  139. MACHINE='arm64-v8a'
  140. ;;
  141. *)
  142. echo -e "${FontRed}error: The architecture is not supported by this script.${FontSuffix}"
  143. exit 1
  144. ;;
  145. esac
  146. if [[ ! -f '/etc/os-release' ]]; then
  147. echo -e "${FontRed}error: Don't use outdated Linux distributions.${FontSuffix}"
  148. exit 1
  149. fi
  150. # Do not combine this judgment condition with the following judgment condition.
  151. ## Be aware of Linux distribution like Gentoo, which kernel supports switch between Systemd and OpenRC.
  152. if [[ -f /.dockerenv ]] || grep -q 'docker\|lxc' /proc/1/cgroup && [[ "$(type -P systemctl)" ]]; then
  153. true
  154. elif [[ -d /run/systemd/system ]] || grep -q systemd <(ls -l /sbin/init); then
  155. true
  156. else
  157. echo -e "${FontRed}error: Only Linux distributions using systemd are supported by this script."
  158. echo -e "${FontRed}error: Please download the pre-built binary from the release page or build it manually.${FontSuffix}"
  159. exit 1
  160. fi
  161. if [[ "$(type -P apt)" ]]; then
  162. PACKAGE_MANAGEMENT_INSTALL='apt -y --no-install-recommends install'
  163. PACKAGE_MANAGEMENT_REMOVE='apt purge'
  164. elif [[ "$(type -P dnf)" ]]; then
  165. PACKAGE_MANAGEMENT_INSTALL='dnf -y install'
  166. PACKAGE_MANAGEMENT_REMOVE='dnf remove'
  167. elif [[ "$(type -P yum)" ]]; then
  168. PACKAGE_MANAGEMENT_INSTALL='yum -y install'
  169. PACKAGE_MANAGEMENT_REMOVE='yum remove'
  170. elif [[ "$(type -P zypper)" ]]; then
  171. PACKAGE_MANAGEMENT_INSTALL='zypper install -y --no-recommends'
  172. PACKAGE_MANAGEMENT_REMOVE='zypper remove'
  173. elif [[ "$(type -P pacman)" ]]; then
  174. PACKAGE_MANAGEMENT_INSTALL='pacman -Syu --noconfirm'
  175. PACKAGE_MANAGEMENT_REMOVE='pacman -Rsn'
  176. else
  177. echo -e "${FontRed}error: This script does not support the package manager in this operating system.${FontSuffix}"
  178. exit 1
  179. fi
  180. else
  181. echo -e "${FontRed}error: This operating system is not supported by this script.${FontSuffix}"
  182. exit 1
  183. fi
  184. }
  185. install_software() {
  186. package_name="$1"
  187. file_to_detect="$2"
  188. type -P "$file_to_detect" >/dev/null 2>&1 && return
  189. if ${PACKAGE_MANAGEMENT_INSTALL} "$package_name"; then
  190. echo "info: $package_name is installed."
  191. else
  192. echo -e "${FontRed}error: Installation of $package_name failed, please check your network.${FontSuffix}"
  193. exit 1
  194. fi
  195. }
  196. get_latest_version() {
  197. # Get latest release version number
  198. local latest_release
  199. if ! latest_release=$(curl -x "${PROXY}" -sS -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/0xJacky/nginx-ui/releases/latest"); then
  200. echo -e "${FontRed}error: Failed to get release list, please check your network.${FontSuffix}"
  201. exit 1
  202. fi
  203. RELEASE_LATEST="$(echo "$latest_release" | sed 'y/,/\n/' | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')"
  204. if [[ -z "$RELEASE_LATEST" ]]; then
  205. if echo "$latest_release" | grep -q "API rate limit exceeded"; then
  206. echo -e "${FontRed}error: github API rate limit exceeded${FontSuffix}"
  207. else
  208. echo -e "${FontRed}error: Failed to get the latest release version.${FontSuffix}"
  209. echo "Welcome bug report: https://github.com/0xJacky/nginx-ui/issues"
  210. fi
  211. exit 1
  212. fi
  213. RELEASE_LATEST="v${RELEASE_LATEST#v}"
  214. }
  215. download_nginx_ui() {
  216. local download_link
  217. download_link="${RPROXY}https://github.com/0xJacky/nginx-ui/releases/download/$RELEASE_LATEST/nginx-ui-linux-$MACHINE.tar.gz"
  218. echo "Downloading Nginx UI archive: $download_link"
  219. if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$TAR_FILE" "$download_link"; then
  220. echo 'error: Download failed! Please check your network or try again.'
  221. return 1
  222. fi
  223. return 0
  224. }
  225. decompression() {
  226. echo "$1"
  227. if ! tar -zxf "$1" -C "$TMP_DIRECTORY"; then
  228. echo -e "${FontRed}error: Nginx UI decompression failed.${FontSuffix}"
  229. "rm" -r "$TMP_DIRECTORY"
  230. echo "removed: $TMP_DIRECTORY"
  231. exit 1
  232. fi
  233. echo "info: Extract the Nginx UI package to $TMP_DIRECTORY and prepare it for installation."
  234. }
  235. install_bin() {
  236. NAME="nginx-ui"
  237. install -m 755 "${TMP_DIRECTORY}/$NAME" "/usr/local/bin/$NAME"
  238. }
  239. install_service() {
  240. mkdir -p '/etc/systemd/system/nginx-ui.service.d'
  241. cat > "$ServicePath" << EOF
  242. [Unit]
  243. Description=Yet another WebUI for Nginx
  244. Documentation=https://github.com/0xJacky/nginx-ui
  245. After=network.target
  246. [Service]
  247. Type=simple
  248. ExecStart=/usr/local/bin/nginx-ui -config /usr/local/etc/nginx-ui/app.ini
  249. Restart=on-failure
  250. TimeoutStopSec=5
  251. KillMode=mixed
  252. [Install]
  253. WantedBy=multi-user.target
  254. EOF
  255. chmod 644 "$ServicePath"
  256. echo "info: Systemd service files have been installed successfully!"
  257. echo -e "${FontGreen}note: The following are the actual parameters for the nginx-ui service startup."
  258. echo -e "${FontGreen}note: Please make sure the configuration file path is correctly set.${FontSuffix}"
  259. systemd_cat_config "$ServicePath"
  260. systemctl daemon-reload
  261. SYSTEMD='1'
  262. }
  263. install_config() {
  264. mkdir -p "$DataPath"
  265. if [[ ! -f "$DataPath/app.ini" ]]; then
  266. cat > "$DataPath/app.ini" << EOF
  267. [app]
  268. PageSize = 10
  269. [server]
  270. Host = 0.0.0.0
  271. Port = 9000
  272. RunMode = release
  273. [cert]
  274. HTTPChallengePort = 9180
  275. [terminal]
  276. StartCmd = login
  277. EOF
  278. echo "info: The default configuration file was installed to '$DataPath/app.ini' successfully!"
  279. fi
  280. echo -e "${FontGreen}note: The following are the current configuration for the nginx-ui."
  281. echo -e "${FontGreen}note: Please change the information if needed.${FontSuffix}"
  282. cat_file_with_name "$DataPath/app.ini"
  283. }
  284. start_nginx_ui() {
  285. if [[ -f "$ServicePath" ]]; then
  286. systemctl start nginx-ui
  287. sleep 1s
  288. if systemctl -q is-active nginx-ui; then
  289. echo 'info: Start the Nginx UI service.'
  290. else
  291. echo -e "${FontRed}error: Failed to start the Nginx UI service.${FontSuffix}"
  292. exit 1
  293. fi
  294. fi
  295. }
  296. stop_nginx_ui() {
  297. if ! systemctl stop nginx-ui; then
  298. echo -e "${FontRed}error: Failed to stop the Nginx UI service.${FontSuffix}"
  299. exit 1
  300. fi
  301. echo "info: Nginx UI service Stopped."
  302. }
  303. remove_nginx_ui() {
  304. if systemctl list-unit-files | grep -qw 'nginx-ui'; then
  305. if [[ -n "$(pidof nginx-ui)" ]]; then
  306. stop_nginx_ui
  307. fi
  308. local delete_files=('/usr/local/bin/nginx-ui' '/etc/systemd/system/nginx-ui.service' '/etc/systemd/system/nginx-ui.service.d')
  309. if [[ "$PURGE" -eq '1' ]]; then
  310. [[ -d "$DataPath" ]] && delete_files+=("$DataPath")
  311. fi
  312. systemctl disable nginx-ui
  313. if ! ("rm" -r "${delete_files[@]}"); then
  314. echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
  315. exit 1
  316. else
  317. for i in "${!delete_files[@]}"
  318. do
  319. echo "removed: ${delete_files[$i]}"
  320. done
  321. systemctl daemon-reload
  322. echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
  323. echo 'info: Nginx UI has been removed.'
  324. if [[ "$PURGE" -eq '0' ]]; then
  325. echo 'info: If necessary, manually delete the configuration and log files.'
  326. echo "info: e.g., $DataPath ..."
  327. fi
  328. exit 0
  329. fi
  330. else
  331. echo 'error: Nginx UI is not installed.'
  332. exit 1
  333. fi
  334. }
  335. # Explanation of parameters in the script
  336. show_help() {
  337. echo "usage: $0 ACTION [OPTION]..."
  338. echo
  339. echo 'ACTION:'
  340. echo ' install Install/Update Nginx UI'
  341. echo ' remove Remove Nginx UI'
  342. echo ' help Show help'
  343. echo 'If no action is specified, then install will be selected'
  344. echo
  345. echo 'OPTION:'
  346. echo ' install:'
  347. echo ' -l, --local Install Nginx UI from a local file'
  348. echo ' -p, --proxy Download through a proxy server, e.g., -p http://127.0.0.1:8118 or -p socks5://127.0.0.1:1080'
  349. echo ' -r, --reverse-proxy Download through a reverse proxy server, e.g., -r https://cloud.nginxui.com/'
  350. echo ' remove:'
  351. echo ' --purge Remove all the Nginx UI files, include logs, configs, etc'
  352. exit 0
  353. }
  354. main() {
  355. check_if_running_as_root
  356. identify_the_operating_system_and_architecture
  357. judgment_parameters "$@"
  358. # Parameter information
  359. [[ "$HELP" -eq '1' ]] && show_help
  360. [[ "$REMOVE" -eq '1' ]] && remove_nginx_ui
  361. # Important Variables
  362. TMP_DIRECTORY="$(mktemp -d)"
  363. TAR_FILE="${TMP_DIRECTORY}/nginx-ui-linux-$MACHINE.tar.gz"
  364. install_software 'curl' 'curl'
  365. # Install from a local file
  366. if [[ -n "$LOCAL_FILE" ]]; then
  367. echo "info: Install Nginx UI from a local file '$LOCAL_FILE'."
  368. decompression "$LOCAL_FILE"
  369. else
  370. get_latest_version
  371. echo "info: Installing Nginx UI $RELEASE_LATEST for $(uname -m)"
  372. if ! download_nginx_ui; then
  373. "rm" -r "$TMP_DIRECTORY"
  374. echo "removed: $TMP_DIRECTORY"
  375. exit 1
  376. fi
  377. decompression "$TAR_FILE"
  378. fi
  379. # Determine if nginx-ui is running
  380. if systemctl list-unit-files | grep -qw 'nginx-ui'; then
  381. if [[ -n "$(pidof nginx-ui)" ]]; then
  382. stop_nginx_ui
  383. NGINX_UI_RUNNING='1'
  384. fi
  385. fi
  386. install_bin
  387. echo 'installed: /usr/local/bin/nginx-ui'
  388. install_service
  389. if [[ "$SYSTEMD" -eq '1' ]]; then
  390. echo "installed: ${ServicePath}"
  391. fi
  392. "rm" -r "$TMP_DIRECTORY"
  393. echo "removed: $TMP_DIRECTORY"
  394. echo "info: Nginx UI $RELEASE_LATEST is installed."
  395. install_config
  396. if [[ "$NGINX_UI_RUNNING" -eq '1' ]]; then
  397. start_nginx_ui
  398. else
  399. systemctl start nginx-ui
  400. systemctl enable nginx-ui
  401. sleep 1s
  402. if systemctl -q is-active nginx-ui; then
  403. echo "info: Start and enable the Nginx UI service."
  404. else
  405. echo -e "${FontYellow}warning: Failed to enable and start the Nginx UI service.${FontSuffix}"
  406. fi
  407. fi
  408. }
  409. main "$@"