Skip to content

Latest commit

 

History

History
124 lines (86 loc) · 2.3 KB

File metadata and controls

124 lines (86 loc) · 2.3 KB

Deployment Guide

Host Nginx + Docker Frontend + Docker Backend

This document describes how to deploy the application on a server that already hosts other websites using host-level Nginx, while keeping the backend fully containerized.

Key rule:
Only the host Nginx listens on ports 80 and 443.
Docker containers run behind it on internal ports and networks.


Architecture Overview

Internet
   |
   v
Host Nginx (80 / 443, SSL termination)
   |
   v
Backend Container (Django/API :8000, internal only)

Prerequisites

  • Linux server with Nginx, Docker, Certbot
  • DNS records:
    • api.yourdomain.com

1. Install Docker and Create Docker Network

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

docker network create app-network

sudo usermod -aG docker $USER

echo "YOUR_ACCESS_TOKEN" | docker login -u YOUR_USERNAME --password-stdin

2. Create and run redis container

docker run -d --name redis --network app-network -p 6379:6379 --restart unless-stopped redis:alpine

3. Create migration folders on the root server

cd /home/main
sudo mkdir -p migrations/accounts migrations/profiles migrations/api
sudo touch migrations/__init__.py
sudo touch migrations/api/__init__.py
sudo touch migrations/accounts/__init__.py
sudo touch migrations/profiles/__init__.py

3. Host Nginx Config – API

Create /etc/nginx/sites-available/api.yourdomain.com

server {
    listen 80;
    server_name api.yourdomain.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name api.yourdomain.com;

    ssl_certificate etc/letsencrypt/live/api.yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/api.yourdomain.com/privkey.pem;

    location /static/ {
        alias /home/main/static/;
    }

    location /media/ {
        alias /home/main/media/;
    }

    location / {
        proxy_pass http://127.0.0.1:7080;
    }
}

7. SSL Certificates

sudo certbot --nginx -d api.yourdomain.com

8. Reload Nginx

sudo nginx -t
sudo systemctl reload nginx

Summary

  • Host Nginx owns ports 80/443
  • Frontend container serves UI and proxies API
  • Backend container is internal only
  • Other host sites remain unaffected