install.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. # Latest release version
  8. RELEASE_LATEST=''
  9. # install
  10. INSTALL='0'
  11. # remove
  12. REMOVE='0'
  13. # help
  14. HELP='0'
  15. # --local ?
  16. LOCAL_FILE=''
  17. # --proxy ?
  18. PROXY=''
  19. # --reverse-proxy ?
  20. RPROXY=""
  21. # --purge
  22. PURGE='0'
  23. # Font color
  24. FontBlack="\033[30m";
  25. FontRed="\033[31m";
  26. FontGreen="\033[32m";
  27. FontYellow="\033[33m";
  28. FontBlue="\033[34m";
  29. FontPurple="\033[35m";
  30. FontSkyBlue="\033[36m";
  31. FontWhite="\033[37m";
  32. FontSuffix="\033[0m";
  33. curl() {
  34. $(type -P curl) -L -q --retry 5 --retry-delay 10 --retry-max-time 60 "$@"
  35. }
  36. ## Demo function for processing parameters
  37. judgment_parameters() {
  38. while [[ "$#" -gt '0' ]]; do
  39. case "$1" in
  40. 'install')
  41. INSTALL='1'
  42. ;;
  43. 'remove')
  44. REMOVE='1'
  45. ;;
  46. 'help')
  47. HELP='1'
  48. ;;
  49. '-l' | '--local')
  50. if [[ -z "$2" ]]; then
  51. echo "error: Please specify the correct local file."
  52. exit 1
  53. fi
  54. LOCAL_FILE="$2"
  55. shift
  56. ;;
  57. '-r' | '--reverse-proxy')
  58. if [[ -z "$2" ]]; then
  59. echo -e "${FontRed}error: Please specify the reverse proxy server address.${FontSuffix}"
  60. exit 1
  61. fi
  62. RPROXY="$2"
  63. shift
  64. ;;
  65. '-p' | '--proxy')
  66. if [[ -z "$2" ]]; then
  67. echo -e "${FontRed}error: Please specify the proxy server address.${FontSuffix}"
  68. exit 1
  69. fi
  70. PROXY="$2"
  71. shift
  72. ;;
  73. '--purge')
  74. PURGE='1'
  75. ;;
  76. *)
  77. echo -e "${FontRed}$0: unknown option $1${FontSuffix}"
  78. exit 1
  79. ;;
  80. esac
  81. shift
  82. done
  83. if ((INSTALL+HELP+REMOVE==0)); then
  84. INSTALL='1'
  85. elif ((INSTALL+HELP+REMOVE>1)); then
  86. echo 'You can only choose one action.'
  87. exit 1
  88. fi
  89. }
  90. cat_file_with_name() {
  91. while [[ "$#" -gt '0' ]]; do
  92. echo -e "${FontSkyBlue}# $1${FontSuffix}\n"
  93. cat "$1"
  94. echo ''
  95. shift
  96. done
  97. }
  98. systemd_cat_config() {
  99. if systemd-analyze --help | grep -qw 'cat-config'; then
  100. systemd-analyze --no-pager cat-config "$@"
  101. echo
  102. else
  103. cat_file_with_name "$@" "$1".d/*
  104. echo -e "${FontYellow}warning: The systemd version on the current operating system is too low."
  105. echo -e "${FontYellow}warning: Please consider to upgrade the systemd or the operating system.${FontSuffix}"
  106. echo
  107. fi
  108. }
  109. check_if_running_as_root() {
  110. # If you want to run as another user, please modify $EUID to be owned by this user
  111. if [[ "$EUID" -ne '0' ]]; then
  112. echo -e "${FontRed}error: You must run this script as root!${FontSuffix}"
  113. exit 1
  114. fi
  115. }
  116. identify_the_operating_system_and_architecture() {
  117. if [[ "$(uname)" == 'Linux' ]]; then
  118. case "$(uname -m)" in
  119. 'i386' | 'i686')
  120. MACHINE='32'
  121. ;;
  122. 'amd64' | 'x86_64')
  123. MACHINE='64'
  124. ;;
  125. 'armv5tel')
  126. MACHINE='arm32-v5'
  127. ;;
  128. 'armv6l')
  129. MACHINE='arm32-v6'
  130. grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
  131. ;;
  132. 'armv7' | 'armv7l')
  133. MACHINE='arm32-v7a'
  134. grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
  135. ;;
  136. 'armv8' | 'aarch64')
  137. MACHINE='arm64-v8a'
  138. ;;
  139. *)
  140. echo -e "${FontRed}error: The architecture is not supported.${FontSuffix}"
  141. exit 1
  142. ;;
  143. esac
  144. if [[ ! -f '/etc/os-release' ]]; then
  145. echo -e "${FontRed}error: Don't use outdated Linux distributions.${FontSuffix}"
  146. exit 1
  147. fi
  148. # Do not combine this judgment condition with the following judgment condition.
  149. ## Be aware of Linux distribution like Gentoo, which kernel supports switch between Systemd and OpenRC.
  150. if [[ -f /.dockerenv ]] || grep -q 'docker\|lxc' /proc/1/cgroup && [[ "$(type -P systemctl)" ]]; then
  151. true
  152. elif [[ -d /run/systemd/system ]] || grep -q systemd <(ls -l /sbin/init); then
  153. true
  154. else
  155. echo -e "${FontRed}error: Only Linux distributions using systemd are supported.${FontSuffix}"
  156. exit 1
  157. fi
  158. if [[ "$(type -P apt)" ]]; then
  159. PACKAGE_MANAGEMENT_INSTALL='apt -y --no-install-recommends install'
  160. PACKAGE_MANAGEMENT_REMOVE='apt purge'
  161. elif [[ "$(type -P dnf)" ]]; then
  162. PACKAGE_MANAGEMENT_INSTALL='dnf -y install'
  163. PACKAGE_MANAGEMENT_REMOVE='dnf remove'
  164. elif [[ "$(type -P yum)" ]]; then
  165. PACKAGE_MANAGEMENT_INSTALL='yum -y install'
  166. PACKAGE_MANAGEMENT_REMOVE='yum remove'
  167. elif [[ "$(type -P zypper)" ]]; then
  168. PACKAGE_MANAGEMENT_INSTALL='zypper install -y --no-recommends'
  169. PACKAGE_MANAGEMENT_REMOVE='zypper remove'
  170. elif [[ "$(type -P pacman)" ]]; then
  171. PACKAGE_MANAGEMENT_INSTALL='pacman -Syu --noconfirm'
  172. PACKAGE_MANAGEMENT_REMOVE='pacman -Rsn'
  173. else
  174. echo -e "${FontRed}error: The script does not support the package manager in this operating system.${FontSuffix}"
  175. exit 1
  176. fi
  177. else
  178. echo -e "${FontRed}error: This operating system is not supported.${FontSuffix}"
  179. exit 1
  180. fi
  181. }
  182. install_software() {
  183. package_name="$1"
  184. file_to_detect="$2"
  185. type -P "$file_to_detect" >/dev/null 2>&1 && return
  186. if ${PACKAGE_MANAGEMENT_INSTALL} "$package_name"; then
  187. echo "info: $package_name is installed."
  188. else
  189. echo -e "${FontRed}error: Installation of $package_name failed, please check your network.${FontSuffix}"
  190. exit 1
  191. fi
  192. }
  193. get_latest_version() {
  194. # Get latest release version number
  195. local latest_release
  196. if ! latest_release=$(curl -x "${PROXY}" -sS -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/0xJacky/nginx-ui/releases/latest"); then
  197. echo -e "${FontRed}error: Failed to get release list, please check your network.${FontSuffix}"
  198. exit 1
  199. fi
  200. RELEASE_LATEST="$(echo "$latest_release" | sed 'y/,/\n/' | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')"
  201. if [[ -z "$RELEASE_LATEST" ]]; then
  202. if echo "$latest_release" | grep -q "API rate limit exceeded"; then
  203. echo -e "${FontRed}error: github API rate limit exceeded${FontSuffix}"
  204. else
  205. echo -e "${FontRed}error: Failed to get the latest release version.${FontSuffix}"
  206. echo "Welcome bug report: https://github.com/0xJacky/nginx-ui/issues"
  207. fi
  208. exit 1
  209. fi
  210. RELEASE_LATEST="v${RELEASE_LATEST#v}"
  211. }
  212. download_nginx_ui() {
  213. local download_link
  214. download_link="${RPROXY}https://github.com/0xJacky/nginx-ui/releases/download/$RELEASE_LATEST/nginx-ui-linux-$MACHINE.tar.gz"
  215. echo "Downloading Nginx UI archive: $download_link"
  216. if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$TAR_FILE" "$download_link"; then
  217. echo 'error: Download failed! Please check your network or try again.'
  218. return 1
  219. fi
  220. return 0
  221. }
  222. decompression() {
  223. echo "$1"
  224. if ! tar -zxf "$1" -C "$TMP_DIRECTORY"; then
  225. echo -e "${FontRed}error: Nginx UI decompression failed.${FontSuffix}"
  226. "rm" -r "$TMP_DIRECTORY"
  227. echo "removed: $TMP_DIRECTORY"
  228. exit 1
  229. fi
  230. echo "info: Extract the Nginx UI package to $TMP_DIRECTORY and prepare it for installation."
  231. }
  232. install_bin() {
  233. NAME="nginx-ui"
  234. install -m 755 "${TMP_DIRECTORY}/$NAME" "/usr/local/bin/$NAME"
  235. }
  236. install_service() {
  237. mkdir -p '/etc/systemd/system/nginx-ui.service.d'
  238. cat > "$ServicePath" << EOF
  239. [Unit]
  240. Description=Yet another WebUI for Nginx
  241. Documentation=https://github.com/0xJacky/nginx-ui
  242. After=network.target
  243. [Service]
  244. Type=simple
  245. ExecStart=/usr/local/bin/nginx-ui -config /usr/local/etc/nginx-ui/app.ini
  246. Restart=on-failure
  247. TimeoutStopSec=5
  248. KillMode=mixed
  249. [Install]
  250. WantedBy=multi-user.target
  251. EOF
  252. chmod 644 "$ServicePath"
  253. echo "info: Systemd service files have been installed successfully!"
  254. echo -e "${FontGreen}note: The following are the actual parameters for the nginx-ui service startup."
  255. echo -e "${FontGreen}note: Please make sure the configuration file path is correctly set.${FontSuffix}"
  256. systemd_cat_config "$ServicePath"
  257. systemctl daemon-reload
  258. SYSTEMD='1'
  259. }
  260. install_config() {
  261. mkdir -p "$DataPath"
  262. if [[ ! -f "$DataPath/app.ini" ]]; then
  263. cat > "$DataPath/app.ini" << EOF
  264. [server]
  265. RunMode = release
  266. HttpPort = 9000
  267. HTTPChallengePort = 9180
  268. EOF
  269. echo "info: The default configuration file was installed to '$DataPath/app.ini' successfully!"
  270. fi
  271. echo -e "${FontGreen}note: The following are the current configuration for the nginx-ui."
  272. echo -e "${FontGreen}note: Please change the information if needed.${FontSuffix}"
  273. cat_file_with_name "$DataPath/app.ini"
  274. }
  275. start_nginx_ui() {
  276. if [[ -f "$ServicePath" ]]; then
  277. systemctl start nginx-ui
  278. sleep 1s
  279. if systemctl -q is-active nginx-ui; then
  280. echo 'info: Start the Nginx UI service.'
  281. else
  282. echo -e "${FontRed}error: Failed to start the Nginx UI service.${FontSuffix}"
  283. exit 1
  284. fi
  285. fi
  286. }
  287. stop_nginx_ui() {
  288. if ! systemctl stop nginx-ui; then
  289. echo -e "${FontRed}error: Failed to stop the Nginx UI service.${FontSuffix}"
  290. exit 1
  291. fi
  292. echo "info: Nginx UI service Stopped."
  293. }
  294. remove_nginx_ui() {
  295. if systemctl list-unit-files | grep -qw 'nginx-ui'; then
  296. if [[ -n "$(pidof nginx-ui)" ]]; then
  297. stop_nginx_ui
  298. fi
  299. local delete_files=('/usr/local/bin/nginx-ui' '/etc/systemd/system/nginx-ui.service' '/etc/systemd/system/nginx-ui.service.d')
  300. if [[ "$PURGE" -eq '1' ]]; then
  301. [[ -d "$DataPath" ]] && delete_files+=("$DataPath")
  302. fi
  303. systemctl disable nginx-ui
  304. if ! ("rm" -r "${delete_files[@]}"); then
  305. echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
  306. exit 1
  307. else
  308. for i in "${!delete_files[@]}"
  309. do
  310. echo "removed: ${delete_files[$i]}"
  311. done
  312. systemctl daemon-reload
  313. echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
  314. echo 'info: Nginx UI has been removed.'
  315. if [[ "$PURGE" -eq '0' ]]; then
  316. echo 'info: If necessary, manually delete the configuration and log files.'
  317. echo "info: e.g., $DataPath ..."
  318. fi
  319. exit 0
  320. fi
  321. else
  322. echo 'error: Nginx UI is not installed.'
  323. exit 1
  324. fi
  325. }
  326. # Explanation of parameters in the script
  327. show_help() {
  328. echo "usage: $0 ACTION [OPTION]..."
  329. echo
  330. echo 'ACTION:'
  331. echo ' install Install/Update Nginx UI'
  332. echo ' remove Remove Nginx UI'
  333. echo ' help Show help'
  334. echo 'If no action is specified, then install will be selected'
  335. echo
  336. echo 'OPTION:'
  337. echo ' install:'
  338. echo ' -l, --local Install Nginx UI from a local file'
  339. 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'
  340. echo ' -r, --reverse-proxy Download through a reverse proxy server, e.g., -r https://mirror.ghproxy.com/'
  341. echo ' remove:'
  342. echo ' --purge Remove all the Nginx UI files, include logs, configs, etc'
  343. exit 0
  344. }
  345. main() {
  346. check_if_running_as_root
  347. identify_the_operating_system_and_architecture
  348. judgment_parameters "$@"
  349. # Parameter information
  350. [[ "$HELP" -eq '1' ]] && show_help
  351. [[ "$REMOVE" -eq '1' ]] && remove_nginx_ui
  352. # Important Variables
  353. TMP_DIRECTORY="$(mktemp -d)"
  354. TAR_FILE="${TMP_DIRECTORY}/nginx-ui-linux-$MACHINE.tar.gz"
  355. install_software 'curl' 'curl'
  356. # Install from a local file
  357. if [[ -n "$LOCAL_FILE" ]]; then
  358. echo "info: Install Nginx UI from a local file '$LOCAL_FILE'."
  359. decompression "$LOCAL_FILE"
  360. else
  361. get_latest_version
  362. echo "info: Installing Nginx UI $RELEASE_LATEST for $(uname -m)"
  363. if ! download_nginx_ui; then
  364. "rm" -r "$TMP_DIRECTORY"
  365. echo "removed: $TMP_DIRECTORY"
  366. exit 1
  367. fi
  368. decompression "$TAR_FILE"
  369. fi
  370. # Determine if nginx-ui is running
  371. if systemctl list-unit-files | grep -qw 'nginx-ui'; then
  372. if [[ -n "$(pidof nginx-ui)" ]]; then
  373. stop_nginx_ui
  374. NGINX_UI_RUNNING='1'
  375. fi
  376. fi
  377. install_bin
  378. echo 'installed: /usr/local/bin/nginx-ui'
  379. install_service
  380. if [[ "$SYSTEMD" -eq '1' ]]; then
  381. echo "installed: ${ServicePath}"
  382. fi
  383. "rm" -r "$TMP_DIRECTORY"
  384. echo "removed: $TMP_DIRECTORY"
  385. echo "info: Nginx UI $RELEASE_LATEST is installed."
  386. install_config
  387. if [[ "$NGINX_UI_RUNNING" -eq '1' ]]; then
  388. start_nginx_ui
  389. else
  390. systemctl start nginx-ui
  391. systemctl enable nginx-ui
  392. sleep 1s
  393. if systemctl -q is-active nginx-ui; then
  394. echo "info: Start and enable the Nginx UI service."
  395. else
  396. echo -e "${FontYellow}warning: Failed to enable and start the Nginx UI service.${FontSuffix}"
  397. fi
  398. fi
  399. }
  400. main "$@"