nginxproxymanager-v5-install.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2021-2023 tteck
  3. # Author: tteck (tteckster)
  4. # License: MIT
  5. # https://github.com/tteck/Proxmox/raw/main/LICENSE
  6. if [ "$VERBOSE" = "yes" ]; then set -x; STD=""; else STD="silent"; fi
  7. silent() { "$@" > /dev/null 2>&1; }
  8. if [ "$DISABLEIPV6" == "yes" ]; then echo "net.ipv6.conf.all.disable_ipv6 = 1" >>/etc/sysctl.conf; $STD sysctl -p; fi
  9. YW=$(echo "\033[33m")
  10. RD=$(echo "\033[01;31m")
  11. BL=$(echo "\033[36m")
  12. GN=$(echo "\033[1;92m")
  13. CL=$(echo "\033[m")
  14. RETRY_NUM=10
  15. RETRY_EVERY=3
  16. CM="${GN}✓${CL}"
  17. CROSS="${RD}✗${CL}"
  18. BFR="\\r\\033[K"
  19. HOLD="-"
  20. set -Eeuo pipefail
  21. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  22. function error_handler() {
  23. local exit_code="$?"
  24. local line_number="$1"
  25. local command="$2"
  26. local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
  27. echo -e "\n$error_message\n"
  28. }
  29. function msg_info() {
  30. local msg="$1"
  31. echo -ne " ${HOLD} ${YW}${msg}..."
  32. }
  33. function msg_ok() {
  34. local msg="$1"
  35. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  36. }
  37. function msg_error() {
  38. local msg="$1"
  39. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  40. }
  41. msg_info "Setting up Container OS "
  42. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  43. locale-gen >/dev/null
  44. for ((i=RETRY_NUM; i>0; i--)); do
  45. if [ "$(hostname -I)" != "" ]; then
  46. break
  47. fi
  48. echo 1>&2 -en "${CROSS}${RD} No Network! "
  49. sleep $RETRY_EVERY
  50. done
  51. if [ "$(hostname -I)" = "" ]; then
  52. echo 1>&2 -e "\n${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  53. echo -e " 🖧 Check Network Settings"
  54. exit 1
  55. fi
  56. msg_ok "Set up Container OS"
  57. msg_ok "Network Connected: ${BL}$(hostname -I)"
  58. set +e
  59. if ping -c 1 -W 1 1.1.1.1 &> /dev/null; then msg_ok "Internet Connected"; else
  60. msg_error "Internet NOT Connected"
  61. read -r -p "Would you like to continue anyway? <y/N> " prompt
  62. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
  63. echo -e " ⚠️ ${RD}Expect Issues Without Internet${CL}"
  64. else
  65. echo -e " 🖧 Check Network Settings"
  66. exit 1
  67. fi
  68. fi
  69. RESOLVEDIP=$(getent hosts github.com | awk '{ print $1 }')
  70. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to ${BL}$RESOLVEDIP${CL}"; fi
  71. set -e
  72. msg_info "Updating Container OS"
  73. $STD apt-get update
  74. $STD apt-get -y upgrade
  75. msg_ok "Updated Container OS"
  76. msg_info "Installing Dependencies"
  77. $STD apt-get update
  78. $STD apt-get -y install \
  79. sudo \
  80. mc \
  81. curl \
  82. gnupg \
  83. make \
  84. g++ \
  85. gcc \
  86. ca-certificates \
  87. apache2-utils \
  88. logrotate \
  89. build-essential \
  90. python3-dev \
  91. git \
  92. lsb-release
  93. msg_ok "Installed Dependencies"
  94. msg_info "Installing Python"
  95. $STD apt-get install -y -q --no-install-recommends python3 python3-pip python3-venv
  96. $STD pip3 install --upgrade setuptools
  97. $STD pip3 install --upgrade pip
  98. $STD python3 -m venv /opt/certbot/
  99. if [ "$(getconf LONG_BIT)" = "32" ]; then
  100. $STD python3 -m pip install --no-cache-dir -U cryptography==3.3.2
  101. fi
  102. $STD python3 -m pip install --no-cache-dir cffi certbot
  103. msg_ok "Installed Python"
  104. msg_info "Installing Openresty"
  105. $STD apt-key add <(curl -fsSL https://openresty.org/package/pubkey.gpg)
  106. sh -c 'echo "deb http://openresty.org/package/debian $(lsb_release -cs) openresty" > /etc/apt/sources.list.d/openresty.list'
  107. $STD apt-get -y update
  108. $STD apt-get -y install --no-install-recommends openresty
  109. msg_ok "Installed Openresty"
  110. msg_info "Setting up Node.js Repository"
  111. $STD bash <(curl -fsSL https://deb.nodesource.com/setup_16.x)
  112. msg_ok "Set up Node.js Repository"
  113. msg_info "Installing Node.js"
  114. $STD apt-get install -y nodejs
  115. msg_ok "Installed Node.js"
  116. msg_info "Installing Yarn"
  117. $STD npm install --global yarn
  118. msg_ok "Installed Yarn"
  119. RELEASE=$(curl -s https://api.github.com/repos/NginxProxyManager/nginx-proxy-manager/releases/latest |
  120. grep "tag_name" |
  121. awk '{print substr($2, 3, length($2)-4) }')
  122. msg_info "Downloading Nginx Proxy Manager v${RELEASE}"
  123. wget -q https://codeload.github.com/NginxProxyManager/nginx-proxy-manager/tar.gz/v${RELEASE} -O - | tar -xz
  124. cd ./nginx-proxy-manager-${RELEASE}
  125. msg_ok "Downloaded Nginx Proxy Manager v${RELEASE}"
  126. msg_info "Setting up Enviroment"
  127. ln -sf /usr/bin/python3 /usr/bin/python
  128. ln -sf /usr/bin/certbot /opt/certbot/bin/certbot
  129. ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/sbin/nginx
  130. ln -sf /usr/local/openresty/nginx/ /etc/nginx
  131. sed -i "s+0.0.0+${RELEASE}+g" backend/package.json
  132. sed -i "s+0.0.0+${RELEASE}+g" frontend/package.json
  133. sed -i 's+^daemon+#daemon+g' docker/rootfs/etc/nginx/nginx.conf
  134. NGINX_CONFS=$(find "$(pwd)" -type f -name "*.conf")
  135. for NGINX_CONF in $NGINX_CONFS; do
  136. sed -i 's+include conf.d+include /etc/nginx/conf.d+g' "$NGINX_CONF"
  137. done
  138. mkdir -p /var/www/html /etc/nginx/logs
  139. cp -r docker/rootfs/var/www/html/* /var/www/html/
  140. cp -r docker/rootfs/etc/nginx/* /etc/nginx/
  141. cp docker/rootfs/etc/letsencrypt.ini /etc/letsencrypt.ini
  142. cp docker/rootfs/etc/logrotate.d/nginx-proxy-manager /etc/logrotate.d/nginx-proxy-manager
  143. ln -sf /etc/nginx/nginx.conf /etc/nginx/conf/nginx.conf
  144. rm -f /etc/nginx/conf.d/dev.conf
  145. mkdir -p /tmp/nginx/body \
  146. /run/nginx \
  147. /data/nginx \
  148. /data/custom_ssl \
  149. /data/logs \
  150. /data/access \
  151. /data/nginx/default_host \
  152. /data/nginx/default_www \
  153. /data/nginx/proxy_host \
  154. /data/nginx/redirection_host \
  155. /data/nginx/stream \
  156. /data/nginx/dead_host \
  157. /data/nginx/temp \
  158. /var/lib/nginx/cache/public \
  159. /var/lib/nginx/cache/private \
  160. /var/cache/nginx/proxy_temp
  161. chmod -R 777 /var/cache/nginx
  162. chown root /tmp/nginx
  163. echo resolver "$(awk 'BEGIN{ORS=" "} $1=="nameserver" {print ($2 ~ ":")? "["$2"]": $2}' /etc/resolv.conf);" >/etc/nginx/conf.d/include/resolvers.conf
  164. if [ ! -f /data/nginx/dummycert.pem ] || [ ! -f /data/nginx/dummykey.pem ]; then
  165. echo -en "${GN} Generating dummy SSL Certificate... "
  166. openssl req -new -newkey rsa:2048 -days 3650 -nodes -x509 -subj "/O=Nginx Proxy Manager/OU=Dummy Certificate/CN=localhost" -keyout /data/nginx/dummykey.pem -out /data/nginx/dummycert.pem &>/dev/null
  167. fi
  168. mkdir -p /app/global /app/frontend/images
  169. cp -r backend/* /app
  170. cp -r global/* /app/global
  171. msg_ok "Set up Enviroment"
  172. msg_info "Building Frontend"
  173. cd ./frontend
  174. export NODE_ENV=development
  175. $STD yarn install --network-timeout=30000
  176. $STD yarn build
  177. cp -r dist/* /app/frontend
  178. cp -r app-images/* /app/frontend/images
  179. msg_ok "Built Frontend"
  180. msg_info "Initializing Backend"
  181. rm -rf /app/config/default.json
  182. if [ ! -f /app/config/production.json ]; then
  183. cat <<'EOF' >/app/config/production.json
  184. {
  185. "database": {
  186. "engine": "knex-native",
  187. "knex": {
  188. "client": "sqlite3",
  189. "connection": {
  190. "filename": "/data/database.sqlite"
  191. }
  192. }
  193. }
  194. }
  195. EOF
  196. fi
  197. cd /app
  198. export NODE_ENV=development
  199. $STD yarn install --network-timeout=30000
  200. msg_ok "Initialized Backend"
  201. msg_info "Creating Service"
  202. cat <<'EOF' >/lib/systemd/system/npm.service
  203. [Unit]
  204. Description=Nginx Proxy Manager
  205. After=network.target
  206. Wants=openresty.service
  207. [Service]
  208. Type=simple
  209. Environment=NODE_ENV=production
  210. ExecStartPre=-mkdir -p /tmp/nginx/body /data/letsencrypt-acme-challenge
  211. ExecStart=/usr/bin/node index.js --abort_on_uncaught_exception --max_old_space_size=250
  212. WorkingDirectory=/app
  213. Restart=on-failure
  214. [Install]
  215. WantedBy=multi-user.target
  216. EOF
  217. msg_ok "Created Service"
  218. echo "export TERM='xterm-256color'" >>/root/.bashrc
  219. echo -e "$APPLICATION LXC provided by https://tteck.github.io/Proxmox/\n" > /etc/motd
  220. chmod -x /etc/update-motd.d/*
  221. if ! getent shadow root | grep -q "^root:[^\!*]"; then
  222. msg_info "Customizing Container"
  223. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  224. mkdir -p $(dirname $GETTY_OVERRIDE)
  225. cat <<EOF >$GETTY_OVERRIDE
  226. [Service]
  227. ExecStart=
  228. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  229. EOF
  230. systemctl daemon-reload
  231. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  232. msg_ok "Customized Container"
  233. fi
  234. if [[ "${SSH_ROOT}" == "yes" ]]; then sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config; systemctl restart sshd; fi
  235. msg_info "Starting Services"
  236. $STD systemctl enable --now openresty
  237. $STD systemctl enable --now npm
  238. msg_ok "Started Services"
  239. msg_info "Cleaning up"
  240. $STD apt-get autoremove
  241. $STD apt-get autoclean
  242. msg_ok "Cleaned"