install.sh 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. rc-service nginx-ui start
  401. sleep 1s
  402. if rc-service nginx-ui status | grep -q "started"; then
  403. echo 'info: Start the Nginx UI service.'
  404. else
  405. echo -e "${FontRed}error: Failed to start the Nginx UI service.${FontSuffix}"
  406. exit 1
  407. fi
  408. else
  409. # init.d
  410. $InitPath start
  411. sleep 1s
  412. if $InitPath status >/dev/null 2>&1; then
  413. echo 'info: Start the Nginx UI service.'
  414. else
  415. echo -e "${FontRed}error: Failed to start the Nginx UI service.${FontSuffix}"
  416. exit 1
  417. fi
  418. fi
  419. }
  420. check_nginx_ui_status() {
  421. if [[ "$SERVICE_TYPE" == "systemd" ]]; then
  422. if systemctl list-unit-files | grep -qw 'nginx-ui'; then
  423. if systemctl -q is-active nginx-ui; then
  424. return 0 # running
  425. else
  426. return 1 # not running
  427. fi
  428. else
  429. return 2 # not installed
  430. fi
  431. elif [[ "$SERVICE_TYPE" == "openrc" ]]; then
  432. if [[ -f "$OpenRCPath" ]]; then
  433. if rc-service nginx-ui status | grep -q "started"; then
  434. return 0 # running
  435. else
  436. return 1 # not running
  437. fi
  438. else
  439. return 2 # not installed
  440. fi
  441. else
  442. # init.d
  443. if [[ -f "$InitPath" ]]; then
  444. if $InitPath status >/dev/null 2>&1; then
  445. return 0 # running
  446. else
  447. return 1 # not running
  448. fi
  449. else
  450. return 2 # not installed
  451. fi
  452. fi
  453. }
  454. restart_nginx_ui() {
  455. if [[ "$SERVICE_TYPE" == "systemd" ]]; then
  456. systemctl restart nginx-ui
  457. sleep 1s
  458. if systemctl -q is-active nginx-ui; then
  459. echo 'info: Restart the Nginx UI service.'
  460. else
  461. echo -e "${FontRed}error: Failed to restart the Nginx UI service.${FontSuffix}"
  462. exit 1
  463. fi
  464. elif [[ "$SERVICE_TYPE" == "openrc" ]]; then
  465. rc-service nginx-ui restart
  466. sleep 1s
  467. if rc-service nginx-ui status | grep -q "started"; then
  468. echo 'info: Restart the Nginx UI service.'
  469. else
  470. echo -e "${FontRed}error: Failed to restart the Nginx UI service.${FontSuffix}"
  471. exit 1
  472. fi
  473. else
  474. # init.d
  475. $InitPath restart
  476. sleep 1s
  477. if $InitPath status >/dev/null 2>&1; then
  478. echo 'info: Restart the Nginx UI service.'
  479. else
  480. echo -e "${FontRed}error: Failed to restart the Nginx UI service.${FontSuffix}"
  481. exit 1
  482. fi
  483. fi
  484. }
  485. stop_nginx_ui() {
  486. if [[ "$SERVICE_TYPE" == "systemd" ]]; then
  487. if ! systemctl stop nginx-ui; then
  488. echo -e "${FontRed}error: Failed to stop the Nginx UI service.${FontSuffix}"
  489. exit 1
  490. fi
  491. elif [[ "$SERVICE_TYPE" == "openrc" ]]; then
  492. if ! rc-service nginx-ui stop; then
  493. echo -e "${FontRed}error: Failed to stop the Nginx UI service.${FontSuffix}"
  494. exit 1
  495. fi
  496. else
  497. # init.d
  498. if ! $InitPath stop; then
  499. echo -e "${FontRed}error: Failed to stop the Nginx UI service.${FontSuffix}"
  500. exit 1
  501. fi
  502. fi
  503. echo "info: Nginx UI service Stopped."
  504. }
  505. remove_nginx_ui() {
  506. if [[ "$SERVICE_TYPE" == "systemd" && $(systemctl list-unit-files | grep -qw 'nginx-ui') ]]; then
  507. if [[ -n "$(pidof nginx-ui)" ]]; then
  508. stop_nginx_ui
  509. fi
  510. delete_files="/usr/local/bin/nginx-ui /etc/systemd/system/nginx-ui.service /etc/systemd/system/nginx-ui.service.d"
  511. if [[ "$PURGE" -eq '1' ]]; then
  512. [[ -d "$DataPath" ]] && delete_files="$delete_files $DataPath"
  513. fi
  514. systemctl disable nginx-ui
  515. if ! ("rm" -r $delete_files); then
  516. echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
  517. exit 1
  518. else
  519. for file in $delete_files
  520. do
  521. echo "removed: $file"
  522. done
  523. systemctl daemon-reload
  524. echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
  525. echo 'info: Nginx UI has been removed.'
  526. if [[ "$PURGE" -eq '0' ]]; then
  527. echo 'info: If necessary, manually delete the configuration and log files.'
  528. echo "info: e.g., $DataPath ..."
  529. fi
  530. exit 0
  531. fi
  532. elif [[ "$SERVICE_TYPE" == "openrc" && -f "$OpenRCPath" ]]; then
  533. if rc-service nginx-ui status | grep -q "started"; then
  534. stop_nginx_ui
  535. fi
  536. delete_files="/usr/local/bin/nginx-ui $OpenRCPath"
  537. if [[ "$PURGE" -eq '1' ]]; then
  538. [[ -d "$DataPath" ]] && delete_files="$delete_files $DataPath"
  539. fi
  540. # Remove from runlevels
  541. rc-update del nginx-ui default
  542. if ! ("rm" -r $delete_files); then
  543. echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
  544. exit 1
  545. else
  546. for file in $delete_files
  547. do
  548. echo "removed: $file"
  549. done
  550. echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
  551. echo 'info: Nginx UI has been removed.'
  552. if [[ "$PURGE" -eq '0' ]]; then
  553. echo 'info: If necessary, manually delete the configuration and log files.'
  554. echo "info: e.g., $DataPath ..."
  555. fi
  556. exit 0
  557. fi
  558. elif [[ "$SERVICE_TYPE" == "initd" && -f "$InitPath" ]]; then
  559. if [[ -n "$(pidof nginx-ui)" ]]; then
  560. stop_nginx_ui
  561. fi
  562. delete_files="/usr/local/bin/nginx-ui $InitPath"
  563. if [[ "$PURGE" -eq '1' ]]; then
  564. [[ -d "$DataPath" ]] && delete_files="$delete_files $DataPath"
  565. fi
  566. # Remove from startup based on distro
  567. if [ -x /sbin/chkconfig ]; then
  568. /sbin/chkconfig --del nginx-ui
  569. elif [ -x /usr/sbin/update-rc.d ]; then
  570. /usr/sbin/update-rc.d -f nginx-ui remove
  571. fi
  572. if ! ("rm" -r $delete_files); then
  573. echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
  574. exit 1
  575. else
  576. for file in $delete_files
  577. do
  578. echo "removed: $file"
  579. done
  580. echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
  581. echo 'info: Nginx UI has been removed.'
  582. if [[ "$PURGE" -eq '0' ]]; then
  583. echo 'info: If necessary, manually delete the configuration and log files.'
  584. echo "info: e.g., $DataPath ..."
  585. fi
  586. exit 0
  587. fi
  588. else
  589. echo 'error: Nginx UI is not installed.'
  590. exit 1
  591. fi
  592. }
  593. # Explanation of parameters in the script
  594. show_help() {
  595. echo "usage: $0 ACTION [OPTION]..."
  596. echo
  597. echo 'ACTION:'
  598. echo ' install Install/Update Nginx UI'
  599. echo ' remove Remove Nginx UI'
  600. echo ' help Show help'
  601. echo 'If no action is specified, then install will be selected'
  602. echo
  603. echo 'OPTION:'
  604. echo ' install:'
  605. echo ' -l, --local Install Nginx UI from a local file'
  606. 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'
  607. echo ' -r, --reverse-proxy Download through a reverse proxy server, e.g., -r https://cloud.nginxui.com/'
  608. echo ' -c, --channel Specify the version channel (stable, prerelease, dev)'
  609. echo ' stable: Latest stable release (default)'
  610. echo ' prerelease: Latest prerelease version'
  611. echo ' dev: Latest development build from dev branch'
  612. echo ' remove:'
  613. echo ' --purge Remove all the Nginx UI files, include logs, configs, etc'
  614. exit 0
  615. }
  616. main() {
  617. check_if_running_as_root
  618. identify_the_operating_system_and_architecture
  619. judgment_parameters "$@"
  620. # Parameter information
  621. [[ "$HELP" -eq '1' ]] && show_help
  622. [[ "$REMOVE" -eq '1' ]] && remove_nginx_ui
  623. # Important Variables
  624. TMP_DIRECTORY="$(mktemp -d)"
  625. TAR_FILE="${TMP_DIRECTORY}/nginx-ui-linux-$MACHINE.tar.gz"
  626. # Auto install OpenRC on Alpine Linux if needed
  627. if [[ "$(type -P apk)" ]]; then
  628. install_software 'openrc' 'openrc'
  629. fi
  630. install_software 'curl' 'curl'
  631. # Install from a local file
  632. if [[ -n "$LOCAL_FILE" ]]; then
  633. echo "info: Install Nginx UI from a local file '$LOCAL_FILE'."
  634. decompression "$LOCAL_FILE"
  635. else
  636. get_latest_version
  637. echo "info: Installing Nginx UI $RELEASE_LATEST ($VERSION_CHANNEL channel) for $(uname -m)"
  638. if ! download_nginx_ui; then
  639. "rm" -r "$TMP_DIRECTORY"
  640. echo "removed: $TMP_DIRECTORY"
  641. exit 1
  642. fi
  643. decompression "$TAR_FILE"
  644. fi
  645. install_bin
  646. echo 'installed: /usr/local/bin/nginx-ui'
  647. install_service
  648. if [[ "$SERVICE_TYPE" == "systemd" && "$SYSTEMD" -eq '1' ]]; then
  649. echo "installed: ${ServicePath}"
  650. elif [[ "$SERVICE_TYPE" == "openrc" && "$OPENRC" -eq '1' ]]; then
  651. echo "installed: ${OpenRCPath}"
  652. elif [[ "$SERVICE_TYPE" == "initd" && "$INITD" -eq '1' ]]; then
  653. echo "installed: ${InitPath}"
  654. fi
  655. "rm" -r "$TMP_DIRECTORY"
  656. echo "removed: $TMP_DIRECTORY"
  657. echo "info: Nginx UI $RELEASE_LATEST is installed."
  658. install_config
  659. # Check nginx-ui service status and decide whether to start or restart
  660. check_nginx_ui_status
  661. service_status=$?
  662. if [[ $service_status -eq 0 ]]; then
  663. # Service is running, restart it
  664. echo "info: Nginx UI service is running, restarting..."
  665. restart_nginx_ui
  666. elif [[ $service_status -eq 1 ]]; then
  667. # Service is installed but not running, start it
  668. echo "info: Nginx UI service is not running, starting..."
  669. start_nginx_ui
  670. # Enable service for auto-start
  671. if [[ "$SERVICE_TYPE" == "systemd" ]]; then
  672. systemctl enable nginx-ui
  673. elif [[ "$SERVICE_TYPE" == "openrc" ]]; then
  674. rc-update add nginx-ui default
  675. fi
  676. else
  677. # Service is not installed, start it and enable
  678. echo "info: Installing and starting Nginx UI service..."
  679. if [[ "$SERVICE_TYPE" == "systemd" ]]; then
  680. systemctl start nginx-ui
  681. systemctl enable nginx-ui
  682. sleep 1s
  683. if systemctl -q is-active nginx-ui; then
  684. echo "info: Start and enable the Nginx UI service."
  685. else
  686. echo -e "${FontYellow}warning: Failed to enable and start the Nginx UI service.${FontSuffix}"
  687. fi
  688. elif [[ "$SERVICE_TYPE" == "openrc" ]]; then
  689. rc-service nginx-ui start
  690. rc-update add nginx-ui default
  691. sleep 1s
  692. if rc-service nginx-ui status | grep -q "started"; then
  693. echo "info: Started and added the Nginx UI service to default runlevel."
  694. else
  695. echo -e "${FontYellow}warning: Failed to start the Nginx UI service.${FontSuffix}"
  696. fi
  697. elif [[ "$SERVICE_TYPE" == "initd" ]]; then
  698. $InitPath start
  699. sleep 1s
  700. if $InitPath status >/dev/null 2>&1; then
  701. echo "info: Started the Nginx UI service."
  702. else
  703. echo -e "${FontYellow}warning: Failed to start the Nginx UI service.${FontSuffix}"
  704. fi
  705. fi
  706. fi
  707. }
  708. main "$@"