nginxproxymanager-v5-install.sh 8.3 KB

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