install.sh 25 KB

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