install.sh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. # Init.d Path
  8. InitPath="/etc/init.d/nginx-ui"
  9. # OpenRC Path
  10. OpenRCPath="/etc/init.d/nginx-ui"
  11. # Service Type (systemd, openrc, initd)
  12. SERVICE_TYPE=''
  13. # Latest release version
  14. RELEASE_LATEST=''
  15. # install
  16. INSTALL='0'
  17. # remove
  18. REMOVE='0'
  19. # help
  20. HELP='0'
  21. # --local ?
  22. LOCAL_FILE=''
  23. # --proxy ?
  24. PROXY=''
  25. # --reverse-proxy ?
  26. # You can set this variable whatever you want in shell session right before running this script by issuing:
  27. # export GH_PROXY='https://cloud.nginxui.com/'
  28. RPROXY=$GH_PROXY
  29. # --purge
  30. PURGE='0'
  31. # Font color
  32. FontBlack="\033[30m";
  33. FontRed="\033[31m";
  34. FontGreen="\033[32m";
  35. FontYellow="\033[33m";
  36. FontBlue="\033[34m";
  37. FontPurple="\033[35m";
  38. FontSkyBlue="\033[36m";
  39. FontWhite="\033[37m";
  40. FontSuffix="\033[0m";
  41. curl() {
  42. $(type -P curl) -L -q --retry 5 --retry-delay 10 --retry-max-time 60 "$@"
  43. }
  44. ## Demo function for processing parameters
  45. judgment_parameters() {
  46. while [[ "$#" -gt '0' ]]; do
  47. case "$1" in
  48. 'install')
  49. INSTALL='1'
  50. ;;
  51. 'remove')
  52. REMOVE='1'
  53. ;;
  54. 'help')
  55. HELP='1'
  56. ;;
  57. '-l' | '--local')
  58. if [[ -z "$2" ]]; then
  59. echo "error: Please specify the correct local file."
  60. exit 1
  61. fi
  62. LOCAL_FILE="$2"
  63. shift
  64. ;;
  65. '-r' | '--reverse-proxy')
  66. if [[ -z "$2" ]]; then
  67. echo -e "${FontRed}error: Please specify the reverse proxy server address.${FontSuffix}"
  68. exit 1
  69. fi
  70. RPROXY="$2"
  71. shift
  72. ;;
  73. '-p' | '--proxy')
  74. if [[ -z "$2" ]]; then
  75. echo -e "${FontRed}error: Please specify the proxy server address.${FontSuffix}"
  76. exit 1
  77. fi
  78. PROXY="$2"
  79. shift
  80. ;;
  81. '--purge')
  82. PURGE='1'
  83. ;;
  84. *)
  85. echo -e "${FontRed}$0: unknown option $1${FontSuffix}"
  86. exit 1
  87. ;;
  88. esac
  89. shift
  90. done
  91. if ((INSTALL+HELP+REMOVE==0)); then
  92. INSTALL='1'
  93. elif ((INSTALL+HELP+REMOVE>1)); then
  94. echo 'You can only choose one action.'
  95. exit 1
  96. fi
  97. }
  98. cat_file_with_name() {
  99. while [[ "$#" -gt '0' ]]; do
  100. echo -e "${FontSkyBlue}# $1${FontSuffix}\n"
  101. cat "$1"
  102. echo ''
  103. shift
  104. done
  105. }
  106. systemd_cat_config() {
  107. if systemd-analyze --help | grep -qw 'cat-config'; then
  108. systemd-analyze --no-pager cat-config "$@"
  109. echo
  110. else
  111. cat_file_with_name "$@" "$1".d/*
  112. echo -e "${FontYellow}warning: The systemd version on the current operating system is too low."
  113. echo -e "${FontYellow}warning: Please consider to upgrade the systemd or the operating system.${FontSuffix}"
  114. echo
  115. fi
  116. }
  117. check_if_running_as_root() {
  118. # If you want to run as another user, please modify $EUID to be owned by this user
  119. if [[ "$EUID" -ne '0' ]]; then
  120. echo -e "${FontRed}error: You must run this script as root!${FontSuffix}"
  121. exit 1
  122. fi
  123. }
  124. identify_the_operating_system_and_architecture() {
  125. if [[ "$(uname)" == 'Linux' ]]; then
  126. case "$(uname -m)" in
  127. 'i386' | 'i686')
  128. MACHINE='32'
  129. ;;
  130. 'amd64' | 'x86_64')
  131. MACHINE='64'
  132. ;;
  133. 'armv5tel')
  134. MACHINE='arm32-v5'
  135. ;;
  136. 'armv6l')
  137. MACHINE='arm32-v6'
  138. grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
  139. ;;
  140. 'armv7' | 'armv7l')
  141. MACHINE='arm32-v7a'
  142. grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
  143. ;;
  144. 'armv8' | 'aarch64')
  145. MACHINE='arm64-v8a'
  146. ;;
  147. *)
  148. echo -e "${FontRed}error: The architecture is not supported by this script.${FontSuffix}"
  149. exit 1
  150. ;;
  151. esac
  152. if [[ ! -f '/etc/os-release' ]]; then
  153. echo -e "${FontRed}error: Don't use outdated Linux distributions.${FontSuffix}"
  154. exit 1
  155. fi
  156. # Do not combine this judgment condition with the following judgment condition.
  157. ## Be aware of Linux distribution like Gentoo, which kernel supports switch between Systemd and OpenRC.
  158. if [[ -f /.dockerenv ]] || grep -q 'docker\|lxc' /proc/1/cgroup && [[ "$(type -P systemctl)" ]]; then
  159. SERVICE_TYPE='systemd'
  160. elif [[ -d /run/systemd/system ]] || grep -q systemd <(ls -l /sbin/init); then
  161. SERVICE_TYPE='systemd'
  162. elif [[ "$(type -P rc-update)" ]]; then
  163. SERVICE_TYPE='openrc'
  164. else
  165. SERVICE_TYPE='initd'
  166. echo -e "${FontYellow}warning: No systemd or OpenRC detected, falling back to init.d.${FontSuffix}"
  167. fi
  168. if [[ "$(type -P apt)" ]]; then
  169. PACKAGE_MANAGEMENT_INSTALL='apt -y --no-install-recommends install'
  170. PACKAGE_MANAGEMENT_REMOVE='apt purge'
  171. elif [[ "$(type -P dnf)" ]]; then
  172. PACKAGE_MANAGEMENT_INSTALL='dnf -y install'
  173. PACKAGE_MANAGEMENT_REMOVE='dnf remove'
  174. elif [[ "$(type -P yum)" ]]; then
  175. PACKAGE_MANAGEMENT_INSTALL='yum -y install'
  176. PACKAGE_MANAGEMENT_REMOVE='yum remove'
  177. elif [[ "$(type -P zypper)" ]]; then
  178. PACKAGE_MANAGEMENT_INSTALL='zypper install -y --no-recommends'
  179. PACKAGE_MANAGEMENT_REMOVE='zypper remove'
  180. elif [[ "$(type -P pacman)" ]]; then
  181. PACKAGE_MANAGEMENT_INSTALL='pacman -Syu --noconfirm'
  182. PACKAGE_MANAGEMENT_REMOVE='pacman -Rsn'
  183. elif [[ "$(type -P opkg)" ]]; then
  184. PACKAGE_MANAGEMENT_INSTALL='opkg install'
  185. PACKAGE_MANAGEMENT_REMOVE='opkg remove'
  186. elif [[ "$(type -P apk)" ]]; then
  187. PACKAGE_MANAGEMENT_INSTALL='apk add --no-cache'
  188. PACKAGE_MANAGEMENT_REMOVE='apk del'
  189. else
  190. echo -e "${FontRed}error: This script does not support the package manager in this operating system.${FontSuffix}"
  191. exit 1
  192. fi
  193. else
  194. echo -e "${FontRed}error: This operating system is not supported by this script.${FontSuffix}"
  195. exit 1
  196. fi
  197. }
  198. install_software() {
  199. package_name="$1"
  200. file_to_detect="$2"
  201. type -P "$file_to_detect" >/dev/null 2>&1 && return
  202. if ${PACKAGE_MANAGEMENT_INSTALL} "$package_name"; then
  203. echo "info: $package_name is installed."
  204. else
  205. echo -e "${FontRed}error: Installation of $package_name failed, please check your network.${FontSuffix}"
  206. exit 1
  207. fi
  208. }
  209. get_latest_version() {
  210. # Get latest release version number
  211. local latest_release
  212. 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
  213. echo -e "${FontRed}error: Failed to get release list, please check your network.${FontSuffix}"
  214. exit 1
  215. fi
  216. RELEASE_LATEST="$(echo "$latest_release" | sed 'y/,/\n/' | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')"
  217. if [[ -z "$RELEASE_LATEST" ]]; then
  218. if echo "$latest_release" | grep -q "API rate limit exceeded"; then
  219. echo -e "${FontRed}error: github API rate limit exceeded${FontSuffix}"
  220. else
  221. echo -e "${FontRed}error: Failed to get the latest release version.${FontSuffix}"
  222. echo "Welcome bug report: https://github.com/0xJacky/nginx-ui/issues"
  223. fi
  224. exit 1
  225. fi
  226. RELEASE_LATEST="v${RELEASE_LATEST#v}"
  227. }
  228. download_nginx_ui() {
  229. local download_link
  230. download_link="${RPROXY}https://github.com/0xJacky/nginx-ui/releases/download/$RELEASE_LATEST/nginx-ui-linux-$MACHINE.tar.gz"
  231. echo "Downloading Nginx UI archive: $download_link"
  232. if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$TAR_FILE" "$download_link"; then
  233. echo 'error: Download failed! Please check your network or try again.'
  234. return 1
  235. fi
  236. return 0
  237. }
  238. decompression() {
  239. echo "$1"
  240. if ! tar -zxf "$1" -C "$TMP_DIRECTORY"; then
  241. echo -e "${FontRed}error: Nginx UI decompression failed.${FontSuffix}"
  242. "rm" -r "$TMP_DIRECTORY"
  243. echo "removed: $TMP_DIRECTORY"
  244. exit 1
  245. fi
  246. echo "info: Extract the Nginx UI package to $TMP_DIRECTORY and prepare it for installation."
  247. }
  248. install_bin() {
  249. NAME="nginx-ui"
  250. install -m 755 "${TMP_DIRECTORY}/$NAME" "/usr/local/bin/$NAME"
  251. }
  252. install_service() {
  253. if [[ "$SERVICE_TYPE" == "systemd" ]]; then
  254. install_systemd_service
  255. elif [[ "$SERVICE_TYPE" == "openrc" ]]; then
  256. install_openrc_service
  257. else
  258. install_initd_service
  259. fi
  260. }
  261. install_systemd_service() {
  262. mkdir -p '/etc/systemd/system/nginx-ui.service.d'
  263. local service_download_link="${RPROXY}https://raw.githubusercontent.com/0xJacky/nginx-ui/main/resources/services/nginx-ui.service"
  264. echo "Downloading Nginx UI service file: $service_download_link"
  265. if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$ServicePath" "$service_download_link"; then
  266. echo -e "${FontRed}error: Download service file failed! Please check your network or try again.${FontSuffix}"
  267. return 1
  268. fi
  269. chmod 644 "$ServicePath"
  270. echo "info: Systemd service files have been installed successfully!"
  271. echo -e "${FontGreen}note: The following are the actual parameters for the nginx-ui service startup."
  272. echo -e "${FontGreen}note: Please make sure the configuration file path is correctly set.${FontSuffix}"
  273. systemd_cat_config "$ServicePath"
  274. systemctl daemon-reload
  275. SYSTEMD='1'
  276. }
  277. install_openrc_service() {
  278. local openrc_download_link="${RPROXY}https://raw.githubusercontent.com/0xJacky/nginx-ui/main/resources/services/nginx-ui.rc"
  279. echo "Downloading Nginx UI OpenRC file: $openrc_download_link"
  280. if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$OpenRCPath" "$openrc_download_link"; then
  281. echo -e "${FontRed}error: Download OpenRC file failed! Please check your network or try again.${FontSuffix}"
  282. return 1
  283. fi
  284. chmod 755 "$OpenRCPath"
  285. echo "info: OpenRC service file has been installed successfully!"
  286. echo -e "${FontGreen}note: The OpenRC service is installed to '$OpenRCPath'.${FontSuffix}"
  287. cat_file_with_name "$OpenRCPath"
  288. # Add to default runlevel
  289. rc-update add nginx-ui default
  290. OPENRC='1'
  291. }
  292. install_initd_service() {
  293. # Download init.d script
  294. local initd_download_link="${RPROXY}https://raw.githubusercontent.com/0xJacky/nginx-ui/main/resources/services/nginx-ui.init"
  295. echo "Downloading Nginx UI init.d file: $initd_download_link"
  296. if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$InitPath" "$initd_download_link"; then
  297. echo -e "${FontRed}error: Download init.d file failed! Please check your network or try again.${FontSuffix}"
  298. exit 1
  299. fi
  300. chmod 755 "$InitPath"
  301. echo "info: Init.d service file has been installed successfully!"
  302. echo -e "${FontGreen}note: The init.d service is installed to '$InitPath'.${FontSuffix}"
  303. cat_file_with_name "$InitPath"
  304. # Add service to startup based on distro
  305. if [ -x /sbin/chkconfig ]; then
  306. /sbin/chkconfig --add nginx-ui
  307. elif [ -x /usr/sbin/update-rc.d ]; then
  308. /usr/sbin/update-rc.d nginx-ui defaults
  309. fi
  310. INITD='1'
  311. }
  312. install_config() {
  313. mkdir -p "$DataPath"
  314. if [[ ! -f "$DataPath/app.ini" ]]; then
  315. cat > "$DataPath/app.ini" << EOF
  316. [app]
  317. PageSize = 10
  318. [server]
  319. Host = 0.0.0.0
  320. Port = 9000
  321. RunMode = release
  322. [cert]
  323. HTTPChallengePort = 9180
  324. [terminal]
  325. StartCmd = login
  326. EOF
  327. echo "info: The default configuration file was installed to '$DataPath/app.ini' successfully!"
  328. fi
  329. echo -e "${FontGreen}note: The following are the current configuration for the nginx-ui."
  330. echo -e "${FontGreen}note: Please change the information if needed.${FontSuffix}"
  331. cat_file_with_name "$DataPath/app.ini"
  332. }
  333. start_nginx_ui() {
  334. if [[ "$SERVICE_TYPE" == "systemd" ]]; then
  335. systemctl start nginx-ui
  336. sleep 1s
  337. if systemctl -q is-active nginx-ui; then
  338. echo 'info: Start the Nginx UI service.'
  339. else
  340. echo -e "${FontRed}error: Failed to start the Nginx UI service.${FontSuffix}"
  341. exit 1
  342. fi
  343. elif [[ "$SERVICE_TYPE" == "openrc" ]]; then
  344. rc-service nginx-ui start
  345. sleep 1s
  346. if rc-service nginx-ui status | grep -q "started"; then
  347. echo 'info: Start the Nginx UI service.'
  348. else
  349. echo -e "${FontRed}error: Failed to start the Nginx UI service.${FontSuffix}"
  350. exit 1
  351. fi
  352. else
  353. # init.d
  354. $InitPath start
  355. sleep 1s
  356. if $InitPath status >/dev/null 2>&1; then
  357. echo 'info: Start the Nginx UI service.'
  358. else
  359. echo -e "${FontRed}error: Failed to start the Nginx UI service.${FontSuffix}"
  360. exit 1
  361. fi
  362. fi
  363. }
  364. stop_nginx_ui() {
  365. if [[ "$SERVICE_TYPE" == "systemd" ]]; then
  366. if ! systemctl stop nginx-ui; then
  367. echo -e "${FontRed}error: Failed to stop the Nginx UI service.${FontSuffix}"
  368. exit 1
  369. fi
  370. elif [[ "$SERVICE_TYPE" == "openrc" ]]; then
  371. if ! rc-service nginx-ui stop; then
  372. echo -e "${FontRed}error: Failed to stop the Nginx UI service.${FontSuffix}"
  373. exit 1
  374. fi
  375. else
  376. # init.d
  377. if ! $InitPath stop; then
  378. echo -e "${FontRed}error: Failed to stop the Nginx UI service.${FontSuffix}"
  379. exit 1
  380. fi
  381. fi
  382. echo "info: Nginx UI service Stopped."
  383. }
  384. remove_nginx_ui() {
  385. if [[ "$SERVICE_TYPE" == "systemd" && $(systemctl list-unit-files | grep -qw 'nginx-ui') ]]; then
  386. if [[ -n "$(pidof nginx-ui)" ]]; then
  387. stop_nginx_ui
  388. fi
  389. local delete_files=('/usr/local/bin/nginx-ui' '/etc/systemd/system/nginx-ui.service' '/etc/systemd/system/nginx-ui.service.d')
  390. if [[ "$PURGE" -eq '1' ]]; then
  391. [[ -d "$DataPath" ]] && delete_files+=("$DataPath")
  392. fi
  393. systemctl disable nginx-ui
  394. if ! ("rm" -r "${delete_files[@]}"); then
  395. echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
  396. exit 1
  397. else
  398. for i in "${!delete_files[@]}"
  399. do
  400. echo "removed: ${delete_files[$i]}"
  401. done
  402. systemctl daemon-reload
  403. echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
  404. echo 'info: Nginx UI has been removed.'
  405. if [[ "$PURGE" -eq '0' ]]; then
  406. echo 'info: If necessary, manually delete the configuration and log files.'
  407. echo "info: e.g., $DataPath ..."
  408. fi
  409. exit 0
  410. fi
  411. elif [[ "$SERVICE_TYPE" == "openrc" && -f "$OpenRCPath" ]]; then
  412. if rc-service nginx-ui status | grep -q "started"; then
  413. stop_nginx_ui
  414. fi
  415. local delete_files=('/usr/local/bin/nginx-ui' "$OpenRCPath")
  416. if [[ "$PURGE" -eq '1' ]]; then
  417. [[ -d "$DataPath" ]] && delete_files+=("$DataPath")
  418. fi
  419. # Remove from runlevels
  420. rc-update del nginx-ui default
  421. if ! ("rm" -r "${delete_files[@]}"); then
  422. echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
  423. exit 1
  424. else
  425. for i in "${!delete_files[@]}"
  426. do
  427. echo "removed: ${delete_files[$i]}"
  428. done
  429. echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
  430. echo 'info: Nginx UI has been removed.'
  431. if [[ "$PURGE" -eq '0' ]]; then
  432. echo 'info: If necessary, manually delete the configuration and log files.'
  433. echo "info: e.g., $DataPath ..."
  434. fi
  435. exit 0
  436. fi
  437. elif [[ "$SERVICE_TYPE" == "initd" && -f "$InitPath" ]]; then
  438. if [[ -n "$(pidof nginx-ui)" ]]; then
  439. stop_nginx_ui
  440. fi
  441. local delete_files=('/usr/local/bin/nginx-ui' "$InitPath")
  442. if [[ "$PURGE" -eq '1' ]]; then
  443. [[ -d "$DataPath" ]] && delete_files+=("$DataPath")
  444. fi
  445. # Remove from startup based on distro
  446. if [ -x /sbin/chkconfig ]; then
  447. /sbin/chkconfig --del nginx-ui
  448. elif [ -x /usr/sbin/update-rc.d ]; then
  449. /usr/sbin/update-rc.d -f nginx-ui remove
  450. fi
  451. if ! ("rm" -r "${delete_files[@]}"); then
  452. echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
  453. exit 1
  454. else
  455. for i in "${!delete_files[@]}"
  456. do
  457. echo "removed: ${delete_files[$i]}"
  458. done
  459. echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
  460. echo 'info: Nginx UI has been removed.'
  461. if [[ "$PURGE" -eq '0' ]]; then
  462. echo 'info: If necessary, manually delete the configuration and log files.'
  463. echo "info: e.g., $DataPath ..."
  464. fi
  465. exit 0
  466. fi
  467. else
  468. echo 'error: Nginx UI is not installed.'
  469. exit 1
  470. fi
  471. }
  472. # Explanation of parameters in the script
  473. show_help() {
  474. echo "usage: $0 ACTION [OPTION]..."
  475. echo
  476. echo 'ACTION:'
  477. echo ' install Install/Update Nginx UI'
  478. echo ' remove Remove Nginx UI'
  479. echo ' help Show help'
  480. echo 'If no action is specified, then install will be selected'
  481. echo
  482. echo 'OPTION:'
  483. echo ' install:'
  484. echo ' -l, --local Install Nginx UI from a local file'
  485. 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'
  486. echo ' -r, --reverse-proxy Download through a reverse proxy server, e.g., -r https://cloud.nginxui.com/'
  487. echo ' remove:'
  488. echo ' --purge Remove all the Nginx UI files, include logs, configs, etc'
  489. exit 0
  490. }
  491. main() {
  492. check_if_running_as_root
  493. identify_the_operating_system_and_architecture
  494. judgment_parameters "$@"
  495. # Parameter information
  496. [[ "$HELP" -eq '1' ]] && show_help
  497. [[ "$REMOVE" -eq '1' ]] && remove_nginx_ui
  498. # Important Variables
  499. TMP_DIRECTORY="$(mktemp -d)"
  500. TAR_FILE="${TMP_DIRECTORY}/nginx-ui-linux-$MACHINE.tar.gz"
  501. install_software 'curl' 'curl'
  502. # Install from a local file
  503. if [[ -n "$LOCAL_FILE" ]]; then
  504. echo "info: Install Nginx UI from a local file '$LOCAL_FILE'."
  505. decompression "$LOCAL_FILE"
  506. else
  507. get_latest_version
  508. echo "info: Installing Nginx UI $RELEASE_LATEST for $(uname -m)"
  509. if ! download_nginx_ui; then
  510. "rm" -r "$TMP_DIRECTORY"
  511. echo "removed: $TMP_DIRECTORY"
  512. exit 1
  513. fi
  514. decompression "$TAR_FILE"
  515. fi
  516. # Determine if nginx-ui is running
  517. NGINX_UI_RUNNING='0'
  518. if [[ "$SERVICE_TYPE" == "systemd" && $(systemctl list-unit-files | grep -qw 'nginx-ui') ]]; then
  519. if [[ -n "$(pidof nginx-ui)" ]]; then
  520. stop_nginx_ui
  521. NGINX_UI_RUNNING='1'
  522. fi
  523. elif [[ "$SERVICE_TYPE" == "openrc" && -f "$OpenRCPath" ]]; then
  524. if rc-service nginx-ui status | grep -q "started"; then
  525. stop_nginx_ui
  526. NGINX_UI_RUNNING='1'
  527. fi
  528. elif [[ "$SERVICE_TYPE" == "initd" && -f "$InitPath" ]]; then
  529. if [[ -n "$(pidof nginx-ui)" ]]; then
  530. stop_nginx_ui
  531. NGINX_UI_RUNNING='1'
  532. fi
  533. fi
  534. install_bin
  535. echo 'installed: /usr/local/bin/nginx-ui'
  536. install_service
  537. if [[ "$SERVICE_TYPE" == "systemd" && "$SYSTEMD" -eq '1' ]]; then
  538. echo "installed: ${ServicePath}"
  539. elif [[ "$SERVICE_TYPE" == "openrc" && "$OPENRC" -eq '1' ]]; then
  540. echo "installed: ${OpenRCPath}"
  541. elif [[ "$SERVICE_TYPE" == "initd" && "$INITD" -eq '1' ]]; then
  542. echo "installed: ${InitPath}"
  543. fi
  544. "rm" -r "$TMP_DIRECTORY"
  545. echo "removed: $TMP_DIRECTORY"
  546. echo "info: Nginx UI $RELEASE_LATEST is installed."
  547. install_config
  548. if [[ "$NGINX_UI_RUNNING" -eq '1' ]]; then
  549. start_nginx_ui
  550. else
  551. if [[ "$SERVICE_TYPE" == "systemd" ]]; then
  552. systemctl start nginx-ui
  553. systemctl enable nginx-ui
  554. sleep 1s
  555. if systemctl -q is-active nginx-ui; then
  556. echo "info: Start and enable the Nginx UI service."
  557. else
  558. echo -e "${FontYellow}warning: Failed to enable and start the Nginx UI service.${FontSuffix}"
  559. fi
  560. elif [[ "$SERVICE_TYPE" == "openrc" ]]; then
  561. rc-service nginx-ui start
  562. rc-update add nginx-ui default
  563. sleep 1s
  564. if rc-service nginx-ui status | grep -q "started"; then
  565. echo "info: Started and added the Nginx UI service to default runlevel."
  566. else
  567. echo -e "${FontYellow}warning: Failed to start the Nginx UI service.${FontSuffix}"
  568. fi
  569. elif [[ "$SERVICE_TYPE" == "initd" ]]; then
  570. $InitPath start
  571. sleep 1s
  572. if $InitPath status >/dev/null 2>&1; then
  573. echo "info: Started the Nginx UI service."
  574. else
  575. echo -e "${FontYellow}warning: Failed to start the Nginx UI service.${FontSuffix}"
  576. fi
  577. fi
  578. fi
  579. }
  580. main "$@"