Skip to content

Deploying Listmonk for newsletters on Ubuntu 22.04 (production-ready)

Stand up a hardened self-hosted Listmonk newsletter platform on Ubuntu 22.04 — PostgreSQL, nginx + Let's Encrypt, SMTP relay through Postal or SES, bounce handling.

You deploy Listmonk to replace a $400/month Mailchimp account. Import the list, design the template, click Send to the first 5,000 subscribers. Half an hour in, somebody on Gmail forwards a “Looks like spam?” to the marketing lead. By morning the bounce rate hits 8% and the IP shows up on b.barracudacentral.org. Nothing about Listmonk itself failed — it sent every message it was asked to. The failures were upstream: the list had three years of unverified addresses, the SMTP relay was the company’s existing Postal Server (which had been carefully warmed for transactional traffic, not bulk), and bounce processing was never wired so the list never shrank. Listmonk is a publishing tool; it inherits whatever discipline you put around it.

What’s happening

A production Listmonk needs more than the binary on a VM. The list needs to be opted-in, ideally double opt-in. SPF must include the relay (Postal, SES, or a dedicated MTA) that Listmonk hands the mail to — Listmonk itself does not deliver. DKIM signing happens at the relay, which means the d= domain must match the From: domain or DMARC will reject. A dedicated subdomain like mailings.example.com and a separate relay credential keeps bulk reputation from leaking into transactional. PostgreSQL must be bound to localhost only and Listmonk must be reached over HTTPS exclusively, never on the bare port 9000 it opens by default. Bounce handling has to be enabled and the bounce mailbox has to actually exist — Listmonk reads IMAP, parses DSN, and unsubscribes the address. Without that loop, every send compounds reputation damage.

How to verify

After install, the listeners should look like this:

ss -lntp | grep -E ':(80|443|5432|9000)\b'
# 0.0.0.0:80     nginx
# 0.0.0.0:443    nginx
# 127.0.0.1:5432 postgres
# 127.0.0.1:9000 listmonk

PostgreSQL on 127.0.0.1 only; Listmonk only reachable behind nginx. Verify the Let’s Encrypt cert on the admin UI:

curl -sI https://lists.example.com/ | head -5
openssl s_client -connect lists.example.com:443 -servername lists.example.com </dev/null 2>/dev/null | grep -E 'subject=|issuer='

DNS scaffolding for the sending subdomain (assuming Postal relays):

dig +short TXT mailings.example.com | grep spf
dig +short TXT postal-<token>._domainkey.mailings.example.com
dig +short TXT _dmarc.mailings.example.com

If SPF returns nothing or omits the relay, every receiver will softfail or quarantine. If DKIM is missing, DMARC alignment is broken from the first send.

The fix

Save the script below as /root/setup-listmonk-ubuntu22.sh, then run it. It performs the full installer flow on a fresh Ubuntu 22.04 VM: apt update, kernel/network tuning, file limits, hardens SSH while keeping root key access, configures UFW with only SSH/HTTP/HTTPS open, installs PostgreSQL 15 bound to localhost, creates the listmonk user and database, downloads the latest Listmonk binary, generates config.toml with localhost-only bindings and an SMTP relay block ready for Postal credentials, writes a systemd unit with LimitNOFILE=64000, runs listmonk --install to bootstrap the schema, brings the service up, configures nginx as a TLS-terminating reverse proxy with Let’s Encrypt, and configures fail2ban for SSH plus a Listmonk admin-auth jail.

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

### Ubuntu 22.04 Listmonk newsletter platform clean VM installer
### Keeps root SSH key login enabled
### Disables SSH password login
### Enables automatic security updates
### Installs PostgreSQL 15 + Listmonk + nginx + Let's Encrypt
### No local backups; assumes VM-level backup
### Reboot remains manual

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

