install.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. Restart=on-failure
  248. TimeoutStopSec=5
  249. KillMode=mixed
  250. [Install]
  251. WantedBy=multi-user.target
  252. EOF
  253. chmod 644 "$ServicePath"
  254. echo "info: Systemd service files have been installed successfully!"
  255. echo -e "${FontGreen}note: The following are the actual parameters for the nginx-ui service startup."
  256. echo -e "${FontGreen}note: Please make sure the configuration file path is correctly set.${FontSuffix}"
  257. systemd_cat_config "$ServicePath"
  258. systemctl daemon-reload
  259. SYSTEMD='1'
  260. }
  261. install_config() {
  262. mkdir -p "$DataPath"
  263. if [[ ! -f "$DataPath/app.ini" ]]; then
  264. cat > "$DataPath/app.ini" << EOF
  265. [app]
  266. PageSize = 10
  267. [server]
  268. HOST = 0.0.0.0
  269. PORT = 9000
  270. RunMode = release
  271. [cert]
  272. HTTPChallengePort = 9180
  273. [terminal]
  274. StartCmd = login
  275. EOF
  276. echo "info: The default configuration file was installed to '$DataPath/app.ini' successfully!"
  277. fi
  278. echo -e "${FontGreen}note: The following are the current configuration for the nginx-ui."
  279. echo -e "${FontGreen}note: Please change the information if needed.${FontSuffix}"
  280. cat_file_with_name "$DataPath/app.ini"
  281. }
  282. start_nginx_ui() {
  283. if [[ -f "$ServicePath" ]]; then
  284. systemctl start nginx-ui
  285. sleep 1s
  286. if systemctl -q is-active nginx-ui; then
  287. echo 'info: Start the Nginx UI service.'
  288. else
  289. echo -e "${FontRed}error: Failed to start the Nginx UI service.${FontSuffix}"
  290. exit 1
  291. fi
  292. fi
  293. }
  294. stop_nginx_ui() {
  295. if ! systemctl stop nginx-ui; then
  296. echo -e "${FontRed}error: Failed to stop the Nginx UI service.${FontSuffix}"
  297. exit 1
  298. fi
  299. echo "info: Nginx UI service Stopped."
  300. }
  301. remove_nginx_ui() {
  302. if systemctl list-unit-files | grep -qw 'nginx-ui'; then
  303. if [[ -n "$(pidof nginx-ui)" ]]; then
  304. stop_nginx_ui
  305. fi
  306. local delete_files=('/usr/local/bin/nginx-ui' '/etc/systemd/system/nginx-ui.service' '/etc/systemd/system/nginx-ui.service.d')
  307. if [[ "$PURGE" -eq '1' ]]; then
  308. [[ -d "$DataPath" ]] && delete_files+=("$DataPath")
  309. fi
  310. systemctl disable nginx-ui
  311. if ! ("rm" -r "${delete_files[@]}"); then
  312. echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
  313. exit 1
  314. else
  315. for i in "${!delete_files[@]}"
  316. do
  317. echo "removed: ${delete_files[$i]}"
  318. done
  319. systemctl daemon-reload
  320. echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
  321. echo 'info: Nginx UI has been removed.'
  322. if [[ "$PURGE" -eq '0' ]]; then
  323. echo 'info: If necessary, manually delete the configuration and log files.'
  324. echo "info: e.g., $DataPath ..."
  325. fi
  326. exit 0
  327. fi
  328. else
  329. echo 'error: Nginx UI is not installed.'
  330. exit 1
  331. fi
  332. }
  333. # Explanation of parameters in the script
  334. show_help() {
  335. echo "usage: $0 ACTION [OPTION]..."
  336. echo
  337. echo 'ACTION:'
  338. echo ' install Install/Update Nginx UI'
  339. echo ' remove Remove Nginx UI'
  340. echo ' help Show help'
  341. echo 'If no action is specified, then install will be selected'
  342. echo
  343. echo 'OPTION:'
  344. echo ' install:'
  345. echo ' -l, --local Install Nginx UI from a local file'
  346. 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'
  347. echo ' -r, --reverse-proxy Download through a reverse proxy server, e.g., -r https://mirror.ghproxy.com/'
  348. echo ' remove:'
  349. echo ' --purge Remove all the Nginx UI files, include logs, configs, etc'
  350. exit 0
  351. }
  352. main() {
  353. check_if_running_as_root
  354. identify_the_operating_system_and_architecture
  355. judgment_parameters "$@"
  356. # Parameter information
  357. [[ "$HELP" -eq '1' ]] && show_help
  358. [[ "$REMOVE" -eq '1' ]] && remove_nginx_ui
  359. # Important Variables
  360. TMP_DIRECTORY="$(mktemp -d)"
  361. TAR_FILE="${TMP_DIRECTORY}/nginx-ui-linux-$MACHINE.tar.gz"
  362. install_software 'curl' 'curl'
  363. # Install from a local file
  364. if [[ -n "$LOCAL_FILE" ]]; then
  365. echo "info: Install Nginx UI from a local file '$LOCAL_FILE'."
  366. decompression "$LOCAL_FILE"
  367. else
  368. get_latest_version
  369. echo "info: Installing Nginx UI $RELEASE_LATEST for $(uname -m)"
  370. if ! download_nginx_ui; then
  371. "rm" -r "$TMP_DIRECTORY"
  372. echo "removed: $TMP_DIRECTORY"
  373. exit 1
  374. fi
  375. decompression "$TAR_FILE"
  376. fi
  377. # Determine if nginx-ui is running
  378. if systemctl list-unit-files | grep -qw 'nginx-ui'; then
  379. if [[ -n "$(pidof nginx-ui)" ]]; then
  380. stop_nginx_ui
  381. NGINX_UI_RUNNING='1'
  382. fi
  383. fi
  384. install_bin
  385. echo 'installed: /usr/local/bin/nginx-ui'
  386. install_service
  387. if [[ "$SYSTEMD" -eq '1' ]]; then
  388. echo "installed: ${ServicePath}"
  389. fi
  390. "rm" -r "$TMP_DIRECTORY"
  391. echo "removed: $TMP_DIRECTORY"
  392. echo "info: Nginx UI $RELEASE_LATEST is installed."
  393. install_config
  394. if [[ "$NGINX_UI_RUNNING" -eq '1' ]]; then
  395. start_nginx_ui
  396. else
  397. systemctl start nginx-ui
  398. systemctl enable nginx-ui
  399. sleep 1s
  400. if systemctl -q is-active nginx-ui; then
  401. echo "info: Start and enable the Nginx UI service."
  402. else
  403. echo -e "${FontYellow}warning: Failed to enable and start the Nginx UI service.${FontSuffix}"
  404. fi
  405. fi
  406. }
  407. main "$@"