install.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 by this script.${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 by this script."
  156. echo -e "${FontRed}error: Please download the pre-built binary from the release page or build it manually.${FontSuffix}"
  157. exit 1
  158. fi
  159. if [[ "$(type -P apt)" ]]; then
  160. PACKAGE_MANAGEMENT_INSTALL='apt -y --no-install-recommends install'
  161. PACKAGE_MANAGEMENT_REMOVE='apt purge'
  162. elif [[ "$(type -P dnf)" ]]; then
  163. PACKAGE_MANAGEMENT_INSTALL='dnf -y install'
  164. PACKAGE_MANAGEMENT_REMOVE='dnf remove'
  165. elif [[ "$(type -P yum)" ]]; then
  166. PACKAGE_MANAGEMENT_INSTALL='yum -y install'
  167. PACKAGE_MANAGEMENT_REMOVE='yum remove'
  168. elif [[ "$(type -P zypper)" ]]; then
  169. PACKAGE_MANAGEMENT_INSTALL='zypper install -y --no-recommends'
  170. PACKAGE_MANAGEMENT_REMOVE='zypper remove'
  171. elif [[ "$(type -P pacman)" ]]; then
  172. PACKAGE_MANAGEMENT_INSTALL='pacman -Syu --noconfirm'
  173. PACKAGE_MANAGEMENT_REMOVE='pacman -Rsn'
  174. else
  175. echo -e "${FontRed}error: This script does not support the package manager in this operating system.${FontSuffix}"
  176. exit 1
  177. fi
  178. else
  179. echo -e "${FontRed}error: This operating system is not supported by this script.${FontSuffix}"
  180. exit 1
  181. fi
  182. }
  183. install_software() {
  184. package_name="$1"
  185. file_to_detect="$2"
  186. type -P "$file_to_detect" >/dev/null 2>&1 && return
  187. if ${PACKAGE_MANAGEMENT_INSTALL} "$package_name"; then
  188. echo "info: $package_name is installed."
  189. else
  190. echo -e "${FontRed}error: Installation of $package_name failed, please check your network.${FontSuffix}"
  191. exit 1
  192. fi
  193. }
  194. get_latest_version() {
  195. # Get latest release version number
  196. local latest_release
  197. 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
  198. echo -e "${FontRed}error: Failed to get release list, please check your network.${FontSuffix}"
  199. exit 1
  200. fi
  201. RELEASE_LATEST="$(echo "$latest_release" | sed 'y/,/\n/' | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')"
  202. if [[ -z "$RELEASE_LATEST" ]]; then
  203. if echo "$latest_release" | grep -q "API rate limit exceeded"; then
  204. echo -e "${FontRed}error: github API rate limit exceeded${FontSuffix}"
  205. else
  206. echo -e "${FontRed}error: Failed to get the latest release version.${FontSuffix}"
  207. echo "Welcome bug report: https://github.com/0xJacky/nginx-ui/issues"
  208. fi
  209. exit 1
  210. fi
  211. RELEASE_LATEST="v${RELEASE_LATEST#v}"
  212. }
  213. download_nginx_ui() {
  214. local download_link
  215. download_link="${RPROXY}https://github.com/0xJacky/nginx-ui/releases/download/$RELEASE_LATEST/nginx-ui-linux-$MACHINE.tar.gz"
  216. echo "Downloading Nginx UI archive: $download_link"
  217. if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$TAR_FILE" "$download_link"; then
  218. echo 'error: Download failed! Please check your network or try again.'
  219. return 1
  220. fi
  221. return 0
  222. }
  223. decompression() {
  224. echo "$1"
  225. if ! tar -zxf "$1" -C "$TMP_DIRECTORY"; then
  226. echo -e "${FontRed}error: Nginx UI decompression failed.${FontSuffix}"
  227. "rm" -r "$TMP_DIRECTORY"
  228. echo "removed: $TMP_DIRECTORY"
  229. exit 1
  230. fi
  231. echo "info: Extract the Nginx UI package to $TMP_DIRECTORY and prepare it for installation."
  232. }
  233. install_bin() {
  234. NAME="nginx-ui"
  235. install -m 755 "${TMP_DIRECTORY}/$NAME" "/usr/local/bin/$NAME"
  236. }
  237. install_service() {
  238. mkdir -p '/etc/systemd/system/nginx-ui.service.d'
  239. cat > "$ServicePath" << EOF
  240. [Unit]
  241. Description=Yet another WebUI for Nginx
  242. Documentation=https://github.com/0xJacky/nginx-ui
  243. After=network.target
  244. [Service]
  245. Type=simple
  246. ExecStart=/usr/local/bin/nginx-ui -config /usr/local/etc/nginx-ui/app.ini
  247. RuntimeDirectory=nginx-ui
  248. WorkingDirectory=/var/run/nginx-ui
  249. Restart=on-failure
  250. TimeoutStopSec=5
  251. KillMode=mixed
  252. [Install]
  253. WantedBy=multi-user.target
  254. EOF
  255. chmod 644 "$ServicePath"
  256. echo "info: Systemd service files have been installed successfully!"
  257. echo -e "${FontGreen}note: The following are the actual parameters for the nginx-ui service startup."
  258. echo -e "${FontGreen}note: Please make sure the configuration file path is correctly set.${FontSuffix}"
  259. systemd_cat_config "$ServicePath"
  260. systemctl daemon-reload
  261. SYSTEMD='1'
  262. }
  263. install_config() {
  264. mkdir -p "$DataPath"
  265. if [[ ! -f "$DataPath/app.ini" ]]; then
  266. cat > "$DataPath/app.ini" << EOF
  267. [app]
  268. PageSize = 10
  269. [server]
  270. Host = 0.0.0.0
  271. Port = 9000
  272. RunMode = release
  273. [cert]
  274. HTTPChallengePort = 9180
  275. [terminal]
  276. StartCmd = login
  277. EOF
  278. echo "info: The default configuration file was installed to '$DataPath/app.ini' successfully!"
  279. fi
  280. echo -e "${FontGreen}note: The following are the current configuration for the nginx-ui."
  281. echo -e "${FontGreen}note: Please change the information if needed.${FontSuffix}"
  282. cat_file_with_name "$DataPath/app.ini"
  283. }
  284. start_nginx_ui() {
  285. if [[ -f "$ServicePath" ]]; then
  286. systemctl start nginx-ui
  287. sleep 1s
  288. if systemctl -q is-active nginx-ui; then
  289. echo 'info: Start the Nginx UI service.'
  290. else
  291. echo -e "${FontRed}error: Failed to start the Nginx UI service.${FontSuffix}"
  292. exit 1
  293. fi
  294. fi
  295. }
  296. stop_nginx_ui() {
  297. if ! systemctl stop nginx-ui; then
  298. echo -e "${FontRed}error: Failed to stop the Nginx UI service.${FontSuffix}"
  299. exit 1
  300. fi
  301. echo "info: Nginx UI service Stopped."
  302. }
  303. remove_nginx_ui() {
  304. if systemctl list-unit-files | grep -qw 'nginx-ui'; then
  305. if [[ -n "$(pidof nginx-ui)" ]]; then
  306. stop_nginx_ui
  307. fi
  308. local delete_files=('/usr/local/bin/nginx-ui' '/etc/systemd/system/nginx-ui.service' '/etc/systemd/system/nginx-ui.service.d')
  309. if [[ "$PURGE" -eq '1' ]]; then
  310. [[ -d "$DataPath" ]] && delete_files+=("$DataPath")
  311. fi
  312. systemctl disable nginx-ui
  313. if ! ("rm" -r "${delete_files[@]}"); then
  314. echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
  315. exit 1
  316. else
  317. for i in "${!delete_files[@]}"
  318. do
  319. echo "removed: ${delete_files[$i]}"
  320. done
  321. systemctl daemon-reload
  322. echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
  323. echo 'info: Nginx UI has been removed.'
  324. if [[ "$PURGE" -eq '0' ]]; then
  325. echo 'info: If necessary, manually delete the configuration and log files.'
  326. echo "info: e.g., $DataPath ..."
  327. fi
  328. exit 0
  329. fi
  330. else
  331. echo 'error: Nginx UI is not installed.'
  332. exit 1
  333. fi
  334. }
  335. # Explanation of parameters in the script
  336. show_help() {
  337. echo "usage: $0 ACTION [OPTION]..."
  338. echo
  339. echo 'ACTION:'
  340. echo ' install Install/Update Nginx UI'
  341. echo ' remove Remove Nginx UI'
  342. echo ' help Show help'
  343. echo 'If no action is specified, then install will be selected'
  344. echo
  345. echo 'OPTION:'
  346. echo ' install:'
  347. echo ' -l, --local Install Nginx UI from a local file'
  348. 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'
  349. echo ' -r, --reverse-proxy Download through a reverse proxy server, e.g., -r https://mirror.ghproxy.com/'
  350. echo ' remove:'
  351. echo ' --purge Remove all the Nginx UI files, include logs, configs, etc'
  352. exit 0
  353. }
  354. main() {
  355. check_if_running_as_root
  356. identify_the_operating_system_and_architecture
  357. judgment_parameters "$@"
  358. # Parameter information
  359. [[ "$HELP" -eq '1' ]] && show_help
  360. [[ "$REMOVE" -eq '1' ]] && remove_nginx_ui
  361. # Important Variables
  362. TMP_DIRECTORY="$(mktemp -d)"
  363. TAR_FILE="${TMP_DIRECTORY}/nginx-ui-linux-$MACHINE.tar.gz"
  364. install_software 'curl' 'curl'
  365. # Install from a local file
  366. if [[ -n "$LOCAL_FILE" ]]; then
  367. echo "info: Install Nginx UI from a local file '$LOCAL_FILE'."
  368. decompression "$LOCAL_FILE"
  369. else
  370. get_latest_version
  371. echo "info: Installing Nginx UI $RELEASE_LATEST for $(uname -m)"
  372. if ! download_nginx_ui; then
  373. "rm" -r "$TMP_DIRECTORY"
  374. echo "removed: $TMP_DIRECTORY"
  375. exit 1
  376. fi
  377. decompression "$TAR_FILE"
  378. fi
  379. # Determine if nginx-ui is running
  380. if systemctl list-unit-files | grep -qw 'nginx-ui'; then
  381. if [[ -n "$(pidof nginx-ui)" ]]; then
  382. stop_nginx_ui
  383. NGINX_UI_RUNNING='1'
  384. fi
  385. fi
  386. install_bin
  387. echo 'installed: /usr/local/bin/nginx-ui'
  388. install_service
  389. if [[ "$SYSTEMD" -eq '1' ]]; then
  390. echo "installed: ${ServicePath}"
  391. fi
  392. "rm" -r "$TMP_DIRECTORY"
  393. echo "removed: $TMP_DIRECTORY"
  394. echo "info: Nginx UI $RELEASE_LATEST is installed."
  395. install_config
  396. if [[ "$NGINX_UI_RUNNING" -eq '1' ]]; then
  397. start_nginx_ui
  398. else
  399. systemctl start nginx-ui
  400. systemctl enable nginx-ui
  401. sleep 1s
  402. if systemctl -q is-active nginx-ui; then
  403. echo "info: Start and enable the Nginx UI service."
  404. else
  405. echo -e "${FontYellow}warning: Failed to enable and start the Nginx UI service.${FontSuffix}"
  406. fi
  407. fi
  408. }
  409. main "$@"