Skip to content

Deployment Overview of Django on Server

Prerequisites and Basic Requirements

To successfully deploy the application, ensure the following requirements are met:

  • Operating System: Ubuntu or Debian.

  • Privileges: Root or sudo access is required for package installation and service management.

  • Ports:

  • 8000: Internal application port (running via development server).

  • 443: External HTTPS port.

  • Dependencies: Python 3, Pip, and system libraries (libpq-dev).

FQDN of the final panel on the hostkey.in domain

The following table describes the domain template used for accessing the application:

Parameter Value
Prefix django
Domain hostkey.in
Full template django{Server_ID_from_Invapi}.hostkey.in

File and Directory Structure

The application utilizes the following directory structure for configuration, data, and logs:

  • /root/django_client/project: Main Django project files and management scripts.

  • /root/django_client/venv: Python virtual environment (on Debian systems).

  • /etc/systemd/system/django.service: Systemd service unit file for the application.

  • /data/nginx/user_conf.d/: Nginx configuration files.

  • /data/nginx/letsencrypt/: SSL certificate data and ACME challenge directory.

  • /root/nginx/compose.yml: Docker Compose configuration for the Nginx proxy.

Application installation process

The application is installed using a combination of system package management, Python virtual environments, and service configuration:

  1. System Preparation: The system packages are updated and upgraded via apt. Required dependencies including python3, python3-pip, libpq-dev, and python3-venv are installed.

  2. Environment Setup: A Python virtual environment is created at /root/django_client/venv/ (on Debian) or the application is installed globally via pip (on Ubuntu). The django and pexpect packages are installed within this environment.

  3. Project Initialization: The Django project structure is generated in /root/django_client/project.

  4. Configuration:

  5. settings.py is modified to define the BASE_DIR, STATIC_ROOT, and ALLOWED_HOSTS.

  6. CSRF_TRUSTED_ORIGINS is configured to allow requests from the application's specific domain.

  7. Database and Static Files:

  8. Database migrations are applied using manage.py migrate.

  9. Static files are collected into the static directory using manage.py collectstatic.

  10. User Creation: A Django superuser is created with the username root to allow administrative access via the web interface.

  11. Service Configuration: A systemd service named django.service is created and enabled to ensure the application starts automatically on boot.

Access Rights and Security

  • Firewall: Ensure port 443 is open for HTTPS traffic.

  • User Permissions: The Django service runs under the root user within its working directory.

  • SSL/TLS: SSL certificates are managed via a Dockerized Certbot instance to secure communications over HTTPS.

Databases

The application uses a database compatible with PostgreSQL (indicated by libpq-dev). Database migrations are applied during the installation process to set up the required schema.

Docker Containers and Their Deployment

The deployment utilizes one primary container for web proxying and SSL management:

Nginx Proxy Container

  • Image: jonasal/nginx-certbot:latest

  • Ports: Uses network_mode: host.

  • Volumes:

  • nginx_secrets:/etc/letsencrypt: Stores SSL certificates.

  • /data/nginx/user_conf.d:/etc/nginx/user_conf.d: Contains custom Nginx site configurations.

  • /data/nginx/letsencrypt:/var/www/letsencrypt: Used for ACME challenges.

  • Environment Variables:

  • [email protected]

  • Custom settings loaded from /data/nginx/nginx-certbot.env.

  • Restart Policy: unless-stopped

Application Update Instructions

To update the main Django application, perform the following steps:

  1. Navigate to the project directory: cd /root/django_client/project.

  2. If using a virtual environment, activate it: source ../venv/bin/activate.

  3. Pull any new code changes (if applicable).

  4. Apply database migrations: python3 manage.py migrate.

  5. Collect static files: python3 manage.py collectstatic --noinput.

  6. Restart the service to apply changes: systemctl restart django.service.

Location of configuration files and data

  • Django Settings: /root/django_client/project/django_client/settings.py

  • Nginx Site Configs: /data/nginx/user_conf.d/django{Server_ID_from_Invapi}.hostkey.in.conf

  • Docker Compose File: /root/nginx/compose.yml

Available ports for connection

  • 443: HTTPS (via Nginx proxy).

Starting and Stopping the application

The application is managed via systemd. Use the following commands:

  • Start Django: systemctl start django

  • Stop Django: systemctl stop django

  • Restart Django: systemctl restart django

  • Check Status: systemctl status django

Proxy Servers (Nginx, Traefik, SSL, custom domains)

The application is served through an Nginx proxy container (jonasal/nginx-certbot) that handles SSL termination. The proxy is configured to:

  • Handle ACME challenges for Let's Encrypt certificate renewal in /var/www/letsencrypt.

  • Route traffic from the root URL (/) to the Django application backend.

  • Manage SSL certificates stored in a Docker volume named nginx_secrets.

question_mark
Is there anything I can help you with?
question_mark
AI Assistant ×