---
title: "Deploy Sub2API Railway"
description: "Self-host Sub2API on Railway with PostgreSQL, Redis, and a data volume."
category: "AI/ML"
url: https://railway.com/deploy/sub2api-railway
---

# Deploy Sub2API Railway

Self-host Sub2API on Railway with PostgreSQL, Redis, and a data volume.

**[Deploy Sub2API Railway on Railway](https://railway.com/template/sub2api-railway)**

Machine-readable deploy manifest (JSON, validated by TemplateCI): https://railway.com/deploy/sub2api-railway/manifest.json

- **Creator:** Bu Junjie's Projects
- **Category:** AI/ML

## Template content

### Sub2API https://demo.sub2api.org/logo.png

- **Source:** https://github.com/wsbjj/sub2api_railway
- **Health check:** /health
- **Public domain:** Yes

### Postgres https://devicons.railway.app/i/postgresql.svg

- **Image:** ghcr.io/railwayapp-templates/postgres-ssl:18

### Redis https://cdn.sanity.io/images/sy1jschh/production/0ce0bfdcfbdbf69662b1116671f97c2dd788b655-157x157.svg

- **Image:** redis:8.2.1
- **Start command:** `/bin/sh -c "rm -rf $RAILWAY_VOLUME_MOUNT_PATH/lost+found/ && exec docker-entrypoint.sh redis-server --requirepass $REDIS_PASSWORD --save 60 1 --dir $RAILWAY_VOLUME_MOUNT_PATH"`

## Documentation

# Deploy and Host Sub2API on Railway

[![Deploy on Railway](https://railway.com/button.svg)](https://railway.com/deploy/sub2api-railway?utm_medium=integration&utm_source=template&utm_campaign=generic)

在 Railway 上部署 Sub2API AI API Gateway，包含应用服务、PostgreSQL、Redis 和持久化
运行数据。模板使用
[`wsbjj/sub2api_railway`](https://github.com/wsbjj/sub2api_railway) 的
`codex/railway-deploy` 分支。

## About Hosting Sub2API

模板创建一个三服务项目：

- `Sub2API`：从仓库 Dockerfile 构建的 API Gateway 与 Web 管理界面。
- `Postgres`：保存用户、订阅、账号、额度和请求记录。
- `Redis`：提供缓存、会话和异步任务支持。

Sub2API 服务通过 `/health` 健康检查，应用数据持久化到 `/app/data`。PostgreSQL 和
Redis 各自使用 Railway Volume，重新部署应用时数据库不会丢失。

## Why Deploy Sub2API on Railway?

- 用一套托管配置同时运行 Gateway、数据库和缓存。
- 使用来源分支内置的 Railway 入口脚本处理 `PORT` 与 `SERVER_PORT`。
- 通过 Railway 私有网络连接 PostgreSQL 和 Redis，不暴露数据库端口给应用。
- 使用 Railway 域名、日志、部署记录和 Volume 管理长期运行实例。
- 需要更高可用性时，可以在部署后把数据库切换为 Railway HA 集群。

## Common Use Cases

- 统一管理多供应商模型账号和订阅额度。
- 为 OpenAI、Claude、Gemini、Grok 等兼容客户端提供统一 API 入口。
- 为团队或个人提供带 Web 管理界面的 API Gateway。
- 记录请求、用量和账号状态，并用 Redis 支撑缓存与异步任务。
- 把复杂度留在自己的 Railway 项目内，方便审计和迁移。

## Dependencies for Sub2API Hosting

### Deployment Dependencies

- 一个 Railway 项目和服务。
- 仓库 `wsbjj/sub2api_railway` 的 `codex/railway-deploy` 分支。
- 一个 PostgreSQL 服务。
- 一个 Redis 服务。
- 一个 Railway Volume 挂载到 `/app/data`。
- 管理员邮箱、管理员密码、JWT 密钥和 TOTP 加密密钥。

## Required Variables

```env
AUTO_SETUP=true
SERVER_HOST=0.0.0.0
SERVER_MODE=release
DATA_DIR=/app/data
TZ=Asia/Shanghai

DATABASE_HOST=${{Postgres.PGHOST}}
DATABASE_PORT=${{Postgres.PGPORT}}
DATABASE_USER=${{Postgres.PGUSER}}
DATABASE_PASSWORD=${{Postgres.PGPASSWORD}}
DATABASE_DBNAME=${{Postgres.PGDATABASE}}
DATABASE_SSLMODE=disable

REDIS_HOST=${{Redis.REDISHOST}}
REDIS_PORT=${{Redis.REDISPORT}}
REDIS_PASSWORD=${{Redis.REDISPASSWORD}}
REDIS_DB=0
REDIS_ENABLE_TLS=false

ADMIN_EMAIL=your-admin-email@example.com
ADMIN_PASSWORD=change-this-password
JWT_SECRET=replace-with-at-least-32-random-bytes
TOTP_ENCRYPTION_KEY=replace-with-32-random-bytes
```

数据库和 Redis 连接变量由模板自动注入。部署时只需要填写管理员账号和三项密钥。

`SERVER_TRUSTED_PROXIES` 和
`SECURITY_TRUST_FORWARDED_IP_FOR_API_KEY_ACL=true` 已预配置为 Railway 网段，
用于避免反向代理导致会话绑定失败。

## After Deployment

1. 等待 `Sub2API` 服务构建和健康检查通过。
2. 访问 `/health`，确认返回 `{"status":"ok"}`。
3. 打开 Railway 域名，使用管理员邮箱和密码登录。
4. 在管理界面添加上游账号、模型和 API Key。
5. 确认 `/app/data`、PostgreSQL 和 Redis 都挂载了持久化存储。

## Source

- Railway fork and deployment guide: https://github.com/wsbjj/sub2api_railway/tree/codex/railway-deploy
- Deployment guide: https://github.com/wsbjj/sub2api_railway/blob/codex/railway-deploy/docs/RAILWAY_DEPLOY_CN.md
- Upstream project: https://github.com/Wei-Shaw/sub2api


## Similar templates

- [Chat Chat](https://railway.com/deploy/-WWW5r) — Chat Chat, your own unified chat and search to AI platform.
- [Weaviate](https://railway.com/deploy/weaviate-1) — Vector database with hybrid search and an API key-protected API
- [Möbius v0.1 Personal AI OS](https://railway.com/deploy/mobius-v01-personal-ai-os) — Self-hosted AI agent that builds and runs apps for you, with memory.

Open this page in a browser: https://railway.com/deploy/sub2api-railway
