proxmox-reverseProxy/lxc/nginx-proxy-manager/setup.sh

201 lines
4.9 KiB
Bash
Raw Normal View History

2020-11-04 16:50:28 +00:00
#!/usr/bin/env sh
set -e
# Helpers
function info { echo -e "\e[32m[info] $*\e[39m"; }
_temp_dir=$(mktemp -d)
cd $_temp_dir
_version_alpine=${_version_alpine:-3.12}
_version_npm=${_version_npm:-2.6.1}
# add openresty repo
if [ ! -f /etc/apk/keys/admin@openresty.com-5ea678a6.rsa.pub ]; then
wget -q -P /etc/apk/keys/ 'http://openresty.org/package/admin@openresty.com-5ea678a6.rsa.pub' &>/dev/null
echo "http://openresty.org/package/alpine/v$_version_alpine/main" >> /etc/apk/repositories
fi
# Update container OS
info "Updating container OS..."
apk update >/dev/null
apk upgrade &>/dev/null
echo "fs.file-max = 65535" > /etc/sysctl.conf
# Install prerequisites
info "Installing prerequisites..."
apk add python3 git certbot jq openresty nodejs npm yarn openssl &>/dev/null
python3 -m ensurepip &>/dev/null
if [ -f /etc/init.d/npm ]; then
info "Stoping services..."
rc-service npm stop &>/dev/null
rc-service openresty stop &>/dev/null
sleep 2
info "Cleaning old files..."
# Cleanup for new install
rm -rf /app \
/var/www/html \
/etc/nginx \
/var/log/nginx \
/var/lib/nginx \
/var/cache/nginx &>/dev/null
fi
# Download nginx-proxy-manager source
info "Downloading NPM v$_version_npm..."
wget -qc https://github.com/jc21/nginx-proxy-manager/archive/v$_version_npm.tar.gz -O - | tar -xz
cd nginx-proxy-manager-$_version_npm
# Copy runtime files
_rootfs=docker/rootfs
mkdir -p /var/www/html && cp -r $_rootfs/var/www/html/* /var/www/html
mkdir -p /etc/nginx/logs && cp -r $_rootfs/etc/nginx/* /etc/nginx
rm -f /etc/nginx/conf.d/dev.conf
cp $_rootfs/etc/letsencrypt.ini /etc/letsencrypt.ini
# Update NPM version in package.json files
echo "`jq --arg _version_npm $_version_npm '.version=$_version_npm' backend/package.json`" > backend/package.json
echo "`jq --arg _version_npm $_version_npm '.version=$_version_npm' frontend/package.json`" > frontend/package.json
# Create required folders
mkdir -p /tmp/nginx/body \
/run/nginx \
/var/log/nginx \
/data/nginx \
/data/custom_ssl \
/data/logs \
/data/access \
/data/nginx/default_host \
/data/nginx/default_www \
/data/nginx/proxy_host \
/data/nginx/redirection_host \
/data/nginx/stream \
/data/nginx/dead_host \
/data/nginx/temp \
/var/lib/nginx/cache/public \
/var/lib/nginx/cache/private \
/var/cache/nginx/proxy_temp
touch /var/log/nginx/error.log && chmod 777 /var/log/nginx/error.log && chmod -R 777 /var/cache/nginx
chown root /tmp/nginx
# Dynamically generate resolvers file, if resolver is IPv6, enclose in `[]`
# thanks @tfmm
echo resolver "$(awk 'BEGIN{ORS=" "} $1=="nameserver" {print ($2 ~ ":")? "["$2"]": $2}' /etc/resolv.conf);" > /etc/nginx/conf.d/include/resolvers.conf
# Generate dummy self-signed certificate.
if [ ! -f /data/nginx/dummycert.pem ] || [ ! -f /data/nginx/dummykey.pem ]
then
echo "Generating dummy SSL certificate..."
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
fi
# Copy app files
mkdir -p /app/global
cp -r backend/* /app
cp -r global/* /app/global
# Build the frontend
info "Building frontend..."
mkdir -p /app/frontend/images
cd frontend
yarn install &>/dev/null
yarn build &>/dev/null
cp -r dist/* /app/frontend
cp -r app-images/* /app/frontend/images
cd /app
info "Initalizing backend..."
rm -rf /app/config/default.json &>/dev/null
if [ ! -f /app/config/production.json ]; then
cat << 'EOF' > /app/config/production.json
{
"database": {
"engine": "knex-native",
"knex": {
"client": "sqlite3",
"connection": {
"filename": "/data/database.sqlite"
}
}
}
}
EOF
fi
yarn install &>/dev/null
# Create required folders
mkdir -p /data
# Update openresty config
info "Configuring openresty..."
cat << 'EOF' > /etc/conf.d/openresty
# Configuration for /etc/init.d/openresty
cfgfile=/etc/nginx/nginx.conf
app_prefix=/etc/nginx
EOF
rc-update add openresty boot &>/dev/null
rc-service openresty restart &>/dev/null
[ -f /usr/sbin/nginx ] && rm /usr/sbin/nginx
ln -s /usr/local/openresty/nginx/sbin/nginx /usr/sbin/nginx
# Create NPM service
info "Creating NPM service..."
cat << 'EOF' > /etc/init.d/npm
#!/sbin/openrc-run
description="Nginx Proxy Manager"
command="/usr/bin/node"
command_args="index.js --abort_on_uncaught_exception --max_old_space_size=250"
command_background="yes"
directory="/app"
pidfile="/var/run/npm.pid"
output_log="/var/log/npm.log"
error_log="/var/log/npm.err"
depends () {
before openresty
}
start_pre() {
mkdir -p /tmp/nginx/body \
/data/letsencrypt-acme-challenge \
/var/log/npm
export NODE_ENV=production
}
stop() {
pkill -9 -f node
return 0
}
restart() {
$0 stop
$0 start
}
EOF
chmod a+x /etc/init.d/npm
rc-update add npm boot &>/dev/null
rc-service npm start &>/dev/null
2020-11-04 17:08:23 +00:00
sleep 5
2020-11-04 16:50:28 +00:00
# Cleanup
info "Cleaning up..."
rm -rf $_temp_dir/nginx-proxy-manager-${_version_npm} &>/dev/null
apk del git jq npm &>/dev/null