dify本地源码部署-详细教程
相关资源包,ubuntu版本需要大于23.1版本,vm的话需要大于16版本虚拟机安装具体教程:https://blog.csdn.net/2401_83830408/article/details/147416235相关资源包:VMware Workstation Pro:通过网盘分享的文件:VMware Workstation Pro 17.6.0.rar链接: https://pan.baid
一:安装虚拟机
提示:相关资源包,ubuntu版本需要大于23.1版本,vm的话需要大于16版本
虚拟机安装具体教程:https://blog.csdn.net/2401_83830408/article/details/147416235
相关资源包:
VMware Workstation Pro:通过网盘分享的文件:VMware Workstation Pro 17.6.0.rar
链接: https://pan.baidu.com/s/1MHhmzjDMTbBYBibM52hYWw 提取码: p52p
ubuntu镜像太大了,各位自行网上下载
二,虚拟机前期准备工作
1,虚拟机安装vmware tools,一般可以直接从VMware Workstation Pro,如果按钮置灰请打开虚拟机终端,运行下述代码(因为有的系统版本高,vmware不支持安装 vmware tools):
sudo apt update && sudo apt install open-vm-tools-desktop -y
sudo reboot
2,先检查磁盘空间(建议40g):
df -h /tmp /var /home
3,安装一系列插件
sudo apt-get --yes --purge autoremove
sudo apt-get --yes clean all
sudo apt-get --yes purge $(dpkg -l | awk '/^rc/ { print $2 }')
sudo snap refresh --list
sudo snap refresh
sudo apt update
sudo apt --yes upgrade
4,删除一些无用资源
sudo snap remove firefox
sudo snap remove firmware-updater
sudo snap remove thunderbird
sudo apt-get remove --yes unattended-upgrades ubuntu-report apport python3-apport whoopsie
sudo apt-get remove --yes update-manager update-manager-core ubuntu-release-upgrader-core
sudo apt-get remove --yes firefox thunderbird
sudo apt-get remove --yes openvpn cloud-init
sudo apt-get remove --yes libreoffice*
sudo snap refresh --hold
sudo snap refresh --time
5,禁用相关服务
sudo systemctl disable apt-daily-upgrade.timer
sudo systemctl disable apt-daily.timer
sudo systemctl disable fwupd-refresh.timer
sudo systemctl disable motd-news.timer
sudo systemctl disable ua-reboot-cmds.service
sudo systemctl disable ua-timer.timer
sudo systemctl disable ubuntu-advantage.service
sudo systemctl mask apt-daily.service
sudo systemctl mask fwupd.service
sudo systemctl mask geoclue.service
sudo systemctl mask ua-timer.service
6,删除相关目录
sudo rm -f /var/log/apport.log
sudo rm -rf /home/abc/.cache/ubuntu-report
sudo rm -rf /home/abc/.cache/update-manager-core
sudo rm -rf /home/abc/.cache/vmware/drag_and_drop
sudo rm -rf /home/abc/.config/update-notifier
sudo rm -rf /var/lib/update-notifier
sudo rm -rf /var/log/unattended-upgrades
7,正式安装所需插件(以下有些配置可以选择性执行,比如数据库配置或者其他配置,不用完全照抄)
sudo apt-get --yes install open-vm-tools-desktop
sudo apt-get --yes install python3 python3-pip git curl net-tools
sudo apt-get --yes install gnupg2 ca-certificates lsb-release ubuntu-keyring
sudo apt-get --yes install openssh-server dconf-editor
# 安装pipx
sudo apt-get --yes install pipx
pipx ensurepath
# 会报错
sudo pipx ensurepath --global # optional to allow pipx actions with --global argument
# 安装 poetry
pipx install poetry
pipx upgrade poetry
# 安装pg
https://www.postgresql.org/download/linux/ubuntu/
sudo apt update
sudo apt install -y postgresql-common
sudo apt update
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
sudo apt-get --yes install postgresql-17 postgresql-17-pgvector
# 如果没有此行,表示安装版本不对
sudo nano /etc/apt/sources.list.d/pgdg.sources
Architectures: amd64
# 修改pgsql文件,使其监听所有ip
sudo nano /etc/postgresql/17/main/postgresql.conf
listen_address = '*'
sudo nano /etc/postgresql/17/main/pg_hba.conf
# 监听内容
host all all 0.0.0.0/0 scram-sha-256
host all all ::0/0 scram-sha-256
sudo systemctl restart postgresql
# 启动服务
sudo systemctl start postgresql
# 查看服务状态(应显示active (running))
sudo systemctl status postgresql
# 设置开机自启
sudo systemctl enable postgresql
sudo -i -u postgres
# 登录PostgreSQL命令行
psql
# 创建用户并设置密码
CREATE USER dify WITH PASSWORD 'dify123';
# 授予创建数据库的权限(可选)
ALTER USER dify WITH CREATEDB;
# 创建数据库并指定所有者
CREATE DATABASE dify_db OWNER dify ;
CREATE DATABASE dify_retry OWNER dify ;
CREATE DATABASE dify_rs OWNER dify ;
CREATE DATABASE dify_vector OWNER dify ;
psql -h localhost -d mydatabase -U myuser -p <port>
sudo -u postgres psql test
CREATE EXTENSION vector;
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
# 安装node.js
curl -sL https://deb.nodesource.com/setup_24.x | sudo -E bash -
sudo vi /etc/apt/sources.list.d/nodesource.list
sudo apt-get install nodejs
sudo npm config set -g audit false
sudo npm config set -g browser false
sudo npm config set -g fund false
sudo npm config set -g save-exact true
sudo npm config set -g update-notifier false
# 安装redis
sudo apt-get --yes install redis-server
sudo vi /etc/redis/redis.conf
sudo systemctl restart redis-server.service
sudo systemctl enable redis-server.service
redis-cli
redis-cli config get protected-mode
# 开放端口号
sudo nano /etc/redis/redis.conf
# 允许外部连接(默认绑定 127.0.0.1)
bind 0.0.0.0
# 确保以下配置存在(默认应为 yes)
protected-mode no
# 重启服务
sudo systemctl restart redis
三,接入dify
dify操作手册:产品简介 - Dify Docs
1,先clone对应的代码
git clone https://github.com/langgenius/dify
git clone https://github.com/langgenius/dify-plugin-daemon
git clone https://github.com/langgenius/dify-sandbox
2,安装dify相关的插件
sudo apt-get --yes install ffmpeg
sudo apt-get --yes install pkg-config gcc libseccomp-dev
sudo snap install go --classic
# 最后安装模型的时候需要uv 切记,需要ubuntu 23版本以上 并且VMware需要 15以上
pip install uv --user --break-system-packages
pip install uv --break-system-packages
3,更改相关应用的配置文件
api(.env):
更换成本地ip,因为要开放给外部使用(非127.0.0.1):
CONSOLE_API_URL
CONSOLE_WEB_URL
SERVICE_API_URL
APP_WEB_URL
FILES_URL
WEB_API_CORS_ALLOW_ORIGINS
CONSOLE_CORS_ALLOW_ORIGINS
WEAVIATE_ENDPOINT
COUCHBASE_CONNECTION_STRING
MILVUS_URI
MYSCALE_HOST
RELYT_HOST
RELYT_USER
RELYT_PASSWORD
RELYT_DATABASE
TENCENT_VECTOR_DB_URL
ELASTICSEARCH_HOST
TIDB_ON_QDRANT_URL
TIDB_API_URL
TIDB_IAM_API_URL
CHROMA_HOST
OPENSEARCH_HOST
BAIDU_VECTOR_DB_ENDPOINT
MATRIXONE_HOST
OCEANBASE_VECTOR_HOST
OPENGAUSS_HOST
CODE_EXECUTION_ENDPOINT
PLUGIN_DAEMON_URL
PLUGIN_REMOTE_INSTALL_HOST
ENDPOINT_URL_TEMPLATE
OTLP_BASE_ENDPOINT
按照本地redis配置更改:
REDIS_HOST
REDIS_PASSWORD
CELERY_BROKER_URL
按照本地pg配置更改:
DB_USERNAME
DB_PASSWORD
DB_HOST
DB_DATABASE
PGVECTO_RS_HOST
PGVECTO_RS_PORT
PGVECTO_RS_USER
PGVECTO_RS_PASSWORD
PGVECTO_RS_DATABASE
PGVECTOR_HOST
PGVECTOR_PORT
PGVECTOR_USER
PGVECTOR_PASSWORD
PGVECTOR_DATABASE
按照指定要求更改
VECTOR_STORE = pgvector
LOG_TZ=Asia/Shanghai
web(.env.local):
更换成本地ip即可
NEXT_PUBLIC_API_PREFIX
NEXT_PUBLIC_PUBLIC_API_PREFIX
dify-plugin-daemon(.env):
按照自己相关配置更改即可
REDIS_PASSWORD
DB_USERNAME
DB_PASSWORD
dify-sandbox:
conf/config.yaml:
python_path: /usr/local/bin/python3 -> python_path: /usr/bin/python3
internal/static/config_default_amd64.go:
/usr/local/lib/python3.10 -> /usr/bin/python3.12
/usr/lib/python3.10 -> /usr/lib/python3.12
4,启动dify相关应用
1,api
# 导航到 api 目录
cd api
# 首次启动:
# 准备环境变量配置文件(改过一次即可)
cp .env.example .env
# 生成随机密钥并替换 .env 文件中的 SECRET_KEY 值
awk -v key="$(openssl rand -base64 42)" '/^SECRET_KEY=/ {sub(/=.*/, "=" key)} 1' .env > temp_env && mv temp_env .env
# 安装依赖
uv sync
# 执行数据库迁移 执行数据库迁移到最新版本:
uv run flask db upgrade
# 后续启动
# 启动 API 服务
uv run flask run --host 0.0.0.0 --port=5001 --debug
# 启动worker服务,异步线程处理(新打开一个终端执行下述命令)
uv run celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail,ops_trace
预期输出:
* Debug mode: on
INFO:werkzeug:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:5001
INFO:werkzeug:Press CTRL+C to quit
INFO:werkzeug: * Restarting with stat
WARNING:werkzeug: * Debugger is active!
INFO:werkzeug: * Debugger PIN: 695-801-919
2,web
cd ~/Desktop/dify/web
# 首次启动:
pnpm install --frozen-lockfile
# 在当前目录中创建一个名为 .env.local 的文件,并从 .env.example 复制内容。根据您的需求修改这些环境变量的值:
# 后续启动
pnpm build
pnpm run start --host=0.0.0.0
3,dify-plugin-daemon
cd ~/Desktop/dify-plugin-daemon
go run ./cmd/server/main.go
4,dify-sandbox
cd ~/Desktop/dify-sandbox
# 首次启动
./install.sh
bash build/build_amd64.sh
# 后续启动
sudo ./main
然后根据启动web里面的链接打开浏览器,就能够成功访问啦~~~~
四:遇到的问题
导入知识库文件
错误异常:
cur.execute("CREATE EXTENSION IF NOT EXISTS vector")
psycopg2.errors.InsufficientPrivilege: 错误: 创建扩展 "vector" 权限不够
HINT: 只有超级用户能创建扩展.
解决步骤:
-- 方法一:使用超级用户创建扩展
CREATE EXTENSION IF NOT EXISTS vector;
-- 方法二:授予当前用户权限
ALTER USER your_username WITH SUPERUSER;
撤销权限(如果需要撤销):
ALTER USER your_username WITH NOSUPERUSER;
虚拟机时间跟主机时间不一致
sudo timedatectl set-ntp true # 启用 NTP
sudo systemctl restart systemd-timesyncd
timedatectl status # 检查状态
更多推荐
所有评论(0)