Skip to content

Deploying Nginx Proxy Manager on Ubuntu 22.04 (production-ready)

Stand up a hardened Nginx Proxy Manager on Ubuntu 22.04 via Docker Compose — MariaDB backend, admin port firewalled, TLS via Let's Encrypt DNS-01.

You spin up Nginx Proxy Manager because someone on the team wants a UI for the certs and the proxy hosts. The README’s docker compose up -d works — the admin UI is on :81, the first vhost gets a cert in two minutes, everything looks fine. Six months later, the admin port is still exposed to the internet, the default admin@example.com / changeme account was never rotated because the email looked like a placeholder, the embedded SQLite from the quickstart became MariaDB only after one panicked migration during which the cert metadata ended up briefly in two places, and a wildcard renewal silently fails because the DNS provider plugin’s credentials live inside the container with no rotation log.

What’s happening

A production NPM install means treating the UI as a privileged surface: the admin port (:81) must be firewalled to a bastion or VPN subnet, the default admin credentials rotated before the container even faces the network, the MariaDB backend run as a separate container with its own volume (not SQLite, which doesn’t survive concurrent writes well past a couple of vhosts), wildcard renewal done via DNS-01 with provider credentials kept in an .env file outside source control, an explicit external Docker network for back-end apps (not relying on default), and LimitNOFILE raised on both NPM and the underlying nginx so the proxy doesn’t run out of file descriptors with a few hundred concurrent upstreams. The default install gets you a working UI; what fails is the second engineer inheriting credentials, network ACLs, and renewal cadence nobody documented.

How to verify

After the stack is up, the listeners should look like this:

ss -lntp | grep -E ':(80|443|81)\b'
# 0.0.0.0:80     docker-proxy -> npm
# 0.0.0.0:443    docker-proxy -> npm
# 127.0.0.1:81   docker-proxy -> npm  (admin UI, localhost only)

The NPM container should report healthy:

docker inspect --format '{{.State.Health.Status}}' npm

A curl -I against a configured proxy host should show HSTS and the correct cert:

curl -sI https://app.example.com/ | grep -iE 'strict-transport|server'
openssl s_client -connect app.example.com:443 -servername app.example.com </dev/null 2>/dev/null \
  | openssl x509 -noout -issuer -dates

And the underlying nginx config that NPM writes should pass syntax check (it’s bind-mounted from a volume; the container exposes it):

docker exec npm nginx -t

If the admin UI answers on 0.0.0.0:81, the issuer is (STAGING), or nginx -t returns errors, the install is not done yet.

The fix

Save the script below as /root/setup-npm-ubuntu22.sh, then run it. It performs the full installer flow on a fresh Ubuntu 22.04 VM: base update, installs Docker CE and the compose plugin, hardens SSH while keeping the root key, configures ufw to allow 22/80/443 publicly and :81 only from ${ADMIN_CIDR}, applies sysctl tuning relevant to a high-concurrency reverse proxy, writes docker-compose.yml and .env for jc21/nginx-proxy-manager plus a separate MariaDB container with a dedicated user, binds the admin UI to 127.0.0.1:81 (and to the firewalled admin subnet if you set one), creates an external edge Docker network for back-end apps, sets ulimits.nofile=200000 on both containers, configures a DNS-01 plugin stub in .env for wildcard renewals, starts the stack, and waits for the admin and public listeners before continuing.

#!/usr/bin/env bash
set -euo pipefail

### Ubuntu 22.04 Nginx Proxy Manager installer
### MariaDB backend in its own container
### Admin UI bound to 127.0.0.1:81 (firewall opens it to ${ADMIN_CIDR} only)
### TLS via Let's Encrypt; DNS-01 stub in .env for wildcards
### No local backups; assumes VM-level backup
### Reboot remains manual

if [[ "$(id -u)" -ne 0 ]]; then
  echo "Run as root."
  exit 1
fi

: "${ADMIN_CIDR:?Set ADMIN_CIDR=10.10.0.0/24 (bastion/VPN subnet allowed to reach :81), or 127.0.0.1/32 to keep it SSH-tunnel only}"
: "${MYSQL_ROOT_PASSWORD:?Set MYSQL_ROOT_PASSWORD=... before running}"
: "${MYSQL_APP_PASSWORD:?Set MYSQL_APP_PASSWORD=... (used by NPM to talk to MariaDB)}"

