# Dify 自行架設 - Claude 3.5 建議 By 蓉爸 RungBa Created: 2025-01-26 Revised: 2025-01-26 --- :::info 繼使用 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](https://hackmd.io/_uploads/H1JxpaEukg.png) --- # 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: ```bash 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: ```bash sudo systemctl start postgresql sudo systemctl enable postgresql ``` 2. Create database and user: ```bash 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: ```bash sudo systemctl start redis-server sudo systemctl enable redis-server ``` ## Dify Installation 1. Clone the repository: ```bash git clone https://github.com/langgenius/dify.git cd dify ``` 2. Set up Python virtual environment: ```bash python3 -m venv venv source venv/bin/activate ``` 3. Install backend dependencies: ```bash cd api pip install -r requirements.txt ``` 4. Configure environment variables: ```bash 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 ``` 5. Install and build frontend: ```bash cd ../web npm install npm run build ``` ## Initialize Database From the api directory: ```bash flask db upgrade flask init ``` ## Running the Application 1. Start the backend server: ```bash cd api flask run --host 0.0.0.0 --port 5001 ``` 2. Start the frontend development server: ```bash 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