diff --git a/lxc/nginx-proxy-manager/README.md b/lxc/nginx-proxy-manager/README.md index d4e458c..56dac47 100644 --- a/lxc/nginx-proxy-manager/README.md +++ b/lxc/nginx-proxy-manager/README.md @@ -4,10 +4,11 @@ Many benefits can be gained by using a LXC container compared to a VM. The resou ## Usage -***Note:*** _Before using this repo, make sure Proxmox is up to date._ - To create a new LXC container on Proxmox and setup Nginx Proxy Manager to run inside of it, run the following in a SSH connection or the Proxmox web shell. +***Note:*** _tested with proxmox 6.4+_ +***Note:*** _This will create alpine container_ + ```bash curl -sL https://raw.githubusercontent.com/ej52/proxmox/main/lxc/nginx-proxy-manager/create.sh | bash -s ``` @@ -40,6 +41,8 @@ There is no login required to access the console from the Proxmox web UI. If you If you are not using proxmox or want to install this on a existing Alpine box, you can run the setup script itself. +***Note:*** _Only Alpine and Ubuntu are currently supported by this script_ + ```bash wget --no-cache -qO - https://raw.githubusercontent.com/ej52/proxmox/main/lxc/nginx-proxy-manager/setup.sh | sh ``` diff --git a/lxc/nginx-proxy-manager/create.sh b/lxc/nginx-proxy-manager/create.sh index ad1232e..bad82ff 100644 --- a/lxc/nginx-proxy-manager/create.sh +++ b/lxc/nginx-proxy-manager/create.sh @@ -191,15 +191,5 @@ EOF # Setup container info "Setting up LXC container..." pct start $_ctid -pct exec $_ctid -- sh -c "wget --no-cache -qO - $_raw_base/setup.sh | sh" - -# Get network details and show completion message -_ip=$(pct exec $_ctid -- ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}') -echo -e "\e[32m[success] Successfully created Nginx Proxy Manager LXC $_ctid.\e[39m"; -echo -e " - -Nginx Proxy Manager is reachable by going to the following URL. - - http://${_ip}:81 - -" \ No newline at end of file +sleep 3 +pct exec $_ctid -- sh -c "wget --no-cache -qO - $_raw_base/setup.sh | sh" \ No newline at end of file diff --git a/lxc/nginx-proxy-manager/install.sh b/lxc/nginx-proxy-manager/install/alpine.sh similarity index 78% rename from lxc/nginx-proxy-manager/install.sh rename to lxc/nginx-proxy-manager/install/alpine.sh index 0c2bf64..5711b2b 100644 --- a/lxc/nginx-proxy-manager/install.sh +++ b/lxc/nginx-proxy-manager/install/alpine.sh @@ -1,6 +1,5 @@ #!/usr/bin/env sh set -euo pipefail - trap trapexit EXIT SIGTERM TEMPDIR=$(mktemp -d) @@ -8,13 +7,17 @@ TEMPLOG="$TEMPDIR/tmplog" TEMPERR="$TEMPDIR/tmperr" LASTCMD="" WGETOPT="-t 1 -T 15 -q" -DEVDEPS="jq npm g++ make gcc git python3-dev musl-dev libffi-dev openssl-dev" +DEVDEPS="npm g++ make gcc git python3-dev musl-dev libffi-dev openssl-dev" +NPMURL="https://github.com/jc21/nginx-proxy-manager" cd $TEMPDIR touch $TEMPLOG # Helpers -log() { logs=$(cat $TEMPLOG | sed -e "s/34/32/g" | sed -e "s/info/success/g"); clear && printf "\033c\e[3J$logs\n\e[34m[info] $*\e[0m\n" | tee $TEMPLOG; } +log() { + logs=$(cat $TEMPLOG | sed -e "s/34/32/g" | sed -e "s/info/success/g"); + clear && printf "\033c\e[3J$logs\n\e[34m[info] $*\e[0m\n" | tee $TEMPLOG; +} runcmd() { LASTCMD=$(grep -n "$*" "$0" | sed "s/[[:blank:]]*runcmd//"); if [[ "$#" -eq 1 ]]; then @@ -81,25 +84,23 @@ grep -q 'openresty.org' /etc/apk/repositories && # Update container OS log "Updating container OS" +echo "fs.file-max = 65535" > /etc/sysctl.conf runcmd apk update runcmd apk upgrade -echo "fs.file-max = 65535" > /etc/sysctl.conf # Install dependancies log "Installing dependencies" -runcmd 'apk add python3 openresty nodejs yarn openssl apache2-utils $DEVDEPS' +runcmd 'apk add python3 openresty nodejs yarn openssl apache2-utils logrotate $DEVDEPS' # Setup python env and PIP log "Setting up python" -ln -sf /usr/bin/python3 /usr/bin/python -python -m venv /opt/certbot/ -runcmd 'wget $WGETOPT -c https://bootstrap.pypa.io/get-pip.py -O - | python' +python3 -m venv /opt/certbot/ +runcmd 'wget $WGETOPT -c https://bootstrap.pypa.io/get-pip.py -O - | python3' # Install certbot and python dependancies runcmd pip install --no-cache-dir -U cryptography==3.3.2 runcmd pip install --no-cache-dir cffi certbot -ln -sf /usr/bin/certbot /opt/certbot/bin/certbot -log "Checking for latest NPM version" +log "Checking for latest NPM release" # Get latest version information for nginx-proxy-manager runcmd 'wget $WGETOPT -O ./_latest_release $_npm_url/releases/latest' _latest_version=$(basename $(cat ./_latest_release | grep -wo "jc21/.*.tar.gz") .tar.gz | cut -d'v' -f2) @@ -107,23 +108,38 @@ _latest_version=$(basename $(cat ./_latest_release | grep -wo "jc21/.*.tar.gz") # Download nginx-proxy-manager source log "Downloading NPM v$_latest_version" runcmd 'wget $WGETOPT -c $_npm_url/archive/v$_latest_version.tar.gz -O - | tar -xz' - cd ./nginx-proxy-manager-$_latest_version -# 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 + +log "Setting up enviroment" +# Crate required symbolic links +ln -sf /usr/bin/python3 /usr/bin/python +ln -sf /usr/bin/certbot /opt/certbot/bin/certbot +ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/sbin/nginx +ln -sf /usr/local/openresty/nginx/ /etc/nginx # Update NPM version in package.json files -echo "`jq --arg _latest_version $_latest_version '.version=$_latest_version' backend/package.json`" > backend/package.json -echo "`jq --arg _latest_version $_latest_version '.version=$_latest_version' frontend/package.json`" > frontend/package.json +sed -i "s+0.0.0+#$_latest_version+g" backend/package.json +sed -i "s+0.0.0+#$_latest_version+g" frontend/package.json + +# Fix nginx config files for use with openresty defaults +sed -i 's+^daemon+#daemon+g' docker/rootfs/etc/nginx/nginx.conf +NGINX_CONFS=$(find "$(pwd)" -type f -name "*.conf") +for NGINX_CONF in $NGINX_CONFS; do + sed -i 's+include conf.d+include /etc/nginx/conf.d+g' "$NGINX_CONF" +done + +# Copy runtime files +mkdir -p /var/www/html /etc/nginx/logs +cp -r docker/rootfs/var/www/html/* /var/www/html/ +cp -r docker/rootfs/etc/nginx/* /etc/nginx/ +cp docker/rootfs/etc/letsencrypt.ini /etc/letsencrypt.ini +cp docker/rootfs/etc/logrotate.d/nginx-proxy-manager /etc/logrotate.d/nginx-proxy-manager +ln -sf /etc/nginx/nginx.conf /etc/nginx/conf/nginx.conf +rm -f /etc/nginx/conf.d/dev.conf # Create required folders mkdir -p /tmp/nginx/body \ /run/nginx \ -/var/log/nginx \ /data/nginx \ /data/custom_ssl \ /data/logs \ @@ -139,7 +155,7 @@ mkdir -p /tmp/nginx/body \ /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 +chmod -R 777 /var/cache/nginx chown root /tmp/nginx # Dynamically generate resolvers file, if resolver is IPv6, enclose in `[]` @@ -147,26 +163,26 @@ chown root /tmp/nginx 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 +if [ ! -f /data/nginx/dummycert.pem ] || [ ! -f /data/nginx/dummykey.pem ]; then log "Generating dummy SSL certificate" runcmd '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 +mkdir -p /app/global /app/frontend/images cp -r backend/* /app cp -r global/* /app/global # Build the frontend log "Building frontend" -mkdir -p /app/frontend/images cd ./frontend +export NODE_ENV=development runcmd yarn install runcmd yarn build cp -r dist/* /app/frontend cp -r app-images/* /app/frontend/images +# Initialize backend log "Initializing backend" rm -rf /app/config/default.json &>/dev/null if [ ! -f /app/config/production.json ]; then @@ -184,26 +200,9 @@ cat << 'EOF' > /app/config/production.json } EOF fi -runcmd cd /app && yarn install - -# Create required folders -mkdir -p /data - -# Update openresty config -log "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 stop &>/dev/null - -if [ -f /usr/sbin/nginx ]; then - rm /usr/sbin/nginx -fi -ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/sbin/nginx +cd /app +export NODE_ENV=development +runcmd yarn install # Create NPM service log "Creating NPM service" @@ -243,8 +242,18 @@ restart() { EOF chmod a+x /etc/init.d/npm rc-update add npm boot &>/dev/null +rc-update add openresty boot &>/dev/null +rc-service openresty stop &>/dev/null # Start services log "Starting services" runcmd rc-service openresty start -runcmd rc-service npm start \ No newline at end of file +runcmd rc-service npm start + +IP=$(ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}') +log "Installation complete + +\e[0mNginx Proxy Manager should be reachable at the following URL. + + http://${IP}:81 +" \ No newline at end of file diff --git a/lxc/nginx-proxy-manager/install/ubuntu.sh b/lxc/nginx-proxy-manager/install/ubuntu.sh new file mode 100644 index 0000000..d90bf6c --- /dev/null +++ b/lxc/nginx-proxy-manager/install/ubuntu.sh @@ -0,0 +1,230 @@ +#!/usr/bin/env bash +set -euo pipefail +trap trapexit EXIT SIGTERM + +TEMPDIR=$(mktemp -d) +TEMPLOG="$TEMPDIR/tmplog" +TEMPERR="$TEMPDIR/tmperr" +LASTCMD="" +WGETOPT="-t 1 -T 15 -q" +NPMURL="https://github.com/jc21/nginx-proxy-manager" + +cd $TEMPDIR +touch $TEMPLOG + +# Helpers +log() { + logs=$(cat $TEMPLOG | sed -e "s/34/32/g" | sed -e "s/info/success/g"); + clear && printf "\033c\e[3J$logs\n\e[34m[info] $*\e[0m\n" | tee $TEMPLOG; +} +runcmd() { + LASTCMD=$(grep -n "$*" "$0" | sed "s/[[:blank:]]*runcmd//"); + if [[ "$#" -eq 1 ]]; then + eval "$@" 2>$TEMPERR; + else + $@ 2>$TEMPERR; + fi +} +trapexit() { + status=$? + + if [[ $status -eq 0 ]]; then + logs=$(cat $TEMPLOG | sed -e "s/34/32/g" | sed -e "s/info/success/g") + clear && printf "\033c\e[3J$logs\n"; + elif [[ -s $TEMPERR ]]; then + logs=$(cat $TEMPLOG | sed -e "s/34/31/g" | sed -e "s/info/error/g") + err=$(cat $TEMPERR | sed $'s,\x1b\\[[0-9;]*[a-zA-Z],,g' | rev | cut -d':' -f1 | rev | cut -d' ' -f2-) + clear && printf "\033c\e[3J$logs\e[33m\n$0: line $LASTCMD\n\e[33;2;3m$err\e[0m\n" + else + printf "\e[33muncaught error occurred\n\e[0m" + fi + + # Cleanup + apt-get remove --purge -y build-essential python3-dev git -qq &>/dev/null + apt-get autoremove -y -qq &>/dev/null + apt-get clean + rm -rf $TEMPDIR + rm -rf /root/.cache +} + +# Check for previous install +if [ -f /lib/systemd/system/npm.service ]; then + log "Stopping services" + systemctl stop openresty + systemctl stop npm + + # Cleanup for new install + log "Cleaning old files" + rm -rf /app \ + /var/www/html \ + /etc/nginx \ + /var/log/nginx \ + /var/lib/nginx \ + /var/cache/nginx &>/dev/null +fi + +# Install dependencies +log "Installing dependencies" +echo "fs.file-max = 65535" > /etc/sysctl.conf +runcmd apt-get update +runcmd apt-get -y install --no-install-recommends wget gnupg openssl ca-certificates apache2-utils logrotate build-essential python3-dev git + +# Install Python +log "Installing python" +runcmd apt-get install -y -q --no-install-recommends python3 python3-pip python3-venv +python3 -m venv /opt/certbot/ +if [ "$(getconf LONG_BIT)" = "32" ]; then + runcmd pip install --no-cache-dir -U cryptography==3.3.2 +fi +runcmd pip install --no-cache-dir cffi certbot + +# Install openresty +log "Installing openresty" +runcmd wget -O - https://openresty.org/package/pubkey.gpg | apt-key add - +echo "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/openresty.list +runcmd apt-get update && apt-get install -y -q --no-install-recommends openresty + +# Install nodejs +log "Installing nodejs" +runcmd wget -O - https://deb.nodesource.com/setup_14.x | bash - +runcmd apt-get update && apt-get install -y -q --no-install-recommends nodejs +runcmd npm install --global yarn + +# Get latest version information for nginx-proxy-manager +log "Checking for latest NPM release" +runcmd 'wget $WGETOPT -O ./_latest_release $NPMURL/releases/latest' +_latest_version=$(basename $(cat ./_latest_release | grep -wo "jc21/.*.tar.gz") .tar.gz | cut -d'v' -f2) + +# Download nginx-proxy-manager source +log "Downloading NPM v$_latest_version" +runcmd 'wget $WGETOPT -c $NPMURL/archive/v$_latest_version.tar.gz -O - | tar -xz' +cd ./nginx-proxy-manager-$_latest_version + +log "Setting up enviroment" +# Crate required symbolic links +ln -sf /usr/bin/python3 /usr/bin/python +ln -sf /usr/bin/certbot /opt/certbot/bin/certbot +ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/sbin/nginx +ln -sf /usr/local/openresty/nginx/ /etc/nginx + +# Update NPM version in package.json files +sed -i "s+0.0.0+#$_latest_version+g" backend/package.json +sed -i "s+0.0.0+#$_latest_version+g" frontend/package.json + +# Fix nginx config files for use with openresty defaults +sed -i 's+^daemon+#daemon+g' docker/rootfs/etc/nginx/nginx.conf +NGINX_CONFS=$(find "$(pwd)" -type f -name "*.conf") +for NGINX_CONF in $NGINX_CONFS; do + sed -i 's+include conf.d+include /etc/nginx/conf.d+g' "$NGINX_CONF" +done + +# Copy runtime files +mkdir -p /var/www/html /etc/nginx/logs +cp -r docker/rootfs/var/www/html/* /var/www/html/ +cp -r docker/rootfs/etc/nginx/* /etc/nginx/ +cp docker/rootfs/etc/letsencrypt.ini /etc/letsencrypt.ini +cp docker/rootfs/etc/logrotate.d/nginx-proxy-manager /etc/logrotate.d/nginx-proxy-manager +ln -sf /etc/nginx/nginx.conf /etc/nginx/conf/nginx.conf +rm -f /etc/nginx/conf.d/dev.conf + +# Create required folders +mkdir -p /tmp/nginx/body \ +/run/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 + +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 + log "Generating dummy SSL certificate" + runcmd '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 /app/frontend/images +cp -r backend/* /app +cp -r global/* /app/global + +# Build the frontend +log "Building frontend" +cd ./frontend +export NODE_ENV=development +runcmd yarn install +runcmd yarn build +cp -r dist/* /app/frontend +cp -r app-images/* /app/frontend/images + +# Initialize backend +log "Initializing 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 +cd /app +export NODE_ENV=development +runcmd yarn install + +# Create NPM service +log "Creating NPM service" +cat << 'EOF' > /lib/systemd/system/npm.service +[Unit] +Description=Nginx Proxy Manager +After=network.target +Wants=openresty.service + +[Service] +Type=simple +Environment=NODE_ENV=production +ExecStartPre=-mkdir -p /tmp/nginx/body /data/letsencrypt-acme-challenge +ExecStart=/usr/bin/node index.js --abort_on_uncaught_exception --max_old_space_size=250 +WorkingDirectory=/app +Restart=on-failure + +[Install] +WantedBy=multi-user.target +EOF +systemctl daemon-reload +systemctl enable npm + +# Start services +log "Starting services" +runcmd systemctl start openresty +runcmd systemctl start npm + +IP=$(ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}') +log "Installation complete + +\e[0mNginx Proxy Manager should be reachable at the following URL. + + http://${IP}:81 +" \ No newline at end of file diff --git a/lxc/nginx-proxy-manager/setup.sh b/lxc/nginx-proxy-manager/setup.sh index 6314b4b..1d8035f 100644 --- a/lxc/nginx-proxy-manager/setup.sh +++ b/lxc/nginx-proxy-manager/setup.sh @@ -1,13 +1,27 @@ #!/usr/bin/env sh -set -euo pipefail -trap 'rm -rf $TMP' EXIT SIGINT SIGTERM - TMP=/tmp/npm_install.sh -URL=https://raw.githubusercontent.com/ej52/proxmox/main/lxc/nginx-proxy-manager/install.sh +URL=https://raw.githubusercontent.com/ej52/proxmox/main/lxc/nginx-proxy-manager/install + +if [ "$(uname)" != "Linux" ]; then + echo "OS NOT SUPPORTED" + exit 1 +fi + +DISTRO=$(cat /etc/*-release | grep -w ID | cut -d= -f2 | tr -d '"') +if [ "$DISTRO" != "alpine" ] && [ "$DISTRO" != "ubuntu" ]; then + echo "DISTRO NOT SUPPORTED" + exit 1 +fi rm -rf $TMP -wget -q -O "$TMP" "$URL" +wget -O "$TMP" "$URL/$DISTRO.sh" chmod +x "$TMP" -sh "$TMP" + +if [ "$(command -v bash)" ]; then + bash "$TMP" +else + sh "$TMP" +fi +