echo
echo "========================================"
echo "Nginx Proxy Manager Ubuntu 22.04 Setup"
echo "========================================"
echo

echo "==> Updating base OS"
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get -y upgrade

echo "==> Installing base packages"
apt-get -y install \
  curl \
  ca-certificates \
  gnupg \
  software-properties-common \
  ufw \
  fail2ban \
  unattended-upgrades \
  chrony \
  lsof \
  net-tools \
  jq

echo "==> Enabling time sync"
systemctl enable --now chrony
timedatectl set-ntp true

echo "==> Applying sysctl tuning for reverse proxy"
cat > /etc/sysctl.d/99-npm.conf <<'SYSCTL'
net.core.somaxconn = 32768
net.core.netdev_max_backlog = 250000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10240 65535

net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr

net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0

fs.file-max = 2097152
SYSCTL

modprobe tcp_bbr || true
echo tcp_bbr > /etc/modules-load.d/bbr.conf
sysctl --system

echo "==> Setting file limits"
cat > /etc/security/limits.d/99-npm.conf <<'LIMITS'
* soft nofile 200000
* hard nofile 200000
root soft nofile 200000
root hard nofile 200000
LIMITS

echo "==> Hardening SSH"
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F-%H%M%S)
set_sshd_option() {
  local key="$1"
  local value="$2"
  if grep -qE "^[#[:space:]]*${key}[[:space:]]+" /etc/ssh/sshd_config; then
    sed -i "s|^[#[:space:]]*${key}[[:space:]].*|${key} ${value}|" /etc/ssh/sshd_config
  else
    echo "${key} ${value}" >> /etc/ssh/sshd_config
  fi
}
set_sshd_option PermitRootLogin prohibit-password
set_sshd_option PasswordAuthentication no
set_sshd_option PubkeyAuthentication yes
set_sshd_option KbdInteractiveAuthentication no
set_sshd_option ChallengeResponseAuthentication no
set_sshd_option X11Forwarding no
set_sshd_option ClientAliveInterval 300
set_sshd_option ClientAliveCountMax 2
set_sshd_option MaxAuthTries 4
sshd -t
systemctl restart ssh

echo "==> Configuring UFW (22, 80, 443 public; :81 from ${ADMIN_CIDR} only)"
ufw --force reset
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow from "${ADMIN_CIDR}" to any port 81 proto tcp
ufw --force enable

echo "==> Configuring fail2ban for SSH"
mkdir -p /etc/fail2ban/jail.d
cat > /etc/fail2ban/jail.d/sshd.local <<'F2B'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
F2B
systemctl enable --now fail2ban

echo "==> Configuring unattended security upgrades"
dpkg-reconfigure -f noninteractive unattended-upgrades

echo "==> Installing Docker CE + compose plugin"
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo $VERSION_CODENAME) stable" \
  > /etc/apt/sources.list.d/docker.list
apt-get update
apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
systemctl enable --now docker

echo "==> Creating NPM layout"
mkdir -p /opt/npm/{data,letsencrypt,mariadb}
chmod 750 /opt/npm /opt/npm/data /opt/npm/letsencrypt /opt/npm/mariadb

echo "==> Writing /opt/npm/.env"
cat > /opt/npm/.env <<ENV
MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
MYSQL_APP_PASSWORD=${MYSQL_APP_PASSWORD}

# Cloudflare DNS-01 plugin sample; replace with your provider+token
# CLOUDFLARE_DNS_API_TOKEN=...
ENV
chmod 600 /opt/npm/.env