: "${LISTMONK_HOSTNAME:?Set LISTMONK_HOSTNAME, e.g. lists.example.com}"
: "${LETSENCRYPT_EMAIL:?Set LETSENCRYPT_EMAIL}"
: "${SMTP_HOST:?Set SMTP_HOST, e.g. send.example.com}"
: "${SMTP_PORT:=587}"
: "${SMTP_USERNAME:?Set SMTP_USERNAME for the relay}"
: "${SMTP_PASSWORD:?Set SMTP_PASSWORD for the relay}"
: "${SMTP_FROM:?Set SMTP_FROM, e.g. news@mailings.example.com}"
: "${BOUNCE_MAILBOX_HOST:?Set BOUNCE_MAILBOX_HOST}"
: "${BOUNCE_MAILBOX_USERNAME:?Set BOUNCE_MAILBOX_USERNAME}"
: "${BOUNCE_MAILBOX_PASSWORD:?Set BOUNCE_MAILBOX_PASSWORD}"
: "${TIMEZONE:=America/Toronto}"

export DEBIAN_FRONTEND=noninteractive

echo
echo "========================================"
echo "Listmonk Ubuntu 22.04 Setup"
echo "Hostname: ${LISTMONK_HOSTNAME}"
echo "========================================"
echo

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

echo "==> Installing base packages"
apt-get -y install \
  vim curl wget gnupg ca-certificates chrony \
  fail2ban unattended-upgrades \
  lsof tcpdump dnsutils \
  ufw jq \
  apt-transport-https software-properties-common

timedatectl set-ntp true
timedatectl set-timezone "${TIMEZONE}"

echo "==> Applying kernel/network tuning"
cat > /etc/sysctl.d/99-listmonk.conf <<'SYSCTL'
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr

net.core.somaxconn = 4096
net.core.netdev_max_backlog = 250000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.ip_local_port_range = 10240 65535

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

kernel.dmesg_restrict = 1
kernel.kptr_restrict = 2
fs.file-max = 2097152
SYSCTL

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

cat > /etc/security/limits.d/99-listmonk.conf <<'LIMITS'
* soft nofile 64000
* hard nofile 64000
root soft nofile 64000
root hard nofile 64000
LIMITS

echo "==> Hardening SSH while keeping root SSH key login enabled"
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 X11Forwarding no
set_sshd_option ClientAliveInterval 300
set_sshd_option ClientAliveCountMax 2
set_sshd_option MaxAuthTries 4
sshd -t
systemctl restart sshd

echo "==> Configuring UFW"
ufw default deny incoming
ufw default allow outgoing
ufw allow OpenSSH
ufw allow 80/tcp
ufw allow 443/tcp
yes | ufw enable

echo "==> Configuring unattended-upgrades for security only"
cat > /etc/apt/apt.conf.d/52unattended-upgrades-local <<'UA'
Unattended-Upgrade::Allowed-Origins {
  "${distro_id}:${distro_codename}-security";
  "${distro_id}ESMApps:${distro_codename}-apps-security";
  "${distro_id}ESM:${distro_codename}-infra-security";
};
Unattended-Upgrade::Automatic-Reboot "false";
UA
cat > /etc/apt/apt.conf.d/20auto-upgrades <<'AAU'
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
AAU

echo "==> Installing PostgreSQL 15 (localhost-bound)"
install -d /etc/apt/keyrings
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc \
  | gpg --dearmor -o /etc/apt/keyrings/postgresql.gpg
echo "deb [signed-by=/etc/apt/keyrings/postgresql.gpg] https://apt.postgresql.org/pub/repos/apt jammy-pgdg main" \
  > /etc/apt/sources.list.d/pgdg.list
apt-get update
apt-get -y install postgresql-15

sed -i "s|^#listen_addresses.*|listen_addresses = '127.0.0.1'|" /etc/postgresql/15/main/postgresql.conf
sed -i "s|^listen_addresses.*|listen_addresses = '127.0.0.1'|" /etc/postgresql/15/main/postgresql.conf
systemctl restart postgresql

LISTMONK_DB_PASSWORD="$(openssl rand -hex 24)"
sudo -u postgres psql <<SQL
DO \$\$
BEGIN
  IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'listmonk') THEN
    CREATE ROLE listmonk LOGIN PASSWORD '${LISTMONK_DB_PASSWORD}';
  ELSE
    ALTER ROLE listmonk WITH LOGIN PASSWORD '${LISTMONK_DB_PASSWORD}';
  END IF;
END \$\$;
SQL
sudo -u postgres psql -tAc "SELECT 1 FROM pg_database WHERE datname='listmonk'" \
  | grep -q 1 \
  || sudo -u postgres createdb -O listmonk listmonk

