Skip to content

Deployment Overview of Django on Server

Prerequisites and Basic Requirements

To ensure a successful deployment, the server must meet the following requirements:

  • Operating System: Debian or Ubuntu.

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

  • Python: Python 3 must be installed.

  • Dependencies: python3-pip, libpq-dev, and python3-venv (for Debian) or python3-pexpect (for Ubuntu).

  • Docker: Docker engine is required for the Nginx reverse proxy and SSL management.

FQDN of the final panel on the hostkey.in domain

The application is accessible via a specific subdomain template based on the server ID.

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

File and Directory Structure

The application uses several directories for project files, virtual environments, and configuration management:

  • /root/django_client/project: The main Django project directory containing the source code and management scripts.

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

  • /etc/systemd/system/django.service: The systemd service unit file for managing the application process.

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

  • /data/nginx/user_conf.d/: Directory containing custom Nginx virtual host configurations.

  • /data/nginx/letsencrypt/: Storage for SSL certificates and ACME challenge files.

Application installation process

The installation follows a multi-step process involving package management, environment setup, and service configuration:

  1. System Update: The system's package repository is updated and existing packages are upgraded via apt.

  2. Dependency Installation: Required Python and system libraries are installed using the package manager.

  3. Environment Setup:

  4. A virtual environment is created at /root/django_client/venv (on Debian) or a global installation is performed (on Ubuntu).

  5. Django is installed via pip.

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

  7. Configuration Setup:

  8. settings.py is modified to set the BASE_DIR, STATIC_ROOT, and ALLOWED_HOSTS.

  9. CSRF_TRUSTED_ORIGINS is configured to allow requests from the specific hostkey.in subdomain.

  10. Static Assets: The collectstatic command is executed to gather all static files into the designated directory.

  11. Database Management: Database migrations are applied using manage.py migrate.

  12. User Creation: A Django superuser is created with predefined credentials for administrative access.

  13. Service Configuration: A systemd service named django is configured to ensure the application starts automatically on boot and restarts upon failure.

Access Rights and Security

  • Firewall/Ports: The application runs internally on port 8000. External traffic is routed through Nginx on port 443 (HTTPS).

  • Service User: The Django service runs as the root user within its specific working directory.

  • SSL/TLS: SSL certificates are managed via a Dockerized Certbot container to ensure encrypted communication.

Databases

The application uses a database backend compatible with PostgreSQL (indicated by the presence of libpq-dev). Database schemas are initialized and updated through Django's migration system (manage.py migrate).

Docker Containers and Their Deployment

The deployment utilizes a Docker Compose setup for managing the web server and SSL certificates.

Nginx Container

  • Image: jonasal/nginx-certbot:latest

  • Ports: Uses network_mode: host to bind directly to host ports.

  • Volumes:

  • nginx_secrets:/etc/letsencrypt: Persistent storage for SSL certificates.

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

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

  • Environment Variables: CERTBOT_EMAIL is set to facilitate certificate renewal notifications.

  • Restart Policy: unless-stopped.

Application Update Instructions

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

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

  2. Activate the virtual environment (if applicable).

  3. Pull any new source code changes.

  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 User Configs: /data/nginx/user_conf.d/

  • SSL Certificates: Managed within the nginx_secrets Docker volume.

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

Available ports for connection

Port Protocol Description
443 TCP Secure HTTPS access (via Nginx)
8000 TCP Internal Django application service

Starting and Stopping the application

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

  • Start Application: systemctl start django.service

  • Stop Application: systemctl stop django.service

  • Restart Application: systemctl restart django.service

  • Check Status: systemctl status django.service

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