install.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/bin/env bash
  2. # Data Path
  3. DataPath=/usr/local/etc/nginx-ui
  4. # Bin Path
  5. # BinPath=/usr/local/bin/nginx-ui
  6. # Service Path
  7. ServicePath=/usr/lib/systemd/system/nginx-ui.service
  8. PROXY=""
  9. ## Demo function for processing parameters
  10. judgment_parameters() {
  11. while [[ "$#" -gt '0' ]]; do
  12. case "$1" in
  13. '-p' | '--proxy')
  14. if [[ -z "$2" ]]; then
  15. echo "error: Please specify the proxy server address."
  16. exit 1
  17. fi
  18. PROXY="$2"
  19. shift
  20. ;;
  21. esac
  22. shift
  23. done
  24. }
  25. check_if_running_as_root() {
  26. # If you want to run as another user, please modify $EUID to be owned by this user
  27. if [[ "$EUID" -ne '0' ]]; then
  28. echo "error: You must run this script as root!"
  29. exit 1
  30. fi
  31. }
  32. identify_the_operating_system_and_architecture() {
  33. if [[ "$(uname)" == 'Linux' ]]; then
  34. case "$(uname -m)" in
  35. 'i386' | 'i686')
  36. MACHINE='386'
  37. ;;
  38. 'amd64' | 'x86_64')
  39. MACHINE='amd64'
  40. ;;
  41. 'armv8' | 'aarch64')
  42. MACHINE='arm64'
  43. ;;
  44. *)
  45. echo "error: The architecture is not supported."
  46. exit 1
  47. ;;
  48. esac
  49. if [[ ! -f '/etc/os-release' ]]; then
  50. echo "error: Don't use outdated Linux distributions."
  51. exit 1
  52. fi
  53. # Do not combine this judgment condition with the following judgment condition.
  54. ## Be aware of Linux distribution like Gentoo, which kernel supports switch between Systemd and OpenRC.
  55. if [[ -f /.dockerenv ]] || grep -q 'docker\|lxc' /proc/1/cgroup && [[ "$(type -P systemctl)" ]]; then
  56. true
  57. elif [[ -d /run/systemd/system ]] || grep -q systemd <(ls -l /sbin/init); then
  58. true
  59. else
  60. echo "error: Only Linux distributions using systemd are supported."
  61. exit 1
  62. fi
  63. if [[ "$(type -P apt)" ]]; then
  64. PACKAGE_MANAGEMENT_INSTALL='apt -y --no-install-recommends install'
  65. # PACKAGE_MANAGEMENT_REMOVE='apt purge'
  66. elif [[ "$(type -P dnf)" ]]; then
  67. PACKAGE_MANAGEMENT_INSTALL='dnf -y install'
  68. # PACKAGE_MANAGEMENT_REMOVE='dnf remove'
  69. elif [[ "$(type -P yum)" ]]; then
  70. PACKAGE_MANAGEMENT_INSTALL='yum -y install'
  71. # PACKAGE_MANAGEMENT_REMOVE='yum remove'
  72. elif [[ "$(type -P zypper)" ]]; then
  73. PACKAGE_MANAGEMENT_INSTALL='zypper install -y --no-recommends'
  74. # PACKAGE_MANAGEMENT_REMOVE='zypper remove'
  75. elif [[ "$(type -P pacman)" ]]; then
  76. PACKAGE_MANAGEMENT_INSTALL='pacman -Syu --noconfirm'
  77. # PACKAGE_MANAGEMENT_REMOVE='pacman -Rsn'
  78. else
  79. echo "error: The script does not support the package manager in this operating system."
  80. exit 1
  81. fi
  82. else
  83. echo "error: This operating system is not supported."
  84. exit 1
  85. fi
  86. }
  87. install_software() {
  88. package_name="$1"
  89. file_to_detect="$2"
  90. type -P "$file_to_detect" >/dev/null 2>&1 && return
  91. if ${PACKAGE_MANAGEMENT_INSTALL} "$package_name"; then
  92. echo "info: $package_name is installed."
  93. else
  94. echo "error: Installation of $package_name failed, please check your network."
  95. exit 1
  96. fi
  97. }
  98. download() {
  99. LATEST_RELEASE=$(curl -L -s --insecure -H 'Accept: application/json' "$PROXY"https://api.github.com/repos/0xJacky/nginx-ui/releases/latest)
  100. # shellcheck disable=SC2001
  101. LATEST_VERSION=$(echo "$LATEST_RELEASE" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
  102. DOWNLOAD_LINK=$PROXY"https://github.com/0xJacky/nginx-ui/releases/download/$LATEST_VERSION/nginx-ui-linux-$MACHINE.tar.gz"
  103. echo "Downloading NginxUI archive: $DOWNLOAD_LINK"
  104. if ! curl --insecure -R -H 'Cache-Control: no-cache' -L "$DOWNLOAD_LINK" > "$TAR_FILE"; then
  105. echo 'error: Download failed! Please check your network or try again.'
  106. return 1
  107. fi
  108. return 0
  109. }
  110. decompression() {
  111. echo "$1"
  112. if ! tar -zxvf "$1" -C "$TMP_DIRECTORY"; then
  113. echo 'error: Nginx UI decompression failed.'
  114. "rm" -r "$TMP_DIRECTORY"
  115. echo "removed: $TMP_DIRECTORY"
  116. exit 1
  117. fi
  118. echo "info: Extract the Nginx UI package to $TMP_DIRECTORY and prepare it for installation."
  119. }
  120. install_bin() {
  121. NAME="nginx-ui"
  122. install -m 755 "${TMP_DIRECTORY}/$NAME" "/usr/local/bin/$NAME"
  123. }
  124. install_service() {
  125. cat > "$ServicePath" << EOF
  126. [Unit]
  127. Description=Yet another WebUI for Nginx
  128. Documentation=https://github.com/0xJacky/nginx-ui
  129. After=network.target
  130. [Service]
  131. Type=simple
  132. ExecStart=/usr/local/bin/nginx-ui --config /usr/local/etc/nginx-ui/app.ini
  133. Restart=on-failure
  134. TimeoutStopSec=5
  135. KillMode=mixed
  136. [Install]
  137. WantedBy=multi-user.target
  138. EOF
  139. chmod 644 ServicePath
  140. }
  141. start_nginx_ui() {
  142. if [[ -f ServicePath ]]; then
  143. systemctl start nginx-ui
  144. sleep 1s
  145. if systemctl -q is-active nginx-ui; then
  146. echo 'info: Start the Nginx UI service.'
  147. else
  148. echo 'error: Failed to start the Nginx UI service.'
  149. exit 1
  150. fi
  151. fi
  152. }
  153. stop_nginx_ui() {
  154. if ! systemctl stop nginx-ui; then
  155. echo 'error: Failed to stop the Nginx UI service.'
  156. exit 1
  157. fi
  158. echo 'info: Nginx UI service Stopped.'
  159. }
  160. main() {
  161. check_if_running_as_root
  162. judgment_parameters "$@"
  163. # TMP
  164. TMP_DIRECTORY="$(mktemp -d)"
  165. # Tar
  166. TAR_FILE="${TMP_DIRECTORY}/nginx-ui-linux-$MACHINE.tar.gz"
  167. identify_the_operating_system_and_architecture
  168. install_software 'curl' 'curl'
  169. download
  170. decompression "$TAR_FILE"
  171. install_bin
  172. install_service
  173. mkdir DataPath
  174. start_nginx_ui
  175. stop_nginx_ui
  176. systemctl start nginx-ui
  177. systemctl enable nginx-ui
  178. sleep 1s
  179. if systemctl -q is-active nginx-ui; then
  180. echo "info: Start and enable the Nginx UI service."
  181. else
  182. echo "warning: Failed to enable and start the Nginx UI service."
  183. fi
  184. }
  185. main "$@"