nginxproxymanager-v5-install.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #!/usr/bin/env bash
  2. if [ "$VERBOSE" == "yes" ]; then set -x; fi
  3. YW=$(echo "\033[33m")
  4. RD=$(echo "\033[01;31m")
  5. BL=$(echo "\033[36m")
  6. GN=$(echo "\033[1;92m")
  7. CL=$(echo "\033[m")
  8. RETRY_NUM=10
  9. RETRY_EVERY=3
  10. NUM=$RETRY_NUM
  11. CM="${GN}✓${CL}"
  12. CROSS="${RD}✗${CL}"
  13. BFR="\\r\\033[K"
  14. HOLD="-"
  15. set -o errexit
  16. set -o errtrace
  17. set -o nounset
  18. set -o pipefail
  19. shopt -s expand_aliases
  20. alias die='EXIT=$? LINE=$LINENO error_exit'
  21. trap die ERR
  22. silent() { "$@" > /dev/null 2>&1; }
  23. function error_exit() {
  24. trap - ERR
  25. local reason="Unknown failure occurred."
  26. local msg="${1:-$reason}"
  27. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  28. echo -e "$flag $msg" 1>&2
  29. exit $EXIT
  30. }
  31. function msg_info() {
  32. local msg="$1"
  33. echo -ne " ${HOLD} ${YW}${msg}..."
  34. }
  35. function msg_ok() {
  36. local msg="$1"
  37. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  38. }
  39. function msg_error() {
  40. local msg="$1"
  41. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  42. }
  43. msg_info "Setting up Container OS "
  44. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  45. locale-gen >/dev/null
  46. while [ "$(hostname -I)" = "" ]; do
  47. echo 1>&2 -en "${CROSS}${RD} No Network! "
  48. sleep $RETRY_EVERY
  49. ((NUM--))
  50. if [ $NUM -eq 0 ]; then
  51. echo 1>&2 -e "${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  52. exit 1
  53. fi
  54. done
  55. msg_ok "Set up Container OS"
  56. msg_ok "Network Connected: ${BL}$(hostname -I)"
  57. set +e
  58. alias die=''
  59. if nc -zw1 8.8.8.8 443; 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=$(nslookup "github.com" | awk -F':' '/^Address: / { matched = 1 } matched { print $2}' | xargs)
  70. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to $RESOLVEDIP"; fi
  71. alias die='EXIT=$? LINE=$LINENO error_exit'
  72. set -e
  73. msg_info "Updating Container OS"
  74. $STD apt-get update
  75. $STD apt-get -y upgrade
  76. msg_ok "Updated Container OS"
  77. msg_info "Installing Dependencies"
  78. $STD apt-get update
  79. $STD apt-get -y install \
  80. sudo \
  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. cat <<EOF >/etc/apt/sources.list.d/openresty.list
  107. deb http://openresty.org/package/debian bullseye openresty
  108. EOF
  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. PASS=$(grep -w "root" /etc/shadow | cut -b6)
  221. if [[ $PASS != $ ]]; then
  222. msg_info "Customizing Container"
  223. rm /etc/motd
  224. rm /etc/update-motd.d/10-uname
  225. touch ~/.hushlogin
  226. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  227. mkdir -p $(dirname $GETTY_OVERRIDE)
  228. cat <<EOF >$GETTY_OVERRIDE
  229. [Service]
  230. ExecStart=
  231. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  232. EOF
  233. systemctl daemon-reload
  234. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  235. msg_ok "Customized Container"
  236. fi
  237. if [[ "${SSH_ROOT}" == "yes" ]]; then
  238. sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
  239. systemctl restart sshd
  240. fi
  241. msg_info "Starting Services"
  242. $STD systemctl enable --now openresty
  243. $STD systemctl enable --now npm
  244. msg_ok "Started Services"
  245. msg_info "Cleaning up"
  246. $STD apt-get autoremove
  247. $STD apt-get autoclean
  248. msg_ok "Cleaned"