# PolyHermes Deployment Guide > πŸ“– **δΈ­ζ–‡η‰ˆζœ¬**: [ιƒ¨η½²ζ–‡ζ‘£οΌˆδΈ­ζ–‡οΌ‰](../zh/DEPLOYMENT.md) This document describes how to deploy the PolyHermes project, including different deployment methods for backend and frontend. ## Table of Contents - [All-in-One Deployment (Recommended)](#all-in-one-deployment-recommended) - [Using Docker Hub Images](#using-docker-hub-images-recommended-for-production) - [Using External Nginx Reverse Proxy](#using-external-nginx-reverse-proxy-recommended-for-production) - [Backend Deployment](#backend-deployment) - [Java Direct Deployment](#java-direct-deployment) - [Docker Deployment](#docker-deployment) - [Frontend Deployment](#frontend-deployment) - [Environment Configuration](#environment-configuration) - [FAQ](#faq) ## All-in-One Deployment (Recommended) Deploy both frontend and backend together in a single Docker container, using Nginx to serve frontend static files and proxy backend API. ### Prerequisites - Docker 20.10+ - Docker Compose 2.0+ ### Deployment Steps 1. **Using Docker Hub Images (Recommended, Production First Choice)** Use officially built Docker images, no local build required, fast deployment. **Method 1: Standalone Deployment (No code clone required, Recommended for Production)** Suitable for production environments, no need to download project code, only configuration files needed for deployment. ```bash # 1. Create deployment directory mkdir polyhermes && cd polyhermes # 2. Download production environment configuration files # Download docker-compose.prod.yml and docker-compose.prod.env.example from GitHub curl -O https://raw.githubusercontent.com/WrBug/PolyHermes/main/docker-compose.prod.yml curl -O https://raw.githubusercontent.com/WrBug/PolyHermes/main/docker-compose.prod.env.example # 3. Create configuration file cp docker-compose.prod.env.example .env # 4. Edit .env file, modify the following required configurations: # - DB_PASSWORD: Database password (recommended to use strong password) # - JWT_SECRET: JWT secret key (generate using openssl rand -hex 64) # - ADMIN_RESET_PASSWORD_KEY: Admin password reset key (generate using openssl rand -hex 32) # # Example of generating random keys: # openssl rand -hex 64 # For JWT_SECRET # openssl rand -hex 32 # For ADMIN_RESET_PASSWORD_KEY # 5. Start services docker-compose -f docker-compose.prod.yml up -d # 6. View logs docker-compose -f docker-compose.prod.yml logs -f # 7. Stop services docker-compose -f docker-compose.prod.yml down ``` **Method 2: Using Deployment Script (Requires code clone)** ```bash # If you have already cloned the code ./deploy.sh --use-docker-hub ``` **Method 3: Modify Existing docker-compose.yml** ```bash # 1. Modify docker-compose.yml # Uncomment: image: wrbug/polyhermes:latest # Comment out build section # 2. Create .env file (see environment configuration below) # 3. Start services docker-compose up -d ``` **Advantages**: - βœ… No local build required, fast deployment - βœ… No code clone required, only configuration files needed for deployment - βœ… Uses officially built images with correct version numbers - βœ… Supports multiple architectures (amd64, arm64), automatically selects matching architecture - βœ… Recommended for production environments **Pull Specific Version**: ```bash # Modify image tag in docker-compose.prod.yml # image: wrbug/polyhermes:v1.0.0 # Or use environment variable export IMAGE_TAG=v1.0.0 # In docker-compose.prod.yml use: image: wrbug/polyhermes:${IMAGE_TAG:-latest} ``` **Update Docker Version**: When a new version is released, you can update using the following steps: ```bash # 1. Stop currently running containers docker-compose -f docker-compose.prod.yml down # 2. Pull the latest version image (or specific version) # Update to latest version docker pull wrbug/polyhermes:latest # Or update to specific version (e.g., v1.0.1) docker pull wrbug/polyhermes:v1.0.1 # 3. If using a specific version, modify the image tag in docker-compose.prod.yml # Edit docker-compose.prod.yml, change image to: # image: wrbug/polyhermes:v1.0.1 # 4. Restart services docker-compose -f docker-compose.prod.yml up -d # 5. Check logs to confirm services started normally docker-compose -f docker-compose.prod.yml logs -f ``` **Notes**: - ⚠️ It is recommended to backup the database before updating (if using MySQL in Docker Compose) - ⚠️ Service will be briefly interrupted during update, recommend updating during off-peak hours - βœ… Using `docker-compose pull` can automatically pull the latest image and update (if using `latest` tag) - βœ… View available versions: Visit [Docker Hub](https://hub.docker.com/r/wrbug/polyhermes/tags) or [GitHub Releases](https://github.com/WrBug/PolyHermes/releases) 2. **Local Build Deployment (Development Environment)** Suitable for development environments or scenarios requiring custom builds. ```bash # Use deployment script ./deploy.sh ``` The script will automatically: - Check Docker environment - Create `.env` configuration file (if it doesn't exist) - Build Docker image (including frontend and backend) - Start services (application + MySQL) **Note**: Locally built version numbers will display as `dev`. 3. **Manual Deployment** ```bash # Create .env file cat > .env < .env < .env.production < ServerName your-domain.com DocumentRoot /path/to/frontend/dist # API proxy ProxyPass /api http://localhost:8000/api ProxyPassReverse /api http://localhost:8000/api # WebSocket proxy ProxyPass /ws ws://localhost:8000/ws ProxyPassReverse /ws ws://localhost:8000/ws # Frontend routing (SPA) Options Indexes FollowSymLinks AllowOverride All Require all granted RewriteEngine On RewriteBase / RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L] ``` #### Method 3: Using serve (Development/Testing) ```bash # Install serve npm install -g serve # Start service serve -s dist -l 3000 ``` ## Environment Configuration ### Backend Environment Variables | Variable Name | Description | Default Value | Required | |---------------|-------------|---------------|----------| | `SPRING_PROFILES_ACTIVE` | Spring Profile | `dev` | No | | `DB_URL` | Database connection URL | - | Yes (Production) | | `DB_USERNAME` | Database username | `root` | Yes (Production) | | `DB_PASSWORD` | Database password | - | Yes (Production) | | `SERVER_PORT` | Server port | `8000` | No | | `JWT_SECRET` | JWT secret key | - | Yes (Production) | | `ADMIN_RESET_PASSWORD_KEY` | Admin password reset key | - | Yes (Production) | ### Frontend Environment Variables | Variable Name | Description | Default Value | |---------------|-------------|---------------| | `VITE_API_URL` | Backend API address | `http://127.0.0.1:8000` | | `VITE_WS_URL` | WebSocket address | `ws://127.0.0.1:8000` | ### Configuration File Description #### Backend Configuration Files - `application.properties` - Base configuration (shared by all environments) - `application-dev.properties` - Development environment configuration - `application-prod.properties` - Production environment configuration Switch environments via `--spring.profiles.active=prod` or environment variable `SPRING_PROFILES_ACTIVE=prod`. #### Frontend Environment Variables Vite uses `.env.production` file to inject environment variables during build. The build script will automatically create this file. ## FAQ ### 1. Database Connection Failed **Problem**: Backend cannot connect to database **Solution**: - Check if database service is running - Check if database connection URL, username, password are correct - Check if firewall allows connection - For Docker deployment, ensure using correct database address (`mysql` instead of `localhost`) ### 2. Frontend Cannot Connect to Backend **Problem**: Frontend requests to backend API fail **Solution**: - Check if backend service is running - Check if `VITE_API_URL` configuration is correct - Check CORS configuration (if cross-origin) - Check network connection and firewall ### 3. WebSocket Connection Failed **Problem**: WebSocket cannot establish connection **Solution**: - Check if `VITE_WS_URL` configuration is correct - Check WebSocket proxy configuration (Nginx/Apache) - Check if firewall allows WebSocket connection - Check if backend WebSocket service is normal ### 4. Docker Container Cannot Access Database **Problem**: Backend in Docker container cannot connect to host database **Solution**: - Use `host.docker.internal` as database address (Mac/Windows) - Use Docker network connection (recommended to use docker-compose) - Check if database allows remote connection ### 5. Build Failed **Problem**: Frontend or backend build fails **Solution**: - Check Node.js version (requires 18+) - Check Java version (requires 17+) - Clean cache and rebuild: ```bash # Frontend rm -rf node_modules dist npm install npm run build # Backend ./gradlew clean build ``` ## Production Environment Checklist - [ ] Modify all default passwords and keys (JWT_SECRET, ADMIN_RESET_PASSWORD_KEY, database password) - [ ] Configure correct database connection (use SSL) - [ ] Set correct Spring Profile (`prod`) - [ ] Configure correct backend API address (frontend) - [ ] Configure reverse proxy (Nginx/Apache) - [ ] Configure HTTPS (recommended for production) - [ ] Configure firewall rules - [ ] Set up log rotation - [ ] Configure monitoring and alerts - [ ] Regular database backups ## Performance Optimization Recommendations ### Backend - Adjust JVM parameters (heap memory, GC strategy) - Configure database connection pool size - Enable HTTP compression - Configure caching strategy ### Frontend - Enable Gzip compression (Nginx) - Configure static resource caching - Use CDN acceleration - Enable HTTP/2 ## Security Recommendations - Use HTTPS (required for production) - Configure CORS whitelist - Regularly update dependencies - Use strong passwords and keys - Limit database access permissions - Configure firewall rules - Regular data backups - Monitor abnormal access ## Technical Support If you have any questions, please submit an Issue to [GitHub](https://github.com/WrBug/PolyHermes) or contact [Twitter](https://x.com/polyhermes).