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 components and privileges are necessary:

  • Operating System: Debian or Ubuntu with APT package manager.
  • Privileges: Root access or sudo privileges to install packages and manage system services.
  • Python Environment: Python 3, python3-pip, libpq-dev, and python3-venv (for Debian) or python3-pexpect (for Ubuntu).
  • Ports: Port 8000 is used internally by the Django application. Ports 80 and 443 are required for the Nginx reverse proxy and SSL termination.
  • Domain: The application is configured to accept requests from https://{{ prefix }}{{ server_id }}.hostkey.in.

File and Directory Structure

The application and its supporting files are organized in the following locations:

  • Project Root: /root/django_client/project
  • Contains the main Django application code, manage.py, and configuration files.
  • Virtual Environment: /root/django_client/venv
  • Stores the isolated Python environment with Django and dependencies (Debian deployment).
  • Static Files: /root/django_client/project/static
  • Directory where collected static assets are stored.
  • Nginx Configuration: /root/nginx
  • Contains the compose.yml file for Docker deployment.
  • Nginx User Config: /data/nginx/user_conf.d
  • Stores custom Nginx configuration files, specifically {{ prefix }}{{ server_id }}.hostkey.in.conf.
  • SSL Certificates: /etc/letsencrypt
  • Mounted volume for Let's Encrypt certificates managed by the Nginx container.
  • Systemd Service: /etc/systemd/system/django.service
  • Defines the service unit for managing the Django application.

Application Installation Process

The Django application is installed and configured as follows:

  1. Package Installation: System packages including Python 3, pip, and development libraries are installed via APT.
  2. Environment Setup:
    • On Debian, a virtual environment is created at /root/django_client/venv.
    • Django is installed within this virtual environment using pip.
    • On Ubuntu, Django is installed globally using pip.
  3. Project Initialization: A new Django project named django_client is created in /root/django_client/project.
  4. Configuration:
    • settings.py is modified to set BASE_DIR and STATIC_ROOT.
    • ALLOWED_HOSTS is set to ['*'].
    • CSRF_TRUSTED_ORIGINS is configured to ['https://{{ prefix }}{{ server_id }}.hostkey.in'].
  5. Database Setup: Database migrations are applied using python manage.py migrate --noinput.
  6. Static Files: Static assets are collected into the static directory using python manage.py collectstatic --noinput.
  7. Superuser Creation: An administrative user is created with the username root, email [email protected], and a password defined by the system configuration.

Docker Containers and Their Deployment

The reverse proxy and SSL management are handled by Docker containers using Docker Compose.

  • Docker Installation: The Docker engine is installed on the server.
  • Compose File: A compose.yml file is generated at /root/nginx/compose.yml.
  • Service Definition:
  • Image: jonasal/nginx-certbot:latest
  • Restart Policy: unless-stopped
  • Environment:
    • CERTBOT_EMAIL is set to [email protected].
    • Environment variables are loaded from /data/nginx/nginx-certbot.env.
  • Network Mode: host
  • Volumes:
    • nginx_secrets (external) mounted to /etc/letsencrypt.
    • /data/nginx/user_conf.d mounted to /etc/nginx/user_conf.d.
  • Execution: The container stack is started using docker compose up -d from the /root/nginx directory.

Proxy Servers

Nginx acts as the reverse proxy and handles SSL termination for the Django application.

  • Configuration Location: Custom Nginx configuration is located at /data/nginx/user_conf.d/{{ prefix }}{{ server_id }}.hostkey.in.conf.
  • Routing: The configuration is modified to route all traffic (location /) to the backend Django application.
  • SSL/TLS: SSL certificates are managed automatically by the nginx-certbot container using Let's Encrypt.
  • Domain Mapping: The proxy is configured to serve the domain {{ prefix }}{{ server_id }}.hostkey.in.

Starting, Stopping, and Updating

The Django application is managed as a systemd service named django.

  • Start the Service:
    systemctl start django.service
    
  • Stop the Service:
    systemctl stop django.service
    
  • Restart the Service:
    systemctl restart django.service
    
  • Enable on Boot:
    systemctl enable django.service
    
  • Check Status:
    systemctl status django.service
    

The service is configured to restart automatically if it fails (Restart=always). After any configuration changes or updates to the Nginx proxy, the Django service may need to be restarted to apply changes.

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