Skip to content

Deployment in the Production Environment

This document provides a step-by-step guide for deploying the project in a production environment.

In production, Nginx is used as the reverse proxy, and Gunicorn runs the backend application.

Step 1: Configure Environment Variables

Create a .env file in the project root directory. Production needs at least database, Django secret, Host/CSRF/CORS, SM2, Redis, and Celery settings.

For the complete variable reference, defaults, build-time mirror variables, and a full example .env, see Environment Variables.

text
POSTGRES_DB=myucspace
POSTGRES_USER=postgres
POSTGRES_PASSWORD=change_me_to_a_strong_password
POSTGRES_HOST=db
POSTGRES_PORT=5432

DJANGO_DEBUG=false
DJANGO_SECRET_KEY=change_me_to_a_long_random_secret
DJANGO_ALLOWED_HOSTS=example.com,localhost,127.0.0.1
CSRF_DOMAINS=https://example.com
CORS_ALLOWED_ORIGINS=https://example.com

SM2_PUBLIC_KEY=your_sm2_public_key
SM2_PRIVATE_KEY=your_sm2_private_key

REDIS_URL=redis://redis:6379/2
CELERY_BROKER_URL=redis://redis:6379/0
CELERY_RESULT_BACKEND=redis://redis:6379/1

WARNING

For security, use strong, unique values for all secrets and keys. Consider using a secrets manager or environment variable management tool in production.

TIP

The production docker-compose.prod.yml includes redis, celery-worker, and celery-beat services.

  • Redis is configured with AOF persistence (redis-server --appendonly yes) and stores data in /data/redis.
  • Celery Beat uses django-celery-beat with DatabaseScheduler, storing schedule data in MySQL. Scheduled tasks can be managed via Django Admin after running python manage.py migrate.

Step 2: Build and Run

bash
# Build and run
sudo docker compose -f docker-compose.prod.yml up --build -d

# Check service status
sudo docker compose -f docker-compose.prod.yml ps

# View logs (if any issues occur)
sudo docker compose -f docker-compose.prod.yml logs -f

Step 3: Initialize the Database and Create a User

bash
# Run database migrations
sudo docker compose -f docker-compose.prod.yml exec backend python manage.py migrate

# Create an administrator account
sudo docker compose -f docker-compose.prod.yml exec backend python manage.py createsuperuser

Refer to the HTTPS Deployment Guide for detailed instructions on setting up HTTPS with Nginx.

Production Access Endpoints

ServiceURL
Frontendhttp://localhost
Backend APIhttp://localhost/api/v1/
Admin Panelhttp://localhost/django-admin/

Domestic (China) Image Mirror Configuration

If image pulls fail, configure a Docker mirror registry using the following command:

bash
sudo -i
bash <(curl -sSL https://linuxmirrors.cn/docker.sh)

See more: Docker Installation & Registry Mirror Switcher.