nginxproxymanager-v5-install.sh 8.2 KB

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