Skip to content

Deployment Overview of Django on Server

Prerequisites and Basic Requirements

The deployment requires a Linux server running either Debian or Ubuntu. The following system-level prerequisites must be met before installation:

  • Operating System: Debian or Ubuntu

  • Privileges: Root access or sudo privileges are required for package installation and service management

  • Domain: The server must be configured to resolve the hostkey.in domain

  • Ports:

  • Port 8000 for the internal Django application

  • Port 443 for external HTTPS access via the proxy

FQDN of the Final Panel

The application is accessible via the following Fully Qualified Domain Name (FQDN) format:

django<Server ID>.hostkey.in:443

The external path for the application is /django_client.

File and Directory Structure

The application files, configuration, and data are organized in the following locations:

  • Project Root: /root/django_client/project

  • Virtual Environment: /root/django_client/venv (Debian only)

  • Static Files: /root/django_client/project/static

  • Systemd Service File: /etc/systemd/system/django.service

  • Nginx Configuration Directory: /data/nginx/user_conf.d

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

  • Nginx Environment File: /data/nginx/nginx-certbot.env

  • SSL Certificates: Stored in the nginx_secrets volume mounted at /etc/letsencrypt

Application Installation Process

The Django application is installed using the following steps:

  1. Package Installation:

    • Required packages are installed via apt: python3, python3-pip, libpq-dev, python3-venv (Debian), or python3-pexpect (Ubuntu).
  2. Virtual Environment Setup (Debian):

    • A Python virtual environment is created at /root/django_client/venv.

    • Django is installed within this environment using pip.

  3. Project Initialization:

    • The Django project named django_client is created in /root/django_client/project.

    • The settings.py file is configured with:

      • BASE_DIR and STATIC_ROOT paths.

      • ALLOWED_HOSTS set to ['*'].

      • CSRF_TRUSTED_ORIGINS set to ['https://django<Server ID>.hostkey.in'].

  4. Database and Static Files:

    • Database migrations are applied using manage.py migrate.

    • Static files are collected using manage.py collectstatic.

  5. Superuser Creation:

    • A default superuser is created with the username root, email [email protected], and a password defined by the system configuration.

Docker Containers and Their Deployment

A Docker container is used to run the Nginx reverse proxy and manage SSL certificates via Certbot.

  • Image: jonasal/nginx-certbot:latest

  • Service Name: nginx

  • Restart Policy: unless-stopped

  • Network Mode: host

  • Volumes:

    • nginx_secrets (external) mounted at /etc/letsencrypt

    • /data/nginx/user_conf.d mounted at /etc/nginx/user_conf.d

  • Environment:

    • CERTBOT_EMAIL: [email protected]

    • Environment variables are loaded from /data/nginx/nginx-certbot.env

The container is managed using the docker compose file located at /root/nginx/compose.yml.

Proxy Servers

Nginx acts as the reverse proxy for the Django application, handling SSL termination and routing.

  • Configuration Location: /data/nginx/user_conf.d/django<Server ID>.hostkey.in.conf

  • Routing:

    • The Nginx configuration routes requests to the Django application running on port 8000.

    • The location block is configured to handle the root path / for the application context.

  • SSL/TLS:

    • Managed automatically by the nginx-certbot container.

    • Certificates are stored in the nginx_secrets volume.

Access Rights and Security

  • System User: The Django service runs as the root user.

  • Firewall: Ensure ports 8000 (internal) and 443 (external) are open.

  • CSRF Protection: The CSRF_TRUSTED_ORIGINS setting in settings.py restricts trusted origins to the specific hostkey.in domain.

  • Allowed Hosts: The ALLOWED_HOSTS setting is configured to accept requests from any host (['*']).

Permission Settings

  • Project Directory: /root/django_client/project is owned by root.

  • Nginx Directory: /root/nginx is owned by root with permissions 0644.

  • Compose File: /root/nginx/compose.yml is owned by root with permissions 0644.

  • Service File: /etc/systemd/system/django.service is owned by root.

Location of Configuration Files and Data

File/Directory Path Description
Django Project /root/django_client/project Main application code and settings
Virtual Environment /root/django_client/venv Python dependencies (Debian)
Static Files /root/django_client/project/static Collected static assets
Systemd Service /etc/systemd/system/django.service Service definition for Django
Nginx Config /data/nginx/user_conf.d/django<Server ID>.hostkey.in.conf Nginx site configuration
Docker Compose /root/nginx/compose.yml Docker service definition
Nginx Env /data/nginx/nginx-certbot.env Environment variables for Nginx

Available Ports for Connection

Port Protocol Description
8000 TCP Internal Django application server
443 TCP External HTTPS access via Nginx

Starting, Stopping, and Updating

The Django application is managed as a systemd service. Use the following commands to control the service:

  • Start the service:

    systemctl start django.service
    

  • Stop the service:

    systemctl stop django.service
    

  • Restart the service:

    systemctl restart django.service
    

  • Enable service on boot:

    systemctl enable django.service
    

To update the Nginx proxy configuration or restart the container:

cd /root/nginx
docker compose up -d
question_mark
Is there anything I can help you with?
question_mark
AI Assistant ×