# How to install self-hosted Dify with Docker and HTTPS on a VPS | Nuvits

Source: [https://nuvits.net/en/blog/install-dify-docker-https-vps](https://nuvits.net/en/blog/install-dify-docker-https-vps)

Published 2026-12-03, by Fernando Chagas.

This guide installs self-hosted Dify `1.20.0` on an Ubuntu 24.04 VPS with Docker Compose, pinned tags for every image, rotated production secrets, and Caddy `2.11.9` handling automatic HTTPS. The setup disables the bundled `nginx` container, closes port `5003` on the plugin daemon that the default Compose file publishes to the host, and covers verification, backups, and upgrades. All commands, host ports, and the initial admin setup were tested on December 3, 2026 on a clean Ubuntu 24.04.5 LTS (`amd64`) VM.

![Architecture of self-hosted Dify 1.20.0 on an Ubuntu 24.04 VPS with Caddy 2.11.9 on ports 80 and 443, plugin_daemon port 5003 closed on the host, separated API, Socket.IO, plugin and Web routes, and 15 internal services protected with rotated secrets](https://nuvits.net/blog/install-dify-docker-https-vps/dify-docker-caddy-vps-architecture.webp)

If you are still deciding between Dify and n8n before spinning up a server, read [Dify vs n8n on a VPS: when to choose each one](https://nuvits.net/en/blog/dify-vs-n8n-vps).

## Versions used

| Component | Version | Source |
| --- | --- | --- |
| Dify (`api`, `web`, `agent_backend`, `local_sandbox`) | `1.20.0` (2026-11-18) | [Dify releases](https://github.com/langgenius/dify/releases/tag/1.20.0) |
| Dify Plugin Daemon | `0.6.15-local` | `docker/docker-compose.yaml` |
| Dify Sandbox | `0.2.15` | `docker/docker-compose.yaml` |
| Weaviate | `1.39.13` | `docker/docker-compose.yaml` |
| PostgreSQL | `15-alpine` (`15.20` tested) | `docker/docker-compose.yaml` |
| Redis | `6-alpine` (`6.2.24` tested) | `docker/docker-compose.yaml` |
| Caddy | `2.11.9` | [Caddy releases](https://github.com/caddyserver/caddy/releases/tag/v2.11.9) |
| Squid (`ssrf_proxy` and `agent_ssrf_proxy`) | `ubuntu/squid:6.6-24.04_edge` (Squid `6.14`) | [Docker Hub](https://hub.docker.com/r/ubuntu/squid) |
| BusyBox (`init_permissions`) | `1.37.0` | [Docker Hub](https://hub.docker.com/r/_/busybox) |
| Docker Engine and Compose | `29.10.1` and `v5.8.1` | official Docker repository |
| OS | Ubuntu 24.04.5 LTS (`amd64`) | |

## What you need

- A 64-bit Ubuntu 24.04 [Linux VPS](https://nuvits.net/en/linux-vps) with SSH access as a `sudo` user (examples use `deploy`), `git`, and `openssl`.
- Docker Engine and Docker Compose **v2.35.0 or newer** (required since Dify `1.20.0` so root `.env` overrides take precedence over files under `envs/`).
- A domain where you can create an `A` record pointing to the VPS's IPv4 address. The examples use `dify.example.com`.
- A valid email address for Caddy's ACME account.

With the default profile (`weaviate,postgresql,collaboration`) and Caddy replacing Nginx, the stack starts **16 containers**: one initialization container that runs and exits `0` (`init_permissions`) plus **15 long-running services** (`api`, `api_websocket`, `worker`, `worker_beat`, `web`, `agent_backend`, `plugin_daemon`, `sandbox`, `local_sandbox`, `db_postgres`, `redis`, `weaviate`, `ssrf_proxy`, `agent_ssrf_proxy`, and `caddy`).

At idle in our test, before creating any workflows or indexing documents, those 15 containers used **around 3.0 GB of RAM** (the four Python processes running `dify-api`, namely `api`, `worker`, `worker_beat`, and `api_websocket`, account for over 2.2 GB on their own). Even though Dify's README lists 2 vCPUs and 4 GiB as the floor to boot the stack, 4 GB leaves almost no headroom for the OS, PostgreSQL cache, and Weaviate indexing spikes. For production, start with **4 vCPUs and 8 GB of RAM**; if you plan to run other automations alongside it, compare options on [Docker VPS](https://nuvits.net/en/docker-vps) and the [VPS plans page](https://nuvits.net/en/vps).

## 1. Set up the firewall and Docker

If your server still lacks a `deploy` user or firewall rules, follow [First steps to secure an Ubuntu 24.04 VPS](https://nuvits.net/en/blog/secure-ubuntu-24-04-vps) first. Allow only SSH, HTTP, and HTTPS in `ufw`:

```bash
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443
sudo ufw enable
sudo ufw status
```

Answer `y` when `ufw enable` asks for confirmation. The `443` rule without a protocol allows both TCP and UDP (HTTP/3 in Caddy).

> **Docker bypasses ufw.** Any port published with `ports:` in Compose becomes reachable from the internet even when `ufw` denies incoming traffic by default. In Dify `1.20.0`'s official `docker-compose.yaml`, `plugin_daemon` publishes port `5003` on `0.0.0.0`. In step 4 we reset that port in our override so only Caddy listens on ports `80` and `443`. See [The Docker port stays open with ufw](https://nuvits.net/en/blog/docker-port-still-open-with-ufw) for the full explanation.

At Nuvits you can order the VPS with Docker preinstalled. On a clean Ubuntu 24.04 server, install Docker Engine and Compose from Docker's official repository following [How to install Docker and Docker Compose on Ubuntu 24.04](https://nuvits.net/en/blog/install-docker-compose-ubuntu-24-04), then check the installed versions:

```bash
sudo apt update && sudo apt install -y git openssl
docker --version
docker compose version
```

Make sure Compose is `v2.35.0` or newer (`v5.8.1` in this test).

## 2. Fetch the docker directory for tag 1.20.0

Instead of cloning Dify's full monorepo history, run a shallow sparse checkout pinned to tag `1.20.0` and copy only `docker/` to `~/dify`:

```bash
git clone --branch 1.20.0 --depth 1 --filter=blob:none --sparse https://github.com/langgenius/dify.git /tmp/dify-repo
git -C /tmp/dify-repo sparse-checkout set docker
cp -r /tmp/dify-repo/docker ~/dify
rm -rf /tmp/dify-repo
cd ~/dify
```

Configuration changed in Dify `1.20.0` compared to older tutorials: instead of a single 900-line `.env.example`, defaults live in `envs/**/*.defaults`, and the root `.env` holds only required deployment inputs and explicit overrides.

## 3. Generate secrets and write .env

The tracked `envs/**/*.defaults` files ship with well-known demo passwords for PostgreSQL (`difyai123456`), Redis (`difyai123456`), the code sandbox (`dify-sandbox`), the plugin daemon, and Weaviate (with Weaviate anonymous access turned on). Replace all of them before starting the stack.

The script below generates random values with `openssl` and writes a clean `.env` in `~/dify`. Replace `dify.example.com` with your domain and `ops@example.com` with your email first:

```bash
cd ~/dify

DIFY_DOMAIN="dify.example.com"
ACME_EMAIL="ops@example.com"

SECRET_KEY=$(openssl rand -base64 42)
SANDBOX_AUTH=$(openssl rand -hex 32)
DB_PASS=$(openssl rand -hex 24)
REDIS_PASS=$(openssl rand -hex 24)
PLUGIN_KEY=$(openssl rand -hex 32)
INNER_KEY=$(openssl rand -hex 32)
CODE_KEY=$(openssl rand -hex 24)
WEAV_KEY=$(openssl rand -hex 24)

cat <<EOF > .env
COMPOSE_PROFILES=weaviate,postgresql,collaboration
DIFY_DOMAIN=${DIFY_DOMAIN}
ACME_EMAIL=${ACME_EMAIL}

SECRET_KEY=${SECRET_KEY}
DIFY_AGENT_LOCAL_SANDBOX_AUTH_TOKEN=${SANDBOX_AUTH}

DB_PASSWORD=${DB_PASS}
REDIS_PASSWORD=${REDIS_PASS}

PLUGIN_DAEMON_KEY=${PLUGIN_KEY}
PLUGIN_DIFY_INNER_API_KEY=${INNER_KEY}

SANDBOX_API_KEY=${CODE_KEY}
CODE_EXECUTION_API_KEY=${CODE_KEY}

WEAVIATE_AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED=false
WEAVIATE_AUTHENTICATION_APIKEY_ALLOWED_KEYS=${WEAV_KEY}
WEAVIATE_API_KEY=${WEAV_KEY}

CONSOLE_API_URL=https://${DIFY_DOMAIN}
CONSOLE_WEB_URL=https://${DIFY_DOMAIN}
SERVICE_API_URL=https://${DIFY_DOMAIN}
APP_API_URL=https://${DIFY_DOMAIN}
APP_WEB_URL=https://${DIFY_DOMAIN}
FILES_URL=https://${DIFY_DOMAIN}
TRIGGER_URL=https://${DIFY_DOMAIN}
ENDPOINT_URL_TEMPLATE=https://${DIFY_DOMAIN}/e/{hook_id}
EOF

chmod 600 .env
```

What each block does:

- `DIFY_AGENT_LOCAL_SANDBOX_AUTH_TOKEN` is mandatory in `1.20.0` and shared between `agent_backend` and `local_sandbox`.
- `PLUGIN_DAEMON_KEY` and `PLUGIN_DIFY_INNER_API_KEY` live in root `.env` (as recommended in [Dify's official Docker README](https://github.com/langgenius/dify/blob/1.20.0/docker/README.md)) so the API, workers, Agent backend, and `plugin_daemon` all receive matching credentials.
- `WEAVIATE_AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED=false` locks down Weaviate; in our test, an internal request without an API key returned `HTTP 401`.
- The eight URL variables (`CONSOLE_API_URL` through `ENDPOINT_URL_TEMPLATE`) replace the `http://localhost` defaults used in webhooks, triggers, file links, and plugin endpoints.

> **Store a copy of `.env` off the VPS.** Dify encrypts saved LLM provider keys with `SECRET_KEY`, and PostgreSQL only applies `DB_PASSWORD` during the initial database bootstrap. Without `.env`, a restored backup cannot decrypt stored credentials.

## 4. Add Caddyfile and the production override

Dify's official Compose uses `nginx:latest` with a manual Certbot flow. Putting **Caddy `2.11.9`** in front through `docker-compose.override.yaml` avoids editing `docker-compose.yaml` (keeping upgrades clean), renews TLS certificates automatically, and fixes three production issues at once:

1. Removes host port `5003` from `plugin_daemon` with `ports: !reset []`.
2. Disables the upstream `nginx` container so Caddy owns ports `80` and `443`.
3. Pins `init_permissions` (`busybox:1.37.0`) and both Squid proxies (`ubuntu/squid:6.6-24.04_edge`) instead of the upstream `:latest` tags.

> **Why pin `ubuntu/squid:6.6-24.04_edge`?** Upstream `docker-compose.yaml` references `ubuntu/squid:latest` and runs `cp /docker-entrypoint-mount.sh /docker-entrypoint.sh` on startup. When tested on December 3, 2026 against the newer `ubuntu/squid:7.2-26.04_stable` image (built on Ubuntu 26.04), both containers entered a `Restarting (127)` crash loop with `sh: 1: cp: not found`. Pinning `6.6-24.04_edge` (Squid `6.14` on Ubuntu 24.04) prevents a future `docker compose pull` from breaking `ssrf_proxy`.

Create `~/dify/Caddyfile` mirroring Dify's API, WebSocket, plugin, and frontend routes:

```caddy Caddyfile
{
	email {$ACME_EMAIL}
}

{$DIFY_DOMAIN} {
	@api path /console/api /console/api/* /api /api/* /v1 /v1/* /openapi /openapi/* /files /files/* /mcp /mcp/* /triggers /triggers/* /callbacks/human-input/v2/im/*
	handle @api {
		reverse_proxy api:5001 {
			flush_interval -1
		}
	}

	handle /socket.io/* {
		reverse_proxy api_websocket:5001
	}

	handle /e/* {
		reverse_proxy plugin_daemon:5002 {
			header_up Dify-Hook-Url https://{host}{uri}
			flush_interval -1
		}
	}

	handle {
		reverse_proxy web:3000 {
			flush_interval -1
		}
	}
}
```

`flush_interval -1` disables response buffering so SSE token streaming reaches the browser without delay. Caddy also forwards accurate `X-Forwarded-For`, `X-Forwarded-Proto`, and `X-Forwarded-Host` headers directly, unlike stacking a TLS proxy in front of Dify's internal Nginx, which overwrites `X-Forwarded-Proto` with `$scheme` (`http`).

Now create `~/dify/docker-compose.override.yaml`:

```yaml docker-compose.override.yaml
services:
  init_permissions:
    image: busybox:1.37.0

  ssrf_proxy:
    image: ubuntu/squid:6.6-24.04_edge

  agent_ssrf_proxy:
    image: ubuntu/squid:6.6-24.04_edge

  plugin_daemon:
    ports: !reset []

  nginx:
    profiles:
      - disabled-nginx
    ports: !reset []

  caddy:
    image: caddy:2.11.9
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    environment:
      DIFY_DOMAIN: ${DIFY_DOMAIN}
      ACME_EMAIL: ${ACME_EMAIL}
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    depends_on:
      - api
      - web
    networks:
      - default

volumes:
  caddy_data:
  caddy_config:
```

Validate the merged Compose model before pulling images:

```bash
sudo docker compose config --quiet && echo "OK"
```

If it prints `OK` with no errors, the YAML and `!reset` overrides are valid.

## 5. Point DNS and start the stack

In your DNS panel, create an `A` record pointing `dify.example.com` to the VPS's public IPv4 address and check that it resolves:

```bash
dig +short dify.example.com A
```

Once DNS resolves, pull the images and start the services:

```bash
cd ~/dify
sudo docker compose pull
sudo docker compose up -d
```

On first boot, `api` and `plugin_daemon` run database migrations for `dify` and `dify_plugin` in PostgreSQL. Wait 40 to 60 seconds, then check container status:

```bash
sudo docker compose ps -a
```

`init_permissions` should show `Exited (0)`. The remaining 15 services should be `Up`, with `db_postgres`, `redis`, `api`, `sandbox`, and `local_sandbox` reporting `(healthy)`. Validate the Caddyfile and check initial logs:

```bash
sudo docker compose exec -T caddy caddy validate --config /etc/caddy/Caddyfile
sudo docker compose logs --tail=30 caddy
sudo docker compose logs --tail=30 api
```

## 6. Verify ports, HTTPS, and complete initial setup

First confirm that **only** Caddy's ports `80` and `443` are listening on the host; ports `5003`, `5432`, `6379`, `5001`, and `3000` must not appear:

```bash
sudo ss -tulpn | grep -E ':(80|443|5001|5002|5003|3000|5432|6379|8080)\b'
```

Next test the HTTP-to-HTTPS redirect, the setup API, and the WebSocket endpoint (replace with your domain):

```bash
curl -sSI http://dify.example.com/ | head -n 5
curl -fsS https://dify.example.com/console/api/setup
curl -fsS "https://dify.example.com/socket.io/?EIO=4&transport=polling" | head -c 120
```

Expected results:

- `HTTP/1.1 308 Permanent Redirect` to `https://dify.example.com/`.
- `{"step":"not_started","setup_at":null}` from `/console/api/setup`.
- A Socket.IO handshake payload starting with `0{"sid":"...","upgrades":["websocket"]...`.

Open `https://dify.example.com/install` in your browser right away and create the administrator account. While `/console/api/setup` returns `not_started`, anyone who reaches your domain can claim the initial owner account. Once submitted, the same endpoint returns `{"step":"finished",...}`.

You can also verify the internal API health check:

```bash
sudo docker compose exec -T api curl -fsS http://127.0.0.1:5001/health
```

It should return `{"pid": ..., "status": "ok", "version": "1.20.0"}`.

## 7. Backups and controlled upgrades

### Full backup

A restorable Dify backup needs three pieces: a logical PostgreSQL dump (covering both `dify` and `dify_plugin`), the persisted data under `~/dify/volumes/` (uploaded files, Weaviate indexes, and plugins, excluding the live `./volumes/db` directory), and your configuration files (`.env`, `docker-compose.override.yaml`, and `Caddyfile`).

With the stack running:

```bash
cd ~/dify
mkdir -p ~/backups/dify

sudo docker compose exec -T db_postgres pg_dumpall -U postgres > ~/backups/dify/dify-db-$(date +%F).sql
grep -c "PostgreSQL database cluster dump complete" ~/backups/dify/dify-db-$(date +%F).sql

sudo tar czf ~/backups/dify/dify-volumes-$(date +%F).tar.gz --exclude=./volumes/db ./volumes
```

The `grep` command must print `1`. Then copy the backup and configuration off the VPS from your local machine:

```bash
mkdir -p backups-dify
scp 'deploy@VPS_IP:backups/dify/dify-*' \
    'deploy@VPS_IP:dify/.env' \
    'deploy@VPS_IP:dify/Caddyfile' \
    'deploy@VPS_IP:dify/docker-compose.override.yaml' \
    ./backups-dify/
```

> **Never run `docker compose down -v` in this directory.** The `-v` flag deletes named volumes, including Caddy's certificate store and any other persistent volume declared in Compose.

### Upgrading to a new release

Before moving from `1.20.0` to a newer release:

1. Read the [Dify release notes](https://github.com/langgenius/dify/releases) for changes in `envs/` defaults or Compose version requirements.
2. Run the full backup above.
3. Fetch the new release tag's `docker/` folder into a temporary directory and update `docker-compose.yaml`, `envs/**/*.defaults`, and helper scripts **without overwriting** `.env`, `Caddyfile`, `docker-compose.override.yaml`, or `volumes/`.
4. Run `bash dify-env-sync.sh` to merge any new `.env.example` keys while preserving your existing values (it writes a backup under `env-backup/`).
5. Validate, pull the new images, and recreate the stack:

```bash
cd ~/dify
sudo docker compose config --quiet
sudo docker compose pull
sudo docker compose down
sudo docker compose up -d
sudo docker compose logs -f api
```

## Next steps

- If you want to connect Dify to webhooks, WhatsApp, or external business systems, see how the two tools pair up in [Dify vs n8n on a VPS](https://nuvits.net/en/blog/dify-vs-n8n-vps) or run n8n on a separate [VPS for n8n](https://nuvits.net/en/n8n-vps).
- If you plan to host companion apps or databases alongside Dify, review memory headroom across our [VPS plans](https://nuvits.net/en/vps) or look at a deployment panel on a [VPS for Coolify](https://nuvits.net/en/coolify-vps).
- Configure SMTP in `envs/core-services/api-worker.env` and `envs/security.env` if you plan to invite teammates to your workspace.
