Try   HackMD

Dify 自行架設 - Claude 3.5 建議

By 蓉爸 RungBa
Created: 2025-01-26
Revised: 2025-01-26


繼使用 LXD/LXC 完成 n8n 的自架後,接下來就再來試試 Dify 的自架!

一樣,我不想用 Docker 方式來架設,但花了一些時間,Google 到的都是使用 Docker 的安裝方式,突然想到我該來問問 Claude 的,於是就有了下方的成果,待我試試 Claude 3.5 給的 installation guide

不想用 Docker 方式架設的原因:
因 Docker 安裝後,整個 container image 會非常肥大,比一般不用Docker 方式建置的 image 大了 10 ~ 20 倍,而且也不利於備份,甚至發現備份時,無法成功匯出 container image,目前不確定是否是 LXD 系統本身還存在這個問題?!

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →


Dify Installation Guide for Ubuntu (Non-Docker)

Prerequisites

  1. Ubuntu 20.04 or newer
  2. Python 3.10 or newer
  3. Node.js 16+ and npm
  4. PostgreSQL 14+
  5. Redis

System Dependencies Installation

First, install required system packages:

sudo apt update
sudo apt install -y python3-pip python3-venv postgresql postgresql-contrib redis-server nodejs npm git

PostgreSQL Setup

  1. Start PostgreSQL service:
sudo systemctl start postgresql
sudo systemctl enable postgresql
  1. Create database and user:
sudo -u postgres psql
CREATE DATABASE dify;
CREATE USER dify WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE dify TO dify;
\q

Redis Setup

  1. Start Redis service:
sudo systemctl start redis-server
sudo systemctl enable redis-server

Dify Installation

  1. Clone the repository:
git clone https://github.com/langgenius/dify.git
cd dify
  1. Set up Python virtual environment:
python3 -m venv venv
source venv/bin/activate
  1. Install backend dependencies:
cd api
pip install -r requirements.txt
  1. Configure environment variables:
cp .env.example .env

Edit .env file with your database and Redis configurations:

# Database
DB_USERNAME=dify
DB_PASSWORD=your_password
DB_HOST=localhost
DB_PORT=5432
DB_DATABASE=dify

# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_USERNAME=
REDIS_PASSWORD=
REDIS_DB=0
  1. Install and build frontend:
cd ../web
npm install
npm run build

Initialize Database

From the api directory:

flask db upgrade
flask init

Running the Application

  1. Start the backend server:
cd api
flask run --host 0.0.0.0 --port 5001
  1. Start the frontend development server:
cd web
npm run dev

Access the Application

Access the web interface at: http://localhost:3000

Common Issues and Troubleshooting

  1. If you encounter PostgreSQL authentication issues:

    • Check pg_hba.conf configuration
    • Verify database user permissions
  2. If Redis connection fails:

    • Verify Redis is running: sudo systemctl status redis-server
    • Check Redis configuration in /etc/redis/redis.conf
  3. Python package conflicts:

    • Ensure you're using Python 3.10+
    • Try recreating the virtual environment
  4. Port conflicts:

    • Check if ports 5001 and 3000 are available
    • Use different ports by modifying the start commands

Security Considerations

  1. Change default passwords
  2. Configure proper firewall rules
  3. Set up SSL/TLS for production use
  4. Regularly update dependencies

Production Deployment

For production deployment, additional steps are recommended:

  1. Use a production-grade WSGI server (e.g., Gunicorn)
  2. Set up Nginx as a reverse proxy
  3. Configure proper security measures
  4. Set up SSL certificates
  5. Configure proper logging