You’ve been asked to stand up a single chat UI that talks to OpenAI, Anthropic, Google Vertex and your in-house Ollama or vLLM — one front door, one audit trail, no rogue browser extensions. LibreChat fits, you run the compose stack, the team signs in, you hand it over. Six months later MongoDB is exposed on the host’s public interface because the compose file mapped 27017:27017, Meilisearch is running with the bootstrap master key, file uploads for RAG fill /var/lib/docker because nobody pinned a volume, and the provider API keys are sitting in plaintext in docker-compose.yml committed to a public ops repo.
What’s happening
A LibreChat deployment that is actually production-ready needs more than git clone && docker compose up. MongoDB and Meilisearch have to stay on the internal compose network — never bound to the host’s public interface. Provider API keys (OpenAI, Anthropic, Google, the local Ollama/vLLM endpoint) have to live in /opt/librechat/.env with mode 600 and be referenced from compose, not pasted into YAML. The RAG file-upload directory has to land on /var/lib/librechat/uploads on the data volume because every uploaded PDF, every Meilisearch index, every embedded vector lives there — leaving it on the OS disk fills the partition silently. The nginx layer terminating TLS has to pass WebSocket upgrades for the streaming responses, the LibreChat session secret has to be randomly generated, and signups have to be turned off or scoped to the SSO domain so the production chat tool stays inside the same identity perimeter as the rest of the fleet.
How to verify
After install, the listeners visible on the host should be exactly these:
ss -lntp | grep -E ':(80|443|3080|27017|7700)\b'
# 0.0.0.0:80 nginx
# 0.0.0.0:443 nginx
# 127.0.0.1:3080 librechat (via compose)
MongoDB on 27017 and Meilisearch on 7700 must not appear on the host at all — they live inside the compose network. End-to-end round trip via the LibreChat API with a JWT obtained from the login flow:
curl -fsS https://chat.example.com/api/config | jq '.endpoints'
That returns the list of registered providers (openAI, anthropic, google, custom-ollama). If a provider you wired up is missing, its env vars never reached the container — check /opt/librechat/.env and that the compose service was recreated, not just restarted.
The fix
Save the script below as /root/setup-librechat-ubuntu22.sh, then run it. It runs the installer flow on a fresh Ubuntu 22.04 VM: base update, kernel tuning, file limits, UFW, SSH hardening, unattended-upgrades, Docker CE + compose plugin, the LibreChat compose stack with MongoDB + Meilisearch + RAG api kept on the internal network, randomly generated session secret + Meilisearch master key + JWT signing keys written to /opt/librechat/.env with chmod 600, the upload directory pinned to /var/lib/librechat/uploads, nginx terminating TLS with WebSocket-aware proxy headers, Let’s Encrypt via certbot, and a poll loop that waits for the :3080 upstream before declaring success.
#!/usr/bin/env bash
set -euo pipefail
### Ubuntu 22.04 LibreChat clean VM installer
### Front-end only; assumes provider endpoints (OpenAI, Anthropic, Google, Ollama, vLLM) are reachable
### Binds LibreChat on 127.0.0.1 only; nginx terminates TLS
### MongoDB + Meilisearch + RAG api kept on the internal compose network
### No local backups; assumes VM-level backup. Mongo dump + /var/lib/librechat must be in your VM snapshot.
### Reboot remains manual
if [[ "$(id -u)" -ne 0 ]]; then
echo "Run as root."
exit 1
fi
: "${LIBRECHAT_DOMAIN:?Set LIBRECHAT_DOMAIN=chat.example.com}"
: "${LIBRECHAT_EMAIL:?Set LIBRECHAT_EMAIL=ops@example.com for Let's Encrypt}"
LIBRECHAT_VERSION="${LIBRECHAT_VERSION:-latest}"
LIBRECHAT_CREDS_KEY="${LIBRECHAT_CREDS_KEY:-$(openssl rand -hex 32)}"
LIBRECHAT_CREDS_IV="${LIBRECHAT_CREDS_IV:-$(openssl rand -hex 16)}"
LIBRECHAT_JWT_SECRET="${LIBRECHAT_JWT_SECRET:-$(openssl rand -hex 32)}"
LIBRECHAT_JWT_REFRESH_SECRET="${LIBRECHAT_JWT_REFRESH_SECRET:-$(openssl rand -hex 32)}"
LIBRECHAT_MEILI_MASTER_KEY="${LIBRECHAT_MEILI_MASTER_KEY:-$(openssl rand -hex 32)}"
LIBRECHAT_ALLOW_REG="${LIBRECHAT_ALLOW_REG:-false}"
OPENAI_API_KEY="${OPENAI_API_KEY:-}"
ANTHROPIC_API_KEY="${ANTHROPIC_API_KEY:-}"
GOOGLE_KEY="${GOOGLE_KEY:-}"
OLLAMA_BASE_URL="${OLLAMA_BASE_URL:-}"
OLLAMA_API_KEY="${OLLAMA_API_KEY:-}"
echo
echo "========================================"
echo "LibreChat 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 \
vim curl wget ca-certificates gnupg2 \
chrony fail2ban unattended-upgrades \
ufw lsof tcpdump dnsutils tar gzip \
iproute2 net-tools openssl jq pciutils git \
software-properties-common
echo "==> Enabling time sync"
systemctl enable --now chrony
timedatectl set-ntp true
echo "==> Detecting NVIDIA GPU (LibreChat host typically has none; this is fine)"
HAS_GPU=0
if lspci | grep -qi 'nvidia'; then
HAS_GPU=1
echo "NVIDIA GPU detected on UI host. Not required for LibreChat; only useful if you co-host inference."
else
echo "No NVIDIA GPU detected. Expected for a UI host; inference runs on the upstream LLM server."
fi
echo "==> Applying kernel/network tuning"
cat > /etc/sysctl.d/99-librechat.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
net.ipv6.conf.default.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_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
echo "==> Setting file limits"
cat > /etc/security/limits.d/99-librechat.conf <<'LIMITS'
* soft nofile 64000
* hard nofile 64000
root soft nofile 64000
root hard nofile 64000
LIMITS
echo "==> Configuring UFW"
ufw --force reset
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw --force enable
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 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 fail2ban"
mkdir -p /etc/fail2ban/jail.d
cat > /etc/fail2ban/jail.d/sshd.local <<'FAIL2BAN'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
FAIL2BAN
systemctl enable --now fail2ban
echo "==> Configuring unattended-upgrades"
cat > /etc/apt/apt.conf.d/20auto-upgrades <<'AUTOUPG'
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
APT::Periodic::AutocleanInterval "7";
AUTOUPG
echo "==> Installing Docker CE"
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 jammy 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 "==> Preparing /opt/librechat and /var/lib/librechat"
mkdir -p /opt/librechat \
/var/lib/librechat/uploads \
/var/lib/librechat/images \
/var/lib/librechat/logs \
/var/lib/librechat/mongo \
/var/lib/librechat/meili
chown -R 1000:1000 /var/lib/librechat
chmod 750 /var/lib/librechat
cat > /opt/librechat/.env <<ENV
HOST=0.0.0.0
PORT=3080
DOMAIN_CLIENT=https://${LIBRECHAT_DOMAIN}
DOMAIN_SERVER=https://${LIBRECHAT_DOMAIN}
NODE_ENV=production
# Secrets
CREDS_KEY=${LIBRECHAT_CREDS_KEY}
CREDS_IV=${LIBRECHAT_CREDS_IV}
JWT_SECRET=${LIBRECHAT_JWT_SECRET}
JWT_REFRESH_SECRET=${LIBRECHAT_JWT_REFRESH_SECRET}
MEILI_MASTER_KEY=${LIBRECHAT_MEILI_MASTER_KEY}
# Datastores
MONGO_URI=mongodb://mongodb:27017/LibreChat
MEILI_HOST=http://meilisearch:7700
MEILI_NO_ANALYTICS=true
SEARCH=true
# Registration
ALLOW_REGISTRATION=${LIBRECHAT_ALLOW_REG}
ALLOW_EMAIL_LOGIN=true
ALLOW_SOCIAL_LOGIN=true
ALLOW_SOCIAL_REGISTRATION=true
# Provider keys (leave empty to disable; provider disappears from the UI)
OPENAI_API_KEY=${OPENAI_API_KEY}
ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
GOOGLE_KEY=${GOOGLE_KEY}
# Local OpenAI-compatible (Ollama, vLLM, anything else)
# Will appear as "Custom" endpoint in the UI
OLLAMA_BASE_URL=${OLLAMA_BASE_URL}
OLLAMA_API_KEY=${OLLAMA_API_KEY}
# RAG
RAG_API_URL=http://rag_api:8000
ENV
chmod 600 /opt/librechat/.env
cat > /opt/librechat/librechat.yaml <<'YAML'
version: 1.1.4
cache: true
endpoints:
custom:
- name: "Ollama (local)"
apiKey: "${OLLAMA_API_KEY}"
baseURL: "${OLLAMA_BASE_URL}"
models:
default: ["llama3.2:3b"]
fetch: true
titleConvo: true
titleModel: "llama3.2:3b"
modelDisplayLabel: "Ollama"
registration:
socialLogins: ["openid"]
YAML
cat > /opt/librechat/docker-compose.yml <<'COMPOSE'
services:
api:
image: ghcr.io/danny-avila/librechat:${LIBRECHAT_VERSION:-latest}
container_name: librechat
restart: unless-stopped
ports:
- "127.0.0.1:3080:3080"
env_file:
- .env
volumes:
- ./librechat.yaml:/app/librechat.yaml:ro
- /var/lib/librechat/uploads:/app/uploads
- /var/lib/librechat/images:/app/client/public/images
- /var/lib/librechat/logs:/app/api/logs
depends_on:
mongodb:
condition: service_healthy
meilisearch:
condition: service_started
rag_api:
condition: service_started
mongodb:
image: mongo:7
container_name: librechat-mongo
restart: unless-stopped
command: mongod --noauth
volumes:
- /var/lib/librechat/mongo:/data/db
healthcheck:
test: ["CMD", "mongosh", "--quiet", "--eval", "db.runCommand({ping:1})"]
interval: 30s
timeout: 10s
retries: 5
start_period: 30s
meilisearch:
image: getmeili/meilisearch:v1.7
container_name: librechat-meili
restart: unless-stopped
environment:
MEILI_MASTER_KEY: ${MEILI_MASTER_KEY}
MEILI_NO_ANALYTICS: "true"
volumes:
- /var/lib/librechat/meili:/meili_data
rag_api:
image: ghcr.io/danny-avila/librechat-rag-api-dev-lite:latest
container_name: librechat-rag
restart: unless-stopped
environment:
RAG_PORT: 8000
COMPOSE
echo "==> Starting LibreChat stack"
docker compose --env-file /opt/librechat/.env -f /opt/librechat/docker-compose.yml up -d
echo "==> Waiting for LibreChat on 127.0.0.1:3080"
for i in {1..120}; do
if ss -lntp | grep -q '127.0.0.1:3080'; then
break
fi
sleep 2
done
if ! ss -lntp | grep -q '127.0.0.1:3080'; then
echo "LibreChat failed to listen on 127.0.0.1:3080"
docker compose -f /opt/librechat/docker-compose.yml logs --tail=200 || true
exit 1
fi
echo "==> Installing nginx and certbot"
apt-get -y install nginx certbot python3-certbot-nginx
cat > /etc/nginx/sites-available/librechat <<NGINX
upstream librechat_backend {
server 127.0.0.1:3080;
keepalive 32;
}
server {
listen 80;
listen [::]:80;
server_name ${LIBRECHAT_DOMAIN};
return 301 https://\$host\$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name ${LIBRECHAT_DOMAIN};
client_max_body_size 200M;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
proxy_connect_timeout 60s;
proxy_buffering off;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options nosniff always;
add_header X-Frame-Options SAMEORIGIN always;
add_header Referrer-Policy strict-origin-when-cross-origin always;
location / {
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
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 \$scheme;
proxy_http_version 1.1;
proxy_pass http://librechat_backend;
}
}
NGINX
ln -sf /etc/nginx/sites-available/librechat /etc/nginx/sites-enabled/librechat
rm -f /etc/nginx/sites-enabled/default
nginx -t
systemctl enable --now nginx
systemctl reload nginx
echo "==> Requesting Let's Encrypt certificate"
certbot --nginx --non-interactive --agree-tos \
-m "${LIBRECHAT_EMAIL}" -d "${LIBRECHAT_DOMAIN}" --redirect
systemctl enable --now certbot.timer
echo "==> Waiting for HTTPS listener"
for i in {1..30}; do
if ss -lntp | grep -q ':443'; then
break
fi
sleep 1
done
echo "==> Final package update"
apt-get -y upgrade
echo
echo "========================================"
echo "DONE"
echo "========================================"
echo
echo "Listeners:"
ss -lntp | grep -E ':(80|443|3080)\b' || true
echo
echo "Containers:"
docker compose -f /opt/librechat/docker-compose.yml ps
echo
echo "Services:"
systemctl is-active docker nginx fail2ban chrony || true
echo
echo "Registered providers (non-empty means the key was picked up):"
for k in OPENAI_API_KEY ANTHROPIC_API_KEY GOOGLE_KEY OLLAMA_BASE_URL; do
v=$(grep "^${k}=" /opt/librechat/.env | cut -d= -f2-)
if [[ -n "$v" ]]; then
echo " ${k} = set"
else
echo " ${k} = (unset; provider hidden)"
fi
done
echo
echo "Web UI:"
echo "https://${LIBRECHAT_DOMAIN}/"
echo
echo "Registration is currently: ${LIBRECHAT_ALLOW_REG}"
echo " - false: only existing users (or OIDC) can log in. The first user must be created via 'docker exec librechat npm run create-user' or by flipping to true, registering once, then flipping back."
echo
echo "Secrets written to /opt/librechat/.env (mode 600). Move them to your secrets manager before going live."
echo
echo "Final step:"
echo "Open the URL, create the first user, confirm at least one provider responds (the model dropdown lists Ollama/OpenAI/Anthropic depending on which keys you set). Then enable OIDC under registration in librechat.yaml and turn off ALLOW_REGISTRATION."
Make it executable and run it:
chmod 700 /root/setup-librechat-ubuntu22.sh
LIBRECHAT_DOMAIN=chat.example.com LIBRECHAT_EMAIL=ops@example.com \
OPENAI_API_KEY=sk-... \
ANTHROPIC_API_KEY=sk-ant-... \
OLLAMA_BASE_URL=https://llm.example.com/v1 \
OLLAMA_API_KEY=<upstream key> \
LIBRECHAT_ALLOW_REG=true \
/root/setup-librechat-ubuntu22.sh
Leave LIBRECHAT_ALLOW_REG=true long enough to register the first user via the web UI, then flip the value in /opt/librechat/.env back to false and docker compose restart api so the production deployment doesn’t accept self-signups. Provider keys that are empty in the env file silently hide that provider from the UI — OPENAI_API_KEY unset means OpenAI doesn’t show up, which is the right behaviour for a tenant that should only ever see the in-house Ollama. Backup-wise, the persistence boundary is /var/lib/librechat plus a periodic mongodump of the LibreChat database; both should be covered by your VM snapshot policy or a restic/borg job.
Run the production LibreChat on a Stack Harbor-managed environment with patch cadence, monitoring, and Sev-1 escalation built in: Managed Cloud Ops. Need a sales conversation about bringing it on board? Talk to us.