1
0

install.sh 21 KB

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