☁️ 阿里云部署 Dify 完整指南
2026 年开源 LLM 应用开发平台实战教程 - 从零到上线的全流程解析
2026 年开源 LLM 应用开发平台实战教程 - 从零到上线的全流程解析
Dify 是一个开源的 LLM 应用开发平台,它简化了从原型到生产的整个流程。相比传统的开发方式,Dify 提供了:
对于企业而言,自部署 Dify 意味着:
根据应用场景的不同,推荐以下配置方案:
适用场景:个人学习、小型内部工具
ECS 配置:2 核 CPU / 4GB 内存 / 50GB SSD
预估成本:约 ¥200-300/月(按量付费)
适用场景:中小型企业、多个 AI 应用并发
ECS 配置:4 核 CPU / 8GB 内存 / 100GB SSD
预估成本:约 ¥400-600/月
适用场景:高并发、多用户、关键业务系统
ECS 配置:8 核 CPU / 16GB 内存 / 200GB SSD + RDS MySQL
预估成本:约 ¥1000-2000/月
确保目标服务器已安装以下基础软件:
虽然本地测试可以使用 IP 地址,但正式部署强烈建议:
dify.yourcompany.com)图 1: Dify 在阿里云上的典型部署架构
# SSH 登录到服务器
ssh root@your-server-ip
# 更新系统包
apt update && apt upgrade -y
# 安装基础工具
apt install -y curl git wget vim htop net-tools
# 设置时区为上海
timedatectl set-timezone Asia/Shanghai
# 安装 UFW 防火墙
apt install -y ufw
# 允许 SSH 连接
ufw allow 22/tcp
# 允许 HTTP/HTTPS
ufw allow 80/tcp
ufw allow 443/tcp
# 启用防火墙
ufw --force enable
# 查看防火墙状态
ufw status
# 卸载旧版本(如果有)
apt remove docker docker-engine docker.io containerd runc
# 安装依赖包
apt install -y ca-certificates curl gnupg lsb-release
# 添加 Docker GPG 密钥
mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# 设置仓库
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
# 安装 Docker
apt update
apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
# 验证安装
docker --version
docker compose version
# 配置 Docker 镜像加速器
cat << EOF | sudo tee /etc/docker/daemon.json
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
EOF
# 重启 Docker 服务
systemctl daemon-reload
systemctl restart docker
# 验证配置
docker info | grep Mirrors
# 创建自定义网络
docker network create dify-network
# 创建数据目录
mkdir -p /opt/dify/{data,logs,config}
chmod 755 /opt/dify
# 设置所有权(可选,如果当前用户是 root 可跳过)
chown -R www-data:www-data /opt/dify
# 切换到部署目录
cd /opt/dify
# 克隆 Dify 仓库
git clone https://github.com/langgenius/dify.git .
# 切换到稳定版本
git checkout v0.6.13
# 查看目录结构
ls -la
# 复制环境变量模板
cp .env.example .env
# 编辑配置文件
vim .env
需要修改的关键配置项:
APP_SECRET_KEY:生成随机字符串作为应用密钥CONSOLE_API_URL:设置为你的域名,如 https://dify.yourcompany.comSERVICE_API_URL:同上FILES_URL:文件访问 URLMYSQL_DATABASE、MYSQL_USERNAME、MYSQL_PASSWORD:数据库配置REDIS_PASSWORD:Redis 密码# 生成 APP_SECRET_KEY
openssl rand -base64 42
# 生成数据库密码
openssl rand -hex 16
# 生成 Redis 密码
openssl rand -base64 32
# 进入部署目录
cd /opt/dify
# 拉取所有 Docker 镜像
docker compose pull
# 启动所有服务(后台运行)
docker compose up -d
# 查看服务状态
docker compose ps
# 查看日志(前 100 行)
docker compose logs --tail=100
Dify 启动需要 2-5 分钟,可以通过以下命令监控:
# 持续查看日志直到看到成功提示
docker compose logs -f | grep "Application startup complete"
http://your-server-ip:3000Dify 默认会在 Docker Compose 中自动启动一个 PostgreSQL 容器,适用于开发和测试。
# 在阿里云控制台创建 RDS 实例
# 选择 PostgreSQL 引擎,版本 13 或更高
# 实例规格:2 核 4GB 起步
# 存储类型:ESSD PL1
# 获取连接信息
# 控制台 → 实例详情 → 连接信息
# 记录:主机地址、端口、账号、密码
# 修改 .env 文件中的数据库配置
MYSQL_HOST=rdsxxxxx.rds.aliyuncs.com
MYSQL_PORT=5432
MYSQL_USER=dify_user
MYSQL_PASSWORD=your_secure_password
MYSQL_DATABASE=dify
Redis 用于存储会话、临时数据和缓存结果:
# 修改 Redis 配置
REDIS_HOST=dify-redis
REDIS_PORT=6379
REDIS_PASSWORD=your_redis_password
REDIS_DB=0
# 生产环境建议
# 使用阿里云 Redis 实例替代容器内 Redis
# 开启持久化(AOF)
# 设置合适的内存限制(如 2GB)
curl -X POST https://dify.yourcompany.com/v1/chat-messages \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"inputs": {},
"query": "你好,请介绍一下你自己",
"response_mode": "blocking",
"user": "test-user-001"
}'
from dify_client import DifyChatClient
client = DifyChatClient(
api_key="YOUR_API_KEY",
base_url="https://dify.yourcompany.com"
)
response = client.chat(
query="如何用 Python 读取 CSV 文件?",
user="developer-001"
)
print(response.get("answer"))
const axios = require('axios');
async function callDifyAPI(question) {
const response = await axios.post(
'https://dify.yourcompany.com/v1/chat-messages',
{
inputs: {},
query: question,
response_mode: 'streaming',
user: 'nodejs-app-001'
},
{
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
}
}
);
return response.data;
}
// 调用示例
callDifyAPI('如何部署 Docker 容器?')
.then(result => console.log(result))
.catch(error => console.error(error));
server {
listen 80;
server_name dify.yourcompany.com;
# 重定向到 HTTPS
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name dify.yourcompany.com;
# SSL 证书配置
ssl_certificate /etc/nginx/ssl/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
# WebSocket 支持
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 超时设置
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# 请求头
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;
}
# 安装 Prometheus 监控
docker run -d \
--name=prometheus \
-p 9090:9090 \
-v /opt/prometheus:/prometheus \
prom/prometheus:latest
# 配置 Grafana 可视化
docker run -d \
--name=grafana \
-p 3001:3000 \
grafana/grafana:latest
请按以下步骤排查:
docker compose ps,确认所有服务都是 Running 状态docker compose logs web,查找错误信息netstat -tlnp | grep 3000,确认端口未被占用ufw status,确保未阻止相关端口升级步骤:
docker compose down
docker run --rm -v dify_mysql_data:/var/lib/mysql mysql:8.0 mysqldump -uroot -p${MYSQL_ROOT_PASSWORD} > backup.sql
cd /opt/dify
git fetch --tags
git checkout v0.6.14 # 替换为目标版本
docker compose down
docker compose pull
docker compose up -d
Dify 支持通过 API 接入任何兼容 OpenAI 格式的模型:
支持的国内模型包括:通义千问、文心一言、智谱 AI、零一万物等。
针对大文件处理,建议:
.env 中设置 UPLOAD_FILE_SIZE_LIMIT=100(单位 MB)性能优化措施:
通过本文的详细指导,您已经掌握了在阿里云上部署 Dify 的完整流程。以下是关键要点回顾:
部署完成后,您可以:
如果在部署过程中遇到问题,可以:
祝您部署顺利,早日构建出优秀的 AI 应用!