install.sh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. if [[ ! -d "/usr/local/bin" ]]; then
  251. mkdir -p "/usr/local/bin"
  252. fi
  253. install -m 755 "${TMP_DIRECTORY}/$NAME" "/usr/local/bin/$NAME"
  254. }
  255. install_service() {
  256. if [[ "$SERVICE_TYPE" == "systemd" ]]; then
  257. install_systemd_service
  258. elif [[ "$SERVICE_TYPE" == "openrc" ]]; then
  259. install_openrc_service
  260. else
  261. install_initd_service
  262. fi
  263. }
  264. install_systemd_service() {
  265. mkdir -p '/etc/systemd/system/nginx-ui.service.d'
  266. local service_download_link="${RPROXY}https://raw.githubusercontent.com/0xJacky/nginx-ui/main/resources/services/nginx-ui.service"
  267. echo "Downloading Nginx UI service file: $service_download_link"
  268. if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$ServicePath" "$service_download_link"; then
  269. echo -e "${FontRed}error: Download service file failed! Please check your network or try again.${FontSuffix}"
  270. return 1
  271. fi
  272. chmod 644 "$ServicePath"
  273. echo "info: Systemd service files have been installed successfully!"
  274. echo -e "${FontGreen}note: The following are the actual parameters for the nginx-ui service startup."
  275. echo -e "${FontGreen}note: Please make sure the configuration file path is correctly set.${FontSuffix}"
  276. systemd_cat_config "$ServicePath"
  277. systemctl daemon-reload
  278. SYSTEMD='1'
  279. }
  280. install_openrc_service() {
  281. local openrc_download_link="${RPROXY}https://raw.githubusercontent.com/0xJacky/nginx-ui/main/resources/services/nginx-ui.rc"
  282. echo "Downloading Nginx UI OpenRC file: $openrc_download_link"
  283. if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$OpenRCPath" "$openrc_download_link"; then
  284. echo -e "${FontRed}error: Download OpenRC file failed! Please check your network or try again.${FontSuffix}"
  285. return 1
  286. fi
  287. chmod 755 "$OpenRCPath"
  288. echo "info: OpenRC service file has been installed successfully!"
  289. echo -e "${FontGreen}note: The OpenRC service is installed to '$OpenRCPath'.${FontSuffix}"
  290. cat_file_with_name "$OpenRCPath"
  291. # Add to default runlevel
  292. rc-update add nginx-ui default
  293. OPENRC='1'
  294. }
  295. install_initd_service() {
  296. # Download init.d script
  297. local initd_download_link="${RPROXY}https://raw.githubusercontent.com/0xJacky/nginx-ui/main/resources/services/nginx-ui.init"
  298. echo "Downloading Nginx UI init.d file: $initd_download_link"
  299. if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$InitPath" "$initd_download_link"; then
  300. echo -e "${FontRed}error: Download init.d file failed! Please check your network or try again.${FontSuffix}"
  301. exit 1
  302. fi
  303. chmod 755 "$InitPath"
  304. echo "info: Init.d service file has been installed successfully!"
  305. echo -e "${FontGreen}note: The init.d service is installed to '$InitPath'.${FontSuffix}"
  306. cat_file_with_name "$InitPath"
  307. # Add service to startup based on distro
  308. if [ -x /sbin/chkconfig ]; then
  309. /sbin/chkconfig --add nginx-ui
  310. elif [ -x /usr/sbin/update-rc.d ]; then
  311. /usr/sbin/update-rc.d nginx-ui defaults
  312. fi
  313. INITD='1'
  314. }
  315. install_config() {
  316. mkdir -p "$DataPath"
  317. if [[ ! -f "$DataPath/app.ini" ]]; then
  318. cat > "$DataPath/app.ini" << EOF
  319. [app]
  320. PageSize = 10
  321. [server]
  322. Host = 0.0.0.0
  323. Port = 9000
  324. RunMode = release
  325. [cert]
  326. HTTPChallengePort = 9180
  327. [terminal]
  328. StartCmd = login
  329. EOF
  330. echo "info: The default configuration file was installed to '$DataPath/app.ini' successfully!"
  331. fi
  332. echo -e "${FontGreen}note: The following are the current configuration for the nginx-ui."
  333. echo -e "${FontGreen}note: Please change the information if needed.${FontSuffix}"
  334. cat_file_with_name "$DataPath/app.ini"
  335. }
  336. start_nginx_ui() {
  337. if [[ "$SERVICE_TYPE" == "systemd" ]]; then
  338. systemctl start nginx-ui
  339. sleep 1s
  340. if systemctl -q is-active nginx-ui; then
  341. echo 'info: Start the Nginx UI service.'
  342. else
  343. echo -e "${FontRed}error: Failed to start the Nginx UI service.${FontSuffix}"
  344. exit 1
  345. fi
  346. elif [[ "$SERVICE_TYPE" == "openrc" ]]; then
  347. rc-service nginx-ui start
  348. sleep 1s
  349. if rc-service nginx-ui status | grep -q "started"; then
  350. echo 'info: Start the Nginx UI service.'
  351. else
  352. echo -e "${FontRed}error: Failed to start the Nginx UI service.${FontSuffix}"
  353. exit 1
  354. fi
  355. else
  356. # init.d
  357. $InitPath start
  358. sleep 1s
  359. if $InitPath status >/dev/null 2>&1; then
  360. echo 'info: Start the Nginx UI service.'
  361. else
  362. echo -e "${FontRed}error: Failed to start the Nginx UI service.${FontSuffix}"
  363. exit 1
  364. fi
  365. fi
  366. }
  367. stop_nginx_ui() {
  368. if [[ "$SERVICE_TYPE" == "systemd" ]]; then
  369. if ! systemctl stop nginx-ui; then
  370. echo -e "${FontRed}error: Failed to stop the Nginx UI service.${FontSuffix}"
  371. exit 1
  372. fi
  373. elif [[ "$SERVICE_TYPE" == "openrc" ]]; then
  374. if ! rc-service nginx-ui stop; then
  375. echo -e "${FontRed}error: Failed to stop the Nginx UI service.${FontSuffix}"
  376. exit 1
  377. fi
  378. else
  379. # init.d
  380. if ! $InitPath stop; then
  381. echo -e "${FontRed}error: Failed to stop the Nginx UI service.${FontSuffix}"
  382. exit 1
  383. fi
  384. fi
  385. echo "info: Nginx UI service Stopped."
  386. }
  387. remove_nginx_ui() {
  388. if [[ "$SERVICE_TYPE" == "systemd" && $(systemctl list-unit-files | grep -qw 'nginx-ui') ]]; then
  389. if [[ -n "$(pidof nginx-ui)" ]]; then
  390. stop_nginx_ui
  391. fi
  392. local delete_files=('/usr/local/bin/nginx-ui' '/etc/systemd/system/nginx-ui.service' '/etc/systemd/system/nginx-ui.service.d')
  393. if [[ "$PURGE" -eq '1' ]]; then
  394. [[ -d "$DataPath" ]] && delete_files+=("$DataPath")
  395. fi
  396. systemctl disable nginx-ui
  397. if ! ("rm" -r "${delete_files[@]}"); then
  398. echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
  399. exit 1
  400. else
  401. for i in "${!delete_files[@]}"
  402. do
  403. echo "removed: ${delete_files[$i]}"
  404. done
  405. systemctl daemon-reload
  406. echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
  407. echo 'info: Nginx UI has been removed.'
  408. if [[ "$PURGE" -eq '0' ]]; then
  409. echo 'info: If necessary, manually delete the configuration and log files.'
  410. echo "info: e.g., $DataPath ..."
  411. fi
  412. exit 0
  413. fi
  414. elif [[ "$SERVICE_TYPE" == "openrc" && -f "$OpenRCPath" ]]; then
  415. if rc-service nginx-ui status | grep -q "started"; then
  416. stop_nginx_ui
  417. fi
  418. local delete_files=('/usr/local/bin/nginx-ui' "$OpenRCPath")
  419. if [[ "$PURGE" -eq '1' ]]; then
  420. [[ -d "$DataPath" ]] && delete_files+=("$DataPath")
  421. fi
  422. # Remove from runlevels
  423. rc-update del nginx-ui default
  424. if ! ("rm" -r "${delete_files[@]}"); then
  425. echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
  426. exit 1
  427. else
  428. for i in "${!delete_files[@]}"
  429. do
  430. echo "removed: ${delete_files[$i]}"
  431. done
  432. echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
  433. echo 'info: Nginx UI has been removed.'
  434. if [[ "$PURGE" -eq '0' ]]; then
  435. echo 'info: If necessary, manually delete the configuration and log files.'
  436. echo "info: e.g., $DataPath ..."
  437. fi
  438. exit 0
  439. fi
  440. elif [[ "$SERVICE_TYPE" == "initd" && -f "$InitPath" ]]; then
  441. if [[ -n "$(pidof nginx-ui)" ]]; then
  442. stop_nginx_ui
  443. fi
  444. local delete_files=('/usr/local/bin/nginx-ui' "$InitPath")
  445. if [[ "$PURGE" -eq '1' ]]; then
  446. [[ -d "$DataPath" ]] && delete_files+=("$DataPath")
  447. fi
  448. # Remove from startup based on distro
  449. if [ -x /sbin/chkconfig ]; then
  450. /sbin/chkconfig --del nginx-ui
  451. elif [ -x /usr/sbin/update-rc.d ]; then
  452. /usr/sbin/update-rc.d -f nginx-ui remove
  453. fi
  454. if ! ("rm" -r "${delete_files[@]}"); then
  455. echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
  456. exit 1
  457. else
  458. for i in "${!delete_files[@]}"
  459. do
  460. echo "removed: ${delete_files[$i]}"
  461. done
  462. echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
  463. echo 'info: Nginx UI has been removed.'
  464. if [[ "$PURGE" -eq '0' ]]; then
  465. echo 'info: If necessary, manually delete the configuration and log files.'
  466. echo "info: e.g., $DataPath ..."
  467. fi
  468. exit 0
  469. fi
  470. else
  471. echo 'error: Nginx UI is not installed.'
  472. exit 1
  473. fi
  474. }
  475. # Explanation of parameters in the script
  476. show_help() {
  477. echo "usage: $0 ACTION [OPTION]..."
  478. echo
  479. echo 'ACTION:'
  480. echo ' install Install/Update Nginx UI'
  481. echo ' remove Remove Nginx UI'
  482. echo ' help Show help'
  483. echo 'If no action is specified, then install will be selected'
  484. echo
  485. echo 'OPTION:'
  486. echo ' install:'
  487. echo ' -l, --local Install Nginx UI from a local file'
  488. 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'
  489. echo ' -r, --reverse-proxy Download through a reverse proxy server, e.g., -r https://cloud.nginxui.com/'
  490. echo ' remove:'
  491. echo ' --purge Remove all the Nginx UI files, include logs, configs, etc'
  492. exit 0
  493. }
  494. main() {
  495. check_if_running_as_root
  496. identify_the_operating_system_and_architecture
  497. judgment_parameters "$@"
  498. # Parameter information
  499. [[ "$HELP" -eq '1' ]] && show_help
  500. [[ "$REMOVE" -eq '1' ]] && remove_nginx_ui
  501. # Important Variables
  502. TMP_DIRECTORY="$(mktemp -d)"
  503. TAR_FILE="${TMP_DIRECTORY}/nginx-ui-linux-$MACHINE.tar.gz"
  504. install_software 'curl' 'curl'
  505. # Install from a local file
  506. if [[ -n "$LOCAL_FILE" ]]; then
  507. echo "info: Install Nginx UI from a local file '$LOCAL_FILE'."
  508. decompression "$LOCAL_FILE"
  509. else
  510. get_latest_version
  511. echo "info: Installing Nginx UI $RELEASE_LATEST for $(uname -m)"
  512. if ! download_nginx_ui; then
  513. "rm" -r "$TMP_DIRECTORY"
  514. echo "removed: $TMP_DIRECTORY"
  515. exit 1
  516. fi
  517. decompression "$TAR_FILE"
  518. fi
  519. # Determine if nginx-ui is running
  520. NGINX_UI_RUNNING='0'
  521. if [[ "$SERVICE_TYPE" == "systemd" && $(systemctl list-unit-files | grep -qw 'nginx-ui') ]]; then
  522. if [[ -n "$(pidof nginx-ui)" ]]; then
  523. stop_nginx_ui
  524. NGINX_UI_RUNNING='1'
  525. fi
  526. elif [[ "$SERVICE_TYPE" == "openrc" && -f "$OpenRCPath" ]]; then
  527. if rc-service nginx-ui status | grep -q "started"; then
  528. stop_nginx_ui
  529. NGINX_UI_RUNNING='1'
  530. fi
  531. elif [[ "$SERVICE_TYPE" == "initd" && -f "$InitPath" ]]; then
  532. if [[ -n "$(pidof nginx-ui)" ]]; then
  533. stop_nginx_ui
  534. NGINX_UI_RUNNING='1'
  535. fi
  536. fi
  537. install_bin
  538. echo 'installed: /usr/local/bin/nginx-ui'
  539. install_service
  540. if [[ "$SERVICE_TYPE" == "systemd" && "$SYSTEMD" -eq '1' ]]; then
  541. echo "installed: ${ServicePath}"
  542. elif [[ "$SERVICE_TYPE" == "openrc" && "$OPENRC" -eq '1' ]]; then
  543. echo "installed: ${OpenRCPath}"
  544. elif [[ "$SERVICE_TYPE" == "initd" && "$INITD" -eq '1' ]]; then
  545. echo "installed: ${InitPath}"
  546. fi
  547. "rm" -r "$TMP_DIRECTORY"
  548. echo "removed: $TMP_DIRECTORY"
  549. echo "info: Nginx UI $RELEASE_LATEST is installed."
  550. install_config
  551. if [[ "$NGINX_UI_RUNNING" -eq '1' ]]; then
  552. start_nginx_ui
  553. else
  554. if [[ "$SERVICE_TYPE" == "systemd" ]]; then
  555. systemctl start nginx-ui
  556. systemctl enable nginx-ui
  557. sleep 1s
  558. if systemctl -q is-active nginx-ui; then
  559. echo "info: Start and enable the Nginx UI service."
  560. else
  561. echo -e "${FontYellow}warning: Failed to enable and start the Nginx UI service.${FontSuffix}"
  562. fi
  563. elif [[ "$SERVICE_TYPE" == "openrc" ]]; then
  564. rc-service nginx-ui start
  565. rc-update add nginx-ui default
  566. sleep 1s
  567. if rc-service nginx-ui status | grep -q "running"; then
  568. echo "info: Started and added the Nginx UI service to default runlevel."
  569. else
  570. echo -e "${FontYellow}warning: Failed to start the Nginx UI service.${FontSuffix}"
  571. fi
  572. elif [[ "$SERVICE_TYPE" == "initd" ]]; then
  573. $InitPath start
  574. sleep 1s
  575. if $InitPath status >/dev/null 2>&1; then
  576. echo "info: Started the Nginx UI service."
  577. else
  578. echo -e "${FontYellow}warning: Failed to start the Nginx UI service.${FontSuffix}"
  579. fi
  580. fi
  581. fi
  582. }
  583. main "$@"