install.sh 27 KB

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