echo "==> Creating listmonk service user"
id -u listmonk >/dev/null 2>&1 || useradd --system --home /opt/listmonk --shell /usr/sbin/nologin listmonk
install -d -o listmonk -g listmonk /opt/listmonk

echo "==> Downloading Listmonk binary"
cd /tmp
LISTMONK_TAG="$(curl -s https://api.github.com/repos/knadh/listmonk/releases/latest | jq -r .tag_name)"
LISTMONK_VER="${LISTMONK_TAG#v}"
curl -fsSL "https://github.com/knadh/listmonk/releases/download/${LISTMONK_TAG}/listmonk_${LISTMONK_VER}_linux_amd64.tar.gz" \
  | tar -xz
install -m 0755 -o root -g root listmonk /usr/local/bin/listmonk

echo "==> Writing /opt/listmonk/config.toml"
cat > /opt/listmonk/config.toml <<TOML
[app]
address = "127.0.0.1:9000"
admin_username = "admin"
admin_password = "$(openssl rand -hex 16 | tee /root/.listmonk_admin_password)"

[db]
host = "127.0.0.1"
port = 5432
user = "listmonk"
password = "${LISTMONK_DB_PASSWORD}"
database = "listmonk"
ssl_mode = "disable"
max_open = 25
max_idle = 25
max_lifetime = "300s"

[smtp]
host = "${SMTP_HOST}"
port = ${SMTP_PORT}
auth_protocol = "login"
username = "${SMTP_USERNAME}"
password = "${SMTP_PASSWORD}"
hello_hostname = "${LISTMONK_HOSTNAME}"
max_conns = 10
max_msg_retries = 2
idle_timeout = "15s"
wait_timeout = "5s"
tls_type = "STARTTLS"
tls_skip_verify = false

[bounce]
enabled = true
webhooks_enabled = true
count = 2
action = "blocklist"

[[bounce.mailboxes]]
enabled = true
type = "imap"
host = "${BOUNCE_MAILBOX_HOST}"
port = 993
auth_protocol = "login"
username = "${BOUNCE_MAILBOX_USERNAME}"
password = "${BOUNCE_MAILBOX_PASSWORD}"
return_path = "${SMTP_FROM}"
scan_interval = "1m"
tls_enabled = true
tls_skip_verify = false
TOML

chown listmonk:listmonk /opt/listmonk/config.toml
chmod 600 /opt/listmonk/config.toml

echo "==> Initialising Listmonk schema"
sudo -u listmonk bash -c "cd /opt/listmonk && /usr/local/bin/listmonk --config /opt/listmonk/config.toml --install --yes"

echo "==> Writing systemd unit"
cat > /etc/systemd/system/listmonk.service <<'UNIT'
[Unit]
Description=Listmonk newsletter platform
After=network-online.target postgresql.service
Wants=network-online.target

[Service]
Type=simple
User=listmonk
Group=listmonk
WorkingDirectory=/opt/listmonk
ExecStart=/usr/local/bin/listmonk --config /opt/listmonk/config.toml
Restart=on-failure
RestartSec=5s
LimitNOFILE=64000
ProtectSystem=full
ProtectHome=true
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target
UNIT

systemctl daemon-reload
systemctl enable --now listmonk

echo "==> Waiting for Listmonk to listen on 127.0.0.1:9000"
for i in {1..30}; do
  if ss -lntp | grep -q '127.0.0.1:9000'; then
    break
  fi
  sleep 1
done

echo "==> Installing nginx + Let's Encrypt"
apt-get -y install nginx certbot python3-certbot-nginx

