返回上一頁

OpenWebUI & Dify 佈署

前期準備

  • 作業系統:預計以 Ubuntu Linux 20.04 LTS 搭配 docker 提供服務

  • 評估環境

    • 評估環境位於 Azure, 使用 B1ms Size, Ubuntu 20.04 Minimal 映像, 建立過程中搭配進階 NSG 以及固定 IP, 並產製 rsa ssh key
    • NSG 指定 22,80,443, 3000, 8080 port 允許辦公室 IP 連入
      • 除 22 以外, 為開放 web 服務的 port, 但我想應先完成初始設置後才調整為 Allow Any
    • 指定固定 IP 的 DNS 名稱, 以利通過公司防火牆
  • 正式環境

    • 台智雲
    • 靜態 IP 須向 MIS 提出申請
    • 後續待移轉時紀錄

環境安裝 & 設定

Docker

docker 安裝參考 docker docs 的 https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository

並於完成 docker 安裝後處理 Linux post-installation steps for Docker Engine

Ubuntu 只要處理 Manage Docker as a non-root user 就好

OpenWebUI

使用 docker 執行

docker pull ghcr.io/open-webui/open-webui:main
docker run -d -p 3000:8080 -v open-webui:/app/backend/data --name open-webui ghcr.io/open-webui/open-webui:main

如此會產生一個具有以下特性的 container instance

  • 使用 ghcr.io/open-webui/open-webui:main 為基礎映像
  • 名稱為 open-webui
  • 對外開放 port 3000 連入
  • 掛載一個名為 open-webui 的 volume

完成指令後, 可由辦公室的電腦開啟瀏覽器測試是否可連線至 http://unit-openwabai.japaneast.cloudapp.azure.com:3000

使用 docker-compose 執行

由於要提供正式服務,不可使用 ghcr.io/open-webui/open-webui:main 這種浮動 tag,應該要使用指定版本號的 tag

以下為指定 v0.6.5 版本的 docker-compose.yaml

version: '3.8'
services:
  openwebui:
    # 正式提供服務應指定版本號, 不用 main, latest, 以確保執行穩定 
    # 清單可自此取得https://github.com/open-webui/open-webui/pkgs/container/open-webui/versions?filters%5Bversion_type%5D=tagged&page=1
    # https://github.com/open-webui/open-webui/pkgs/container/open-webui/394913177?tag=v0.6.5
    image: ghcr.io/open-webui/open-webui:v0.6.5 
    # 不開 port mapping, 只向 localhost 開放 8080 port, 後面讓 nginx 處理
    # ports:
    #   - "3000:8080"
    expose:
      - "8080"
    restart: always      
    volumes:
      - open-webui:/app/backend/data
volumes:
  open-webui:

Web 介面設定

在前一個步驟開啟的頁面中, 依照畫面說明進行初始設定, 建立管理員帳號

Connect Model

因沒有使用離線的 model, 我們需要加入自訂 的 OpenAI 相容 API 端點, 進到對話視窗頁面後, 點選右上角的管理員名稱 -> 設定, 來設定連線

1.00

1.00

AOAI
docker run -p 8080:8080 --name=azure-openai-proxy \
  --env AZURE_OPENAI_ENDPOINT=https://aoai-endpoint.openai.azure.com/ \
  --env AZURE_OPENAI_API_VER="2024-02-15-preview" \
  --env AZURE_OPENAI_MODEL_MAPPER="gpt-35-turbo=deploy35,gpt-4=deploygpt4" \
  stulzq/azure-openai-proxy:latest

帳號管理

OpenWebUI 的帳號建立支援 csv 匯入,csv 範例檔名為 user-import.csv csv 格式如下:

Name,Email,Password,Role

Name: 使用者名稱, 後續可更改, 名字可有空格 Email: 使用者Email, 後續不可更改 Password: 使用者密碼, 看起來沒有強度下限 Role: 使用者角色, 有 pending, user, admin 三者可選, 預設給 user 就好

pending 使用者可以登入, 但會看到空白畫面, 待處理帳號得 admin 成員去點帳號的 待處理 一下, 才會變成 user

更新 & 重啟

參考官方文件:https://docs.openwebui.com/getting-started/quick-start/#updating

Dify

使用 docker compose 執行

