install.sh 13 KB

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