cat > /etc/nginx/sites-available/listmonk.conf <<NGINX
server {
    listen 80;
    server_name ${LISTMONK_HOSTNAME};
    location /.well-known/acme-challenge/ { root /var/www/html; }
    location / { return 301 https://\$host\$request_uri; }
}

server {
    listen 443 ssl http2;
    server_name ${LISTMONK_HOSTNAME};

    ssl_certificate /etc/letsencrypt/live/${LISTMONK_HOSTNAME}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/${LISTMONK_HOSTNAME}/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;

    client_max_body_size 100m;

    # Block /api/admin from the public internet unless you really need it
    location /api/admin/ {
        allow 10.0.0.0/8;
        allow 192.168.0.0/16;
        deny all;
        proxy_pass http://127.0.0.1:9000;
        proxy_set_header Host \$host;
    }

    location / {
        proxy_pass http://127.0.0.1:9000;
        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_read_timeout 300s;
    }
}
NGINX

mkdir -p /var/www/html
ln -sf /etc/nginx/sites-available/listmonk.conf /etc/nginx/sites-enabled/listmonk.conf
rm -f /etc/nginx/sites-enabled/default
certbot --nginx -n --agree-tos -m "${LETSENCRYPT_EMAIL}" -d "${LISTMONK_HOSTNAME}"
systemctl reload nginx

echo "==> Configuring fail2ban"
mkdir -p /etc/fail2ban/jail.d /etc/fail2ban/filter.d

cat > /etc/fail2ban/jail.d/sshd.local <<'FAIL2BAN_SSH'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
FAIL2BAN_SSH

cat > /etc/fail2ban/filter.d/listmonk-auth.conf <<'F2B'
[Definition]
failregex = ^.*"POST /api/admin/login HTTP/[\d.]+" 401 .* "[^"]*" "[^"]*" \[client <HOST>\]$
            ^.*"POST /admin/login HTTP/[\d.]+" 401 .* \[client <HOST>\]$
ignoreregex =
F2B

cat > /etc/fail2ban/jail.d/listmonk.local <<'FAIL2BAN_LM'
[listmonk-auth]
enabled = true
filter = listmonk-auth
logpath = /var/log/nginx/access.log
maxretry = 5
findtime = 10m
bantime = 24h
banaction = iptables-multiport
port = http,https
FAIL2BAN_LM

systemctl enable --now fail2ban

echo "==> Final package update"
apt-get update
apt-get -y upgrade

echo
echo "========================================"
echo "DONE"
echo "========================================"
echo
echo "Listeners:"
ss -lntp | grep -E ':(80|443|5432|9000)\b' || true
echo
echo "Services:"
systemctl is-active postgresql listmonk nginx fail2ban || true
echo
echo "Web UI:"
echo "https://${LISTMONK_HOSTNAME}/admin/"
echo "Admin user:     admin"
echo "Admin password: $(cat /root/.listmonk_admin_password)"
echo
echo "Relay configured: ${SMTP_HOST}:${SMTP_PORT} as ${SMTP_USERNAME}"
echo "From address:     ${SMTP_FROM}"
echo "Bounce mailbox:   ${BOUNCE_MAILBOX_USERNAME}@${BOUNCE_MAILBOX_HOST}"
echo
echo "Suggested DNS for the mailings subdomain:"
echo "  TXT mailings.example.com   -> v=spf1 include:${SMTP_HOST} ~all"
echo "  TXT _dmarc.mailings.example.com -> v=DMARC1; p=none; rua=mailto:dmarc@example.com"
echo
echo "Next: log in, send a test broadcast to a Gmail-verified address, confirm"
echo "Authentication-Results shows spf=pass dkim=pass dmarc=pass."

Make it executable and run it:

chmod 700 /root/setup-listmonk-ubuntu22.sh
LISTMONK_HOSTNAME=lists.example.com \
LETSENCRYPT_EMAIL=ops@example.com \
SMTP_HOST=send.example.com \
SMTP_USERNAME=ListmonkBulk \
SMTP_PASSWORD=<postal-credential> \
SMTP_FROM=news@mailings.example.com \
BOUNCE_MAILBOX_HOST=mail.example.com \
BOUNCE_MAILBOX_USERNAME=bounces@example.com \
BOUNCE_MAILBOX_PASSWORD=<mailbox-password> \
/root/setup-listmonk-ubuntu22.sh

The Listmonk admin password is written to /root/.listmonk_admin_password. Log in, import the list under double opt-in only, and send a test broadcast to a Gmail-verified address before any production run. Confirm Authentication-Results shows spf=pass dkim=pass dmarc=pass. Set the bounce action to blocklist after 2 soft bounces so addresses leave the list automatically. The script intentionally does not configure host-level backups — assume VM-level snapshots; on bare metal, add a daily pg_dump listmonk plus a restic step covering /opt/listmonk/uploads if you store campaign images there.

A self-hosted newsletter platform only pays off if deliverability is monitored, the list stays clean, and the warm-up curve is respected. Stack Harbor runs Listmonk as part of environment management, with bounce rate, complaint rate, and Gmail Postmaster Tools watched.