git clone https://github.com/langgenius/dify.git # 應該要指定 release tag

cd dify/docker

cp .env.example .env

docker compose up -d

.env 內,至少要完成下列設定,才可以讓 dify 正常運作

# Common Variables
# 結尾不用 /
CONSOLE_WEB_URL=http://dify.example.com
APP_WEB_URL=http://dify.example.com

# Server Configuration
# 如果服務跑起來就直接對外公開, 應先設置 admin init password
INIT_PASSWORD=your-p@ssw0rd

# Mail related configuration
MAIL_TYPE=smtp
MAIL_DEFAULT_SEND_FROM=gary@example.com
SMTP_SERVER=mail.example.com
SMTP_PORT=465
SMTP_USERNAME=gary
SMTP_PASSWORD=P@ssw0rd

# Docker Compose Service Expose Host Port Configurations
EXPOSE_NGINX_PORT=127.0.0.1:8081
EXPOSE_NGINX_SSL_PORT=127.0.0.1:44381

Web 介面設定

連線下列網址, 進行初次 Admin 登入

http://unit-openwabai.japaneast.cloudapp.azure.com/install

完成後以下列網址登入

http://unit-openwabai.japaneast.cloudapp.azure.com

帳號管理

Dify 建立帳號時採 email 啟用的方式,要先確認 .env 中與 email 相關的設定都有設定好, 另外 CONSOLE_WEB_URL 會作為 mail template 的變數提供給 user,務必設定

配置其他服務

searxng 搜尋工具

https://docs.dify.ai/zh-hans/guides/tools/tool-configuration/searxng

http://host.docker.internal:8081 在 linux 上無法使用, 所以無法在 linux 上同時起 dify 跟 searxng, 除非修改兩邊的 docker compose, 但初步嘗試看起來串不通

git clone https://github.com/searxng/searxng-docker.git
cd searxng-docker

# 調整 ultrasecretkey, 產生一個亂數來當 key
sed -i "s|ultrasecretkey|$(openssl rand -hex 32)|g" searxng/settings.yml

github 上的 readme 提示到: On the first run, you must remove cap_drop: - ALL from the docker-compose.yaml file for the searxng service to successfully create /etc/searxng/uwsgi.ini. This is necessary because the cap_drop: - ALL directive removes all capabilities, including those required for the creation of the uwsgi.ini file. After the first run, you should re-add cap_drop: - ALL to the docker-compose.yaml file for security reasons.

可以用 vs code 搭配 Remote - SSH (ms-vscode-remote.remote-ssh) 來連進主機進行修改

另外, 如果只是在單機提供服務給 dify 使用, 可以不需要使用 caddy, 把 docker-compose.yaml 內的 caddy 整段刪除

調整後的 docker-compose.yml 如下

version: "3.7"

services:
  redis:
    container_name: redis
    image: docker.io/valkey/valkey:8-alpine
    command: valkey-server --save 30 1 --loglevel warning
    restart: unless-stopped
    networks:
      - searxng
    volumes:
      - valkey-data2:/data
    cap_drop:
      - ALL
    cap_add:
      - SETGID
      - SETUID
      - DAC_OVERRIDE
    logging:
      driver: "json-file"
      options:
        max-size: "1m"
        max-file: "1"

  searxng:
    container_name: searxng
    image: docker.io/searxng/searxng:latest
    restart: unless-stopped
    networks:
      - searxng
    ports:
      - "127.0.0.1:8080:8080"
    volumes:
      - ./searxng:/etc/searxng:rw
    environment:
      - SEARXNG_BASE_URL=https://${SEARXNG_HOSTNAME:-localhost}/
      - UWSGI_WORKERS=${SEARXNG_UWSGI_WORKERS:-4}
      - UWSGI_THREADS=${SEARXNG_UWSGI_THREADS:-4}
    cap_drop:
       - ALL
    cap_add:
      - CHOWN
      - SETGID
      - SETUID
    logging:
      driver: "json-file"
      options:
        max-size: "1m"
        max-file: "1"

networks:
  searxng:

volumes:
  valkey-data2:

完成調整後可正確啟動 searxng2 但使用 dify 官方說明文件的查詢蘋果指令, 會回應 429 TOO MANY REQUEST

curl "http://localhost:8080/search?q=apple&format=json&categories=general"

