nginxproxymanager-v5-install.sh 8.1 KB

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