echo "==> Writing /opt/npm/docker-compose.yml"
cat > /opt/npm/docker-compose.yml <<'COMPOSE'
services:
  npm:
    image: jc21/nginx-proxy-manager:latest
    container_name: npm
    restart: always
    environment:
      DB_MYSQL_HOST: "npm-db"
      DB_MYSQL_PORT: 3306
      DB_MYSQL_USER: "npm"
      DB_MYSQL_PASSWORD: "${MYSQL_APP_PASSWORD}"
      DB_MYSQL_NAME: "npm"
      DISABLE_IPV6: "true"
    ports:
      - "80:80"
      - "443:443"
      - "127.0.0.1:81:81"
    volumes:
      - /opt/npm/data:/data
      - /opt/npm/letsencrypt:/etc/letsencrypt
    networks:
      - edge
      - npm-internal
    depends_on:
      npm-db:
        condition: service_healthy
    ulimits:
      nofile:
        soft: 200000
        hard: 200000
    healthcheck:
      test: ["CMD", "curl", "-fsS", "http://127.0.0.1:81/api"]
      interval: 15s
      timeout: 5s
      retries: 5

  npm-db:
    image: mariadb:10.11
    container_name: npm-db
    restart: always
    environment:
      MARIADB_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
      MARIADB_DATABASE: "npm"
      MARIADB_USER: "npm"
      MARIADB_PASSWORD: "${MYSQL_APP_PASSWORD}"
    volumes:
      - /opt/npm/mariadb:/var/lib/mysql
    networks:
      - npm-internal
    healthcheck:
      test: ["CMD", "mariadb-admin", "ping", "-h", "127.0.0.1", "-uroot", "-p${MYSQL_ROOT_PASSWORD}"]
      interval: 10s
      timeout: 5s
      retries: 10

networks:
  edge:
    name: edge
  npm-internal:
    internal: true
COMPOSE

echo "==> Starting the stack"
cd /opt/npm
docker compose pull
docker compose --env-file .env up -d

echo "==> Waiting for NPM admin API and public listeners"
for i in {1..60}; do
  if ss -lntp | grep -q ':443' && ss -lntp | grep -q ':80' && ss -lntp | grep -q '127.0.0.1:81'; then
    break
  fi
  sleep 1
done

if ! ss -lntp | grep -q ':443'; then
  echo "NPM did not bind :443"
  docker compose logs --tail=200 npm || true
  exit 1
fi

# Wait for healthcheck status (best effort)
for i in {1..30}; do
  STATUS=$(docker inspect --format '{{.State.Health.Status}}' npm 2>/dev/null || echo unknown)
  if [[ "$STATUS" == "healthy" ]]; then
    break
  fi
  sleep 2
done

echo "==> Verifying NPM-rendered nginx config syntax"
docker exec npm nginx -t || true

echo
echo "========================================"
echo "DONE"
echo "========================================"
echo
echo "Kernel:"
uname -r
echo
echo "Listeners:"
ss -lntp | grep -E ':(80|443|81)\b' || true
echo
echo "Containers:"
docker compose -f /opt/npm/docker-compose.yml ps
echo
echo "Health:"
docker inspect --format '{{.Name}} -> {{.State.Health.Status}}' npm npm-db 2>/dev/null || true
echo
echo "Admin UI:    http://127.0.0.1:81/  (SSH-tunnel: ssh -L 81:127.0.0.1:81 root@host)"
echo "Allowed CIDR for :81: ${ADMIN_CIDR}"
echo "Default login on first boot: admin@example.com / changeme"
echo "  --> rotate immediately in the UI before any DNS points at this host."
echo
echo "Add proxy hosts in the UI; choose 'Let's Encrypt' with DNS-01 for wildcards."
echo "DNS-01 provider credentials live in /opt/npm/.env (mode 600)."
echo
echo "Compose stack: /opt/npm/docker-compose.yml"
echo "Data volume:   /opt/npm/data"
echo "MariaDB data:  /opt/npm/mariadb"
echo "Certificates:  /opt/npm/letsencrypt"

Make it executable and run it (export the three required env vars first):

chmod 700 /root/setup-npm-ubuntu22.sh
export ADMIN_CIDR=10.10.0.0/24        # or 127.0.0.1/32 to keep :81 SSH-tunnel only
export MYSQL_ROOT_PASSWORD=...
export MYSQL_APP_PASSWORD=...
/root/setup-npm-ubuntu22.sh

On first login, rotate the default admin@example.com / changeme account before the DNS for any vhost points at this host — the credentials are well-known and the admin UI publishes a usable API. For wildcard renewals, choose “DNS Challenge” in the proxy host editor, drop the provider token into /opt/npm/.env (mode 600), and recreate the container once. The script does not configure host-level backups — the assumption is the VM has snapshot/backup at the cloud layer, with /opt/npm/data, /opt/npm/mariadb, and /opt/npm/letsencrypt covered.

When a self-hosted edge fronts customer workloads under SLO, the Cloud Compute team owns the patching cadence, renewal monitoring, and on-call runbook — reach out.