此為 searxng/settings.yml 內的 limiter: true 導致,將它改成 false 即可

完成 crul 測試後, 回頭調整 dify 設定

由於 dify 是在 docker 內, localhost 為 dify 本身, 不是 linux 主機 (docker 基礎課程), 所以安裝好 searxng 後, 服務路徑需填入 http://host.docker.internal:8080

好了 !! 忙了半天都跑不起來, 根本沒辦法在同一台 linux 跑, 生氣!!!!!!

更新

https://docs.dify.ai/getting-started/install-self-hosted/docker-compose#upgrade-dify

cd dify/docker
docker compose down
git pull origin main
docker compose pull
docker compose up -d

Nginx

由於申請兩台機器又要裝 docker 有點浪費,決定只申請一台, 並透過 nginx 將不同 domain 指向不同的 docker 服務

以下為概略的網路架構圖

flowchart TD

A --80/443--> nginx
B --80/443--> nginx
E(... All<br/> Internet<br/> Users) --80/443--> nginx

subgraph linux server
  nginx --dify.example.com--> Dify(Dify Nginx Ingress @ local 8081 port)
  nginx -- openwebui.example.com--> OpenWebUI(Open WebUI @ loca 8080 port)
  subgraph docker 
    Dify
    OpenWebUI
  end
end

為了將 dify 的修改減低到最低,這邊不處理 dify docker-compose 內的 nginx,直接將 .env 內的 nginx port 調整為 8081

EXPOSE_NGINX_PORT=127.0.0.1:8081
EXPOSE_NGINX_SSL_PORT=127.0.0.1:44381 # 一同設定 SSL_PORT 單純是不讓 dify 跟 linux 的 nginx 搶 443 port

dify 的 nginx 就不配置 ssl 了,不需要處理兩次 ssl termination

開始安裝 nginx

https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/#installing-prebuilt-ubuntu-packages

sudo apt-get update
sudo apt-get install nginx
sudo nginx -v

完成後可用 curl 指令來確認 nginx 有沒有正確綁 80 port

curl 127.0.0.1

接著開始進行 nginx 配置,配置檔放在 /etc/nginx/conf.d/ 下建立一個 ai.conf 檔案

以下為 ai.conf 內容:

# redirect 80 ->443
server {
    listen 80;
    server_name unit-dify.example.com unit-openwebui.example.com;
    return 301 https://$host$request_uri;
}

# unit-dify.example.com
server {
    listen 443 ssl;
    server_name unit-dify.example.com;

    ssl_certificate     /etc/ssl/certs/example.com.crt;
    ssl_certificate_key /etc/ssl/private/example.com.key;

    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    location / {
        proxy_pass http://localhost:8081;  # map to dify nginx expose port 8081
        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;
    }
}

# unit-openwebui.example.com
server {
    listen 443 ssl;
    server_name unit-openwebui.example.com;

    ssl_certificate     /etc/ssl/certs/example.com.crt;
    ssl_certificate_key /etc/ssl/private/example.com.key;

    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    location / {
        proxy_pass http://localhost:8080; # map to open-webui expose port 8080
        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;
    }
}

可以看到上面的設定檔有指定 SSL 憑證

公司給的設定檔憑證分成 key 跟兩個 crt,其中 key 為私鑰,兩個 crt 分別為中繼及終端憑證

需要將兩個 crt 給整合成一個完整憑證鍊的憑證檔

cat domain.crt intermediate.crt > example.com.crt

完成合併後,再將私鑰以及憑證放到對應的位置,通常私鑰放置在 /etc/ssl/private/ 中,憑證放在 /etc/ssl/certs/

sudo cp ~/path2/example.com.key /etc/ssl/private/example.com.key
sudo cp ~/path2/example.com.crt /etc/ssl/certs/example.com.crt

完成設定後,可使用 sudo nginx -t 來驗證設定檔是否正確,如果正確會看到以下訊息:

 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
 nginx: configuration file /etc/nginx/nginx.conf test is successful

驗證無誤可重新啟動 nginx 服務 sudo systemctl restart nginx

重啟後可使用 curl 來測試是否能夠正確取得 openwebui 及 dify 的頁面。

curl unit-openwebui.example.com
curl unit-dify.example.com