第五章 Archlinux中启动api、celery、web

一、启动api

1.下载dify源码

直接从github下载dify1.1.0的zip格式源码,在windows本地解压。Github网站:https://github.com/langgenius/dify。解压后复制到archlinux中,复制命令如下:

cp -r /mnt/******/dify-1.1.0 ./    # 根据实际的解压路径来
2.安装依赖

在archlinux中,进入 api 目录 cd /home/dify-1.1.0/api。复制环境变量配置文件 cp .env.example .env

设置.env文件

  • 使用openssl rand -base64 42生成SECRET_KEY值,将该值赋值给.env文件中的SECRET_KEY变量。
  • 增加日志,LOG_FILE=./logs/dify.log,方便后续的调试。
  • 由于是本地运行,设置变量CHECK_UPDATE_URL=,禁止dify自动检查最新版本。

添加国内镜像https://pypi.tuna.tsinghua.edu.cn/simple。添加步骤如下:

(1)poetry install 默认从PyPI(Python Package Index)下载Python模块。PyPI是Python社区的主要软件仓库,包含了大量的开源Python包。

(2)在pyproject.toml中配置镜像源:
    查看命令:
    poetry source show
    
    添加命令:
    poetry source add <source_name> <source_url>

	添加一个私有源:
	poetry source add private https://pypi.tuna.tsinghua.edu.cn/simple

    添加有源后,需要运行以下命令
    poetry lock              # 使用poetry lock命令,需要解析依赖目录,时间会比较久。

    # 不运行poetry lock,使用poetry install会报错:
    pyproject.toml changed significantly since poetry.lock was last generated. Run 
    `poetry lock` to fix the lock file.

在api目录中使用 Poetry 安装依赖:

poetry env use 3.12.6
poetry install

# poetry env use 3.12.6命令会指定使用 python 3.12.6,同时在api目录下创建.venv文件,该文件就是python的虚拟环境,项目的python模块是下载到该文件下。

# poetry install命令,将下载pyproject.toml 中列出的模块。安装完成后,可以使用poetry show 命令查看已下载的模块。

执行数据库迁移

在api目录中运行以下命令

poetry run bash
flask db upgrade

poetry run flask db upgrade

运行API服务

  • 开启虚拟环境

    poetry env activate
    

    开启之前可以使用python --version,确认目前的python版本是不是自己指定的,如不是,使用命令pyenv global 3.12.6 更改。

  • 启动API服务(开发环境)

    poetry run flask run --host 0.0.0.0 --port=5001 --debug
    
3.后台启动api

3.1创建systemd服务文件

3.1.1使用 root 或 sudo 权限创建一个新的 systemd 服务文件dify-api.service。

sudo vim /usr/lib/systemd/system/dify-api.service

3.1.2将以下内容写入dify-api.service

[Unit]
Description=Dify API Service
After=network.target postgresql.service redis.service weaviate.service
Requires=postgresql.service redis.service
[Service]
User=root  # 替换为你的实际用户名
Group=root    # 替换为你的实际用户组
WorkingDirectory=/home/dify-1.1.0/api  # 替换为你的实际路径
Environment="PERSISTENCE_DATA_PATH=/home/dify-1.1.0/data/difydata"
Environment="QUERY_DEFAULTS_LIMIT=25"
Environment="AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED=false"
Environment="DEFAULT_VECTORIZER_MODULE=none"
Environment="AUTHENTICATION_APIKEY_ENABLED=true"
Environment="AUTHENTICATION_APIKEY_ALLOWED_KEYS=WVF5YThaHlkYwhGUSmCRgsX3tD5ngdN8pkih"
Environment="AUTHENTICATION_APIKEY_USERS=hello@dify.ai"
Environment="AUTHORIZATION_ADMINLIST_ENABLED=true"
Environment="AUTHORIZATION_ADMINLIST_USERS=hello@dify.ai"
	
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/root/.local/bin"
ExecStart=/usr/bin/bash -c 'source $(poetry env info --path)/bin/activate && gunicorn -b 0.0.0.0:5001 --workers 1 --worker-class gevent --timeout 240  app:app'

Restart=on-failure
RestartSec=30
StartLimitInterval=300s

[Install]
WantedBy=multi-user.target

gunicorn启动优化

# 在dify-api.service修改以下内容
删除以下变量:
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/root/.local/bin"

更改ExecStart为以下设置:
ExecStart=/home/dify-1.1.0/api/.venv/bin/gunicorn -b 0.0.0.0:5001 --workers 1 --worker-class gevent --timeout 240  app:app

如果CPU的核心数大于4,可以设置 "--workers 4"

3.1.3设置开机后启动

sudo systemctl enable --now dify-api.service

# 运行命令后,自动创建软连接
Created symlink '/etc/systemd/system/multi-user.target.wants/dify-api.service' → '/usr/lib/systemd/system/dify-api.service'.

3.1.4重新加载 systemd

sudo systemctl daemon-reload

3.1.5启用并启动服务

sudo systemctl enable dify-api.service
# sudo systemctl start dify-api.service
sudo systemctl restart dify-api.service
# 关闭
sudo systemctl stop dify-api.service

3.1.6检查服务状态

sudo systemctl status dify-api.service

3.1.7查看日志

journalctl -xeu dify-api.service  # 查看运行日志
journalctl -u dify-api.service -f # 查看实时日志

3.1.8其他

在虚拟环境中手动启动

cd /path/to/dify0153/api
source $(poetry env info --path)/bin/activate

flask run --host 0.0.0.0 --port=5001
或者:
gunicorn -b 0.0.0.0:5001 app:app

二、启动celery

1.启动celery

在api目录下,使用poetry env activate开启虚拟环境,然后运行以下命令

Linux环境中启动:
poetry run celery -A app.celery worker -P gevent -c 1 -Q dataset,generation,mail,ops_trace --loglevel INFO
2.后台启动celery

2.1创建 systemd 服务文件

2.1.1使用 root 或 sudo 权限创建一个新的 systemd 服务文件celery-dify.service

sudo vim /usr/lib/systemd/system/celery-dify.service

2.1.2写入以下配置(根据你的环境修改)

[Unit]
Description=Celery Worker for Dify API
After=network.target postgresql.service redis.service weaviate.service
Requires=postgresql.service redis.service

[Service]
User=root  # 替换为你的 Linux 用户名
Group=root    # 替换为你的用户组
WorkingDirectory=/home/dify-1.1.0/api  # 替换为你的项目路径
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/root/.local/bin"

# 使用 Poetry 虚拟环境启动 Celery
ExecStart=/usr/bin/bash -c 'source $(poetry env info --path)/bin/activate && poetry run celery -A app.celery worker -P gevent -c 1 -Q dataset,generation,mail,ops_trace --loglevel INFO'

Restart=on-failure
RestartSec=20s
StartLimitInterval=300s
		
[Install]
WantedBy=multi-user.target

celery-dify.service文件的优化

删除以下内容:
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/root/.local/bin"

ExecStart 使用绝对路径:
ExecStart=/root/.local/bin/poetry run celery -A app.celery worker -P gevent -c 1 -Q dataset,generation,mail,ops_trace --loglevel INFO

或者:
ExecStart=/home/dify-1.1.0/api/.venv/bin/celery -A app.celery worker -P gevent -c 1 -Q dataset,generation,mail,ops_trace --loglevel INFO

2.1.3启用并启动 Celery 服务

# 重新加载 systemd 配置
	sudo systemctl daemon-reload

# 设置开机自启
	sudo systemctl enable celery-dify.service
# 运行该命令后,自动创建软链接
Created symlink '/etc/systemd/system/multi-user.target.wants/celery-dify.service' → '/usr/lib/systemd/system/celery-dify.service'.

# 立即启动 Celery
	sudo systemctl start celery-dify.service
或 sudo systemctl restart celery-dify.service
# 关闭
	sudo systemctl stop celery-dify.service

2.1.4检查 Celery 运行状态

# 查看服务状态
	sudo systemctl status celery-dify.service
# 查看实时日志
	journalctl -u celery-dify.service -f

2.1.5其他

验证celery和redis的链接,查看api目录下.env文件的配置信息。

# celery configuration
CELERY_BROKER_URL=redis://:difyai123456@localhost:6379/1

三、启动web

1.启动web

进入web目录cd /home/dify-1.1.0/web,在web目录下创建文件 .env.local,并复制.env.example中的内容。

cp .env.example  .env.local

根据需求修改.env.local文件中的环境变量的值:

将NEXT_PUBLIC_DEPLOY_ENV=DEVELOPMENT,更改为NEXT_PUBLIC_DEPLOY_ENV=PRODUCTION

# For production release, change this to PRODUCTION
NEXT_PUBLIC_DEPLOY_ENV=DEVELOPMENT
# The deployment edition, SELF_HOSTED
NEXT_PUBLIC_EDITION=SELF_HOSTED
# The base URL of console application, refers to the Console base URL of WEB service if console domain is
# different from api or web app domain.
# example: http://cloud.dify.ai/console/api
NEXT_PUBLIC_API_PREFIX=http://localhost:5001/console/api
# The URL for Web APP, refers to the Web App base URL of WEB service if web app domain is different from
# console or api domain.
# example: http://udify.app/api
NEXT_PUBLIC_PUBLIC_API_PREFIX=http://localhost:5001/api

# SENTRY
NEXT_PUBLIC_SENTRY_DSN=
NEXT_PUBLIC_SENTRY_ORG=
NEXT_PUBLIC_SENTRY_PROJECT=

安装PNPM

新版本的dify,可能是要求使用**pnpm 作为包管理器**

npm i -g pnpm

安装依赖

pnpm install --frozen-lockfile

启动web服务(要求在web目录下运行命令)

npm命令:
(1)生产模式
		npm run build    # 先构建代码 
		npm run start	 # 启动

(2)开发模式
	    npm run dev
	
pnpm命令:
	pnpm build      # 先构建代码 
	pnpm start      # 启动
	

可能的报错:

问题 解决方法 备注
[root@L web]# npm run build

> dify-web@1.1.0 build
> next build

sh: line 1: next: command not found
npm install

安装 package.json 中列出的所有依赖,包括 next
第一次是 npm run build
,需要先使用npm install
安装依赖
使用npm install
安装依赖时,仍然有报错
npm install -g pnpm # 全局安装 pnpm

然后使用pnpm install --frozen-lockfile
新版本的dify,可能是要求使用**pnpm 作为包管理器**
2.后台启动web

2.1创建systemd服务文件

2.1.1使用 root 或 sudo 权限创建一个新的 systemd 服务文件dify-web.service

sudo vim /usr/lib/systemd/system/dify-web.service

2.1.2 写入以下配置(根据你的需求修改)

[Unit]
Description=Dify Web Node.js Service
After=network.target postgresql.service redis.service weaviate.service
Requires=postgresql.service redis.service

[Service]
User=root  # 替换为你的 Linux 用户名
Group=root    # 替换为你的用户组
WorkingDirectory=/home/dify-1.1.0/web     # 替换为你的项目路径

# 使用 node 直接运行 npm-cli.js
ExecStart=/root/.nvm/versions/node/v22.14.0/bin/pnpm start

Restart=on-failure
RestartSec=30
StartLimitInterval=300s

[Install]
WantedBy=multi-user.target

ExecStart中可以使用以下命令:

ExecStart=/root/.nvm/versions/node/v22.14.0/bin/node /root/.nvm/versions/node/v22.14.0/bin/pnpm run start

保证已经创建以下软链接

如果没有创建以下软连接,使用node、npm、pnpm、命令会报错。

ln -s /root/.nvm/versions/node/v22.14.0/bin/node /usr/local/bin/node
ln -s /root/.nvm/versions/node/v22.14.0/bin/npm /usr/local/bin/npm
ln -s /root/.nvm/versions/node/v22.14.0/bin/npm /usr/local/bin/pnpm

2.1.3启用并启动服务

sudo systemctl daemon-reload
sudo systemctl enable dify-web.service  # 开机自启

# 运行命令sudo systemctl enable dify-web.service后,自动创建软链接:Created symlink '/etc/systemd/system/multi-user.target.wants/dify-web.service' → '/usr/lib/systemd/system/dify-web.service'.

sudo systemctl start dify-web.service  # 立即启动

# 关闭
sudo systemctl stop dify-web.service 

2.1.4检查服务状态

sudo systemctl status dify-web.service
journalctl -u dify-web.service -f  # 查看实时日志
journalctl -xeu dify-web.service  # 查看日志

2.1.5访问web

在windows系统中的浏览器,访问 http://localhost:3000。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