nginx-proxy-manager-install.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/usr/bin/env bash
  2. set -o errexit
  3. set -o errtrace
  4. set -o nounset
  5. set -o pipefail
  6. shopt -s expand_aliases
  7. alias die='EXIT=$? LINE=$LINENO error_exit'
  8. trap die ERR
  9. trap 'die "Script interrupted."' INT
  10. function error_exit() {
  11. trap - ERR
  12. local DEFAULT='Unknown failure occured.'
  13. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  14. local FLAG="\e[91m[ERROR:LXC] \e[93m$EXIT@$LINE"
  15. msg "$FLAG $REASON"
  16. exit $EXIT
  17. }
  18. function msg() {
  19. local TEXT="$1"
  20. echo -e "$TEXT"
  21. }
  22. CROSS='\033[1;31m\xE2\x9D\x8C\033[0m'
  23. RD=`echo "\033[01;31m"`
  24. BL=`echo "\033[36m"`
  25. CM='\xE2\x9C\x94\033'
  26. GN=`echo "\033[1;92m"`
  27. CL=`echo "\033[m"`
  28. RETRY_NUM=10
  29. RETRY_EVERY=3
  30. NUM=$RETRY_NUM
  31. echo -en "${GN} Setting up Container OS... "
  32. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  33. locale-gen >/dev/null
  34. while [ "$(hostname -I)" = "" ]; do
  35. 1>&2 echo -en "${CROSS}${RD} No Network! "
  36. sleep $RETRY_EVERY
  37. ((NUM--))
  38. if [ $NUM -eq 0 ]
  39. then
  40. 1>&2 echo -e "${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  41. exit 1
  42. fi
  43. done
  44. echo -e "${CM}${CL} \r"
  45. echo -en "${GN} Network Connected: ${BL}$(hostname -I)${CL} "
  46. echo -e "${CM}${CL} \r"
  47. echo -en "${GN} Updating Container OS... "
  48. apt update &>/dev/null
  49. apt-get -qqy upgrade &>/dev/null
  50. echo -e "${CM}${CL} \r"
  51. echo -en "${GN} Installing Dependencies... "
  52. apt-get update &>/dev/null
  53. apt-get -qqy install \
  54. sudo \
  55. curl \
  56. wget \
  57. gnupg \
  58. openssl \
  59. ca-certificates \
  60. apache2-utils \
  61. logrotate \
  62. build-essential \
  63. python3-dev \
  64. git \
  65. lsb-release &>/dev/null
  66. echo -e "${CM}${CL} \r"
  67. echo -en "${GN} Installing Python... "
  68. apt-get install -y -q --no-install-recommends python3 python3-pip python3-venv &>/dev/null
  69. pip3 install --upgrade setuptools &>/dev/null
  70. pip3 install --upgrade pip &>/dev/null
  71. python3 -m venv /opt/certbot/ &>/dev/null
  72. if [ "$(getconf LONG_BIT)" = "32" ]; then
  73. python3 -m pip install --no-cache-dir -U cryptography==3.3.2 &>/dev/null
  74. fi
  75. python3 -m pip install --no-cache-dir cffi certbot &>/dev/null
  76. echo -e "${CM}${CL} \r"
  77. echo -en "${GN} Installing Openresty... "
  78. wget -q -O - https://openresty.org/package/pubkey.gpg | apt-key add - &>/dev/null
  79. codename=`grep -Po 'VERSION="[0-9]+ \(\K[^)]+' /etc/os-release` &>/dev/null
  80. echo "deb http://openresty.org/package/debian $codename openresty" | tee /etc/apt/sources.list.d/openresty.list &>/dev/null
  81. apt-get -y update &>/dev/null
  82. apt-get -y install --no-install-recommends openresty &>/dev/null
  83. echo -e "${CM}${CL} \r"
  84. echo -en "${GN} Setting up Node.js Repository... "
  85. sudo curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash - &>/dev/null
  86. echo -e "${CM}${CL} \r"
  87. echo -en "${GN} Installing Node.js... "
  88. sudo apt-get install -y nodejs git make g++ gcc &>/dev/null
  89. echo -e "${CM}${CL} \r"
  90. echo -en "${GN} Installing Yarn... "
  91. npm install --global yarn &>/dev/null
  92. echo -e "${CM}${CL} \r"
  93. RELEASE=$(curl -s https://api.github.com/repos/NginxProxyManager/nginx-proxy-manager/releases/latest \
  94. | grep "tag_name" \
  95. | awk '{print substr($2, 3, length($2)-4) }') \
  96. echo -en "${GN} Downloading NPM v${RELEASE}... "
  97. wget -q https://codeload.github.com/NginxProxyManager/nginx-proxy-manager/tar.gz/v${RELEASE} -O - | tar -xz &>/dev/null
  98. cd ./nginx-proxy-manager-${RELEASE}
  99. echo -e "${CM}${CL} \r"
  100. echo -en "${GN} Setting up Enviroment... "
  101. ln -sf /usr/bin/python3 /usr/bin/python
  102. ln -sf /usr/bin/certbot /opt/certbot/bin/certbot
  103. ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/sbin/nginx
  104. ln -sf /usr/local/openresty/nginx/ /etc/nginx
  105. sed -i "s+0.0.0+${RELEASE}+g" backend/package.json
  106. sed -i "s+0.0.0+${RELEASE}+g" frontend/package.json
  107. sed -i 's+^daemon+#daemon+g' docker/rootfs/etc/nginx/nginx.conf
  108. NGINX_CONFS=$(find "$(pwd)" -type f -name "*.conf")
  109. for NGINX_CONF in $NGINX_CONFS; do
  110. sed -i 's+include conf.d+include /etc/nginx/conf.d+g' "$NGINX_CONF"
  111. done
  112. mkdir -p /var/www/html /etc/nginx/logs
  113. cp -r docker/rootfs/var/www/html/* /var/www/html/
  114. cp -r docker/rootfs/etc/nginx/* /etc/nginx/
  115. cp docker/rootfs/etc/letsencrypt.ini /etc/letsencrypt.ini
  116. cp docker/rootfs/etc/logrotate.d/nginx-proxy-manager /etc/logrotate.d/nginx-proxy-manager
  117. ln -sf /etc/nginx/nginx.conf /etc/nginx/conf/nginx.conf
  118. rm -f /etc/nginx/conf.d/dev.conf
  119. mkdir -p /tmp/nginx/body \
  120. /run/nginx \
  121. /data/nginx \
  122. /data/custom_ssl \
  123. /data/logs \
  124. /data/access \
  125. /data/nginx/default_host \
  126. /data/nginx/default_www \
  127. /data/nginx/proxy_host \
  128. /data/nginx/redirection_host \
  129. /data/nginx/stream \
  130. /data/nginx/dead_host \
  131. /data/nginx/temp \
  132. /var/lib/nginx/cache/public \
  133. /var/lib/nginx/cache/private \
  134. /var/cache/nginx/proxy_temp
  135. chmod -R 777 /var/cache/nginx
  136. chown root /tmp/nginx
  137. echo resolver "$(awk 'BEGIN{ORS=" "} $1=="nameserver" {print ($2 ~ ":")? "["$2"]": $2}' /etc/resolv.conf);" > /etc/nginx/conf.d/include/resolvers.conf
  138. echo -e "${CM}${CL} \r"
  139. if [ ! -f /data/nginx/dummycert.pem ] || [ ! -f /data/nginx/dummykey.pem ]; then
  140. echo -en "${GN} Generating dummy SSL Certificate... "
  141. 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
  142. fi
  143. mkdir -p /app/global /app/frontend/images
  144. cp -r backend/* /app
  145. cp -r global/* /app/global
  146. echo -e "${CM}${CL} \r"
  147. echo -en "${GN} Building Frontend... "
  148. cd ./frontend
  149. export NODE_ENV=development
  150. yarn install --network-timeout=30000 &>/dev/null
  151. yarn build &>/dev/null
  152. cp -r dist/* /app/frontend
  153. cp -r app-images/* /app/frontend/images
  154. echo -e "${CM}${CL} \r"
  155. echo -en "${GN} Initializing Backend... "
  156. rm -rf /app/config/default.json &>/dev/null
  157. if [ ! -f /app/config/production.json ]; then
  158. cat << 'EOF' > /app/config/production.json
  159. {
  160. "database": {
  161. "engine": "knex-native",
  162. "knex": {
  163. "client": "sqlite3",
  164. "connection": {
  165. "filename": "/data/database.sqlite"
  166. }
  167. }
  168. }
  169. }
  170. EOF
  171. fi
  172. cd /app
  173. export NODE_ENV=development
  174. yarn install --network-timeout=30000 &>/dev/null
  175. echo -e "${CM}${CL} \r"
  176. echo -en "${GN} Creating NPM Service... "
  177. cat << 'EOF' > /lib/systemd/system/npm.service
  178. [Unit]
  179. Description=Nginx Proxy Manager
  180. After=network.target
  181. Wants=openresty.service
  182. [Service]
  183. Type=simple
  184. Environment=NODE_ENV=production
  185. ExecStartPre=-mkdir -p /tmp/nginx/body /data/letsencrypt-acme-challenge
  186. ExecStart=/usr/bin/node index.js --abort_on_uncaught_exception --max_old_space_size=250
  187. WorkingDirectory=/app
  188. Restart=on-failure
  189. [Install]
  190. WantedBy=multi-user.target
  191. EOF
  192. echo -e "${CM}${CL} \r"
  193. PASS=$(grep -w "root" /etc/shadow | cut -b6);
  194. if [[ $PASS != $ ]]; then
  195. echo -en "${GN} Customizing Container... "
  196. rm /etc/motd
  197. rm /etc/update-motd.d/10-uname
  198. touch ~/.hushlogin
  199. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  200. mkdir -p $(dirname $GETTY_OVERRIDE)
  201. cat << EOF > $GETTY_OVERRIDE
  202. [Service]
  203. ExecStart=
  204. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  205. EOF
  206. systemctl daemon-reload
  207. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  208. echo -e "${CM}${CL} \r"
  209. fi
  210. echo -en "${GN} Starting Services... "
  211. systemctl enable npm &>/dev/null
  212. systemctl start openresty
  213. systemctl start npm
  214. echo -e "${CM}${CL} \r"
  215. echo -en "${GN} Cleanup... "
  216. apt-get autoremove >/dev/null
  217. apt-get autoclean >/dev/null
  218. rm -rf /var/{cache,log}/* /var/lib/apt/lists/*
  219. echo -e "${CM}${CL} \n"