install.sh 13 KB

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