---
title: "Self-Host Dify"
description: "Deploy Dify on Railway with Postgres, Redis, and persistent storage. Build AI applications, RAG pipelines, and agent workflows through Dify's visual interface."
date: 2026-04-14T00:00:00.000Z
updated: 2026-10-29T11:14:02.894Z
topics: ["AI"]
tags: ["Dify", "Docker", "Self Hosted"]
url: https://railway.com/guides/dify
---

# Self-Host Dify

[Dify](https://dify.ai/) is an open-source platform for building LLM-powered applications. It provides a visual workflow editor, built-in RAG pipeline, agent framework, and API management through a web interface.

Dify is a multi-service application that requires an API server, a worker, a web frontend, Postgres, and Redis. Railway is well-suited for this type of deployment since each component runs as a separate service with shared private networking.

Dify calls external LLM APIs (OpenAI, Anthropic, etc.) for model inference. Railway is CPU-based and does not offer GPU instances.

## What you will set up

- Dify API server, worker, and web frontend as separate Railway services
- PostgreSQL and Redis databases
- Persistent storage for uploaded files
- A public domain for the Dify web interface

## One-click deploy from a template

Railway has a Dify template that provisions all services and databases in one step.

[![Deploy on Railway](https://cms.railway.com/media/aeo-6ec9624cf8f8a7408f19e40a27a1e6020de459a42ac3fe1c4a016cb01cd1aa9b.png)](https://railway.com/deploy/V1xiql)

After deploying, skip to [Configure LLM providers](#configure-llm-providers) to connect your API keys.

If you prefer to set things up manually, continue below.

## Manual deployment

### 1. Create the project and databases

1. Create a new [project](https://docs.railway.com/projects) on Railway.
2. Add [PostgreSQL](https://docs.railway.com/databases/postgresql): click **+ New > Database > PostgreSQL**.
3. Add [Redis](https://docs.railway.com/databases/redis): click **+ New > Database > Redis**.

### 2. Deploy the API server

1. Click **+ New > Docker Image**.
2. Enter `langgenius/dify-api` as the image name.
3. Add the following environment variables, using [reference variables](https://docs.railway.com/variables#referencing-another-services-variable) for database connections:

| Variable | Value |
| --- | --- |
| `MODE` | `api` |
| `DB_HOST` | `${{Postgres.PGHOST}}` |
| `DB_PORT` | `${{Postgres.PGPORT}}` |
| `DB_USERNAME` | `${{Postgres.PGUSER}}` |
| `DB_PASSWORD` | `${{Postgres.PGPASSWORD}}` |
| `DB_DATABASE` | `${{Postgres.PGDATABASE}}` |
| `REDIS_HOST` | `${{Redis.REDISHOST}}` |
| `REDIS_PORT` | `${{Redis.REDISPORT}}` |
| `REDIS_PASSWORD` | `${{Redis.REDISPASSWORD}}` |
| `SECRET_KEY` | A random string (use `openssl rand -hex 32`) |
| `STORAGE_TYPE` | `local` |
| `STORAGE_LOCAL_PATH` | `/app/api/storage` |

4. Attach a [Volume](https://docs.railway.com/volumes) mounted at `/app/api/storage` for file uploads.
5. Generate a [public domain](https://docs.railway.com/networking/public-networking#railway-provided-domain) for the API server.

### 3. Deploy the worker

1. Click **+ New > Docker Image**.
2. Enter `langgenius/dify-api` as the image name (same image, different mode).
3. Copy the same environment variables from the API server, but change:

| Variable | Value |
| --- | --- |
| `MODE` | `worker` |

4. No public domain is needed. The worker communicates with Postgres and Redis over [private networking](https://docs.railway.com/networking/private-networking).

### 4. Deploy the web frontend

1. Click **+ New > Docker Image**.
2. Enter `langgenius/dify-web` as the image name.
3. Add the following environment variable:

| Variable | Value |
| --- | --- |
| `CONSOLE_API_URL` | The public URL of your API server (e.g., `https://dify-api-production-xxxx.up.railway.app`) |
| `APP_API_URL` | Same as `CONSOLE_API_URL` |

4. Generate a [public domain](https://docs.railway.com/networking/public-networking#railway-provided-domain) for the web frontend.

## Configure LLM providers

1. Open the web frontend domain in your browser.
2. Create an admin account on first access.
3. Go to **Settings > Model Providers**.
4. Add your LLM API keys (OpenAI, Anthropic, or others).

Dify stores provider credentials encrypted in Postgres.

## Next steps

- [Manage Volumes](https://docs.railway.com/volumes): Resize or back up your persistent storage.
- [Set up a custom domain](https://docs.railway.com/networking/domains): Use your own domain for the Dify interface.
- [Private Networking](https://docs.railway.com/networking/private-networking): How services communicate within a project.
- [Monitor your app](https://docs.railway.com/observability): View logs and metrics for your Dify services.