npm_setup.sh 6.8 KB

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