Self-host Dify on your own VPS: multi-service LLM workflows, RAG and private model gateways
Deploy Dify on your own Linux server with PostgreSQL, Redis/Valkey and isolated sandbox services over Zenavo private networking, wire it to LiteLLM, New API or SearXNG, and keep knowledge base data on your own machine.
This post is also available in 简体中文, 繁體中文, 日本語, Español and Bahasa Indonesia.
Building an internal RAG assistant or multi-step LLM workflow usually means gluing together prompt templates, document chunking, vector search, tool calls and API logs. Dify packages all of that into a visual workflow studio and an API backend you can self-host. Running it on your own VPS keeps uploaded documents, embeddings and execution logs on your own disk without cloud workspace limits.
This guide covers how Dify's services fit together on Zenavo, how to wire it to a self-hosted model gateway or search tool over .zenavo.internal, and what to check when bringing it up. Your server and Zenavo are billed separately.
1. Server sizing and architecture
Dify is a multi-service stack rather than a single container. For a smooth setup with document indexing and concurrent workflow runs, connect a Linux server with at least 4 GB of memory (8 GB if you also run a vector database and local embedding workloads on the same host).
Inside a single Zenavo project, every service resolves the others over the project private network without exposing internal ports to the internet:
| Component | Role in the stack | Persistent storage / notes |
|---|---|---|
postgresql |
Primary relational store (plus pgvector if used as your vector backend) |
Persistent volume at /var/lib/postgresql/data; schedule backups in Backup & Restore |
redis / valkey |
Celery task queue, cache and rate-limit state | Persistent volume if you want queued tasks to survive restarts |
dify-api & dify-worker |
langgenius/dify-api (MODE=api and MODE=worker) |
Shared SECRET_KEY, database URL, Redis URL, and /app/api/storage volume for uploaded files |
dify-web |
langgenius/dify-web Next.js console |
Points CONSOLE_API_URL and APP_API_URL at your public HTTPS domain |
dify-sandbox |
langgenius/dify-sandbox isolated runner for Code nodes |
Reachable only from dify-api / dify-worker over .zenavo.internal |
nginx (or proxy) |
Routes /console/api, /api, /v1, /files → dify-api:5001 and / → dify-web:3000 |
The only service that needs a public domain bound on port 80 |
Check the Zenavo Templates gallery first for one-step AI stacks, or import/create the services in one project on your server.
2. Critical environment variables
Most Dify startup and login issues come down to mismatched URLs or missing secrets between dify-api, dify-worker and dify-web:
SECRET_KEY: generate one strong random string and set the exact same value on bothdify-apianddify-worker. Dify uses it to encrypt stored model provider keys and workspace tokens.- Public URLs on
dify-webanddify-api: once you bind your custom domain (for exampledify.example.com) under Domains & HTTPS, setCONSOLE_API_URL,CONSOLE_WEB_URL,SERVICE_API_URL,APP_API_URLandAPP_WEB_URLtohttps://dify.example.com. - Private database & Redis URLs: point
DB_HOSTatpostgresql.zenavo.internalandREDIS_HOSTatredis.zenavo.internal(orvalkey.zenavo.internal). Neither database needs a public port. - Vector store: if you use PostgreSQL with
pgvector(VECTOR_STORE=pgvector), you can keep both relational data and embeddings in one managed PostgreSQL service that is easy to snapshot via Backups & Restore.
Keep
SECRET_KEYsafe: back upSECRET_KEYin a password manager (such as self-hosted Vaultwarden). ChangingSECRET_KEYlater makes previously saved model credentials in PostgreSQL unreadable.
3. Wire Dify to your own model gateway and search engine
Instead of pasting raw OpenAI or Anthropic keys directly into Dify, you can connect Dify to another self-hosted service in the same Zenavo project over .zenavo.internal:
- Model gateway (LiteLLM, New API, or Magpie): in Dify's Settings → Model Provider → OpenAI-API-compatible, set the API Endpoint URL to
http://litellm.zenavo.internal:4000/v1(or your New API / Magpie internal service address) and paste a virtual key or token. All Dify traffic stays on your server's private network with centralized budgets and spend logs. - Private web search (SearXNG): enable JSON output in SearXNG and point Dify's built-in SearXNG tool at
http://searxng.zenavo.internal:8080so your agents can ground answers in live web results without paid search API quotas. - General workflow automation (n8n): let Dify handle LLM chains, knowledge retrieval and prompt versioning, then trigger n8n webhooks for CRM syncs, Slack alerts or scheduled batch jobs.
4. Initialize the admin account and verify workers
- Open
https://dify.example.com/installright after the services turnRunningand create the initial admin account. - Create a simple Chatbot or Workflow app and upload a small Markdown or PDF file into Knowledge to verify that
dify-workerprocesses the indexing queue cleanly. - If a Knowledge upload stays stuck in
Indexing, open the built-in Zenavo Agent on the right edge of the console and ask it to inspectdify-workerandredislogs—or debug directly from your terminal with Claude Code andzenavo-mcp.
FAQ
Why does Dify show a blank page or fail to log in after setting a domain?
Check that CONSOLE_API_URL and APP_API_URL on dify-web match your https:// domain (without a trailing slash) and that your reverse proxy forwards /console/api and /api to dify-api on port 5001.
Why do uploaded Knowledge documents stay in Queuing or Indexing forever?
dify-worker is either not running, pointing at a different Redis/Valkey instance than dify-api, or missing the shared /app/api/storage volume if dify-api and dify-worker run as separate services.
Should I use Dify or n8n?
Use Dify when the core problem is prompt orchestration, RAG knowledge bases, model comparison and publishing a chat/completion API. Use n8n when you need 400+ SaaS integrations, cron schedules and webhook glue—or pair them together over .zenavo.internal.
Can I migrate an existing Dify setup from Zeabur to Zenavo?
Yes. Export your project template YAML from Zeabur and import it in Zenavo (see Migrate from Zeabur), then restore your PostgreSQL dump via Backup & Restore (see Migrate PostgreSQL from Zeabur) and keep the original SECRET_KEY.
Connect a Linux server or browse the Zenavo Templates gallery to run your AI workflows and knowledge bases on infrastructure you control.
This is what coding agents read, at https://zenavo.app/blog/self-host-dify.md.
Could not load the Markdown. Open it directly: https://zenavo.app/blog/self-host-dify.md