Skip to content

Deployment Overview of Django on Server

Prerequisites and Basic Requirements

The following requirements must be met on the target server before or during deployment:

  • Operating System: Debian or Ubuntu (Linux distributions supporting APT package management).

  • Privileges: Root access or sudo privileges are required to install packages, create systemd services, and manage Docker containers.

  • Network Connectivity: The server must have internet access to download packages and Docker images.

  • Ports: Port 8000 is used for the internal Django application. Port 443 is configured for external HTTPS access.

FQDN of the Final Panel

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

  • Format: <prefix><Server ID>.hostkey.in

  • Specific FQDN: django<Server ID>.hostkey.in

  • Port: 443 (HTTPS)

  • External Path: /django_client

Note: Replace <Server ID> with the actual unique identifier assigned to your server instance.

File and Directory Structure

The application files and configurations are organized in the following locations:

  • Django Project Root: /root/django_client/project

  • Virtual Environment: /root/django_client/venv (Debian specific) or global Python installation (Ubuntu specific).

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

  • Nginx and Certbot Configuration:

  • Nginx Docker Compose file: /root/nginx/compose.yml

  • Custom Nginx site configuration: /data/nginx/user_conf.d/django<Server ID>.hostkey.in.conf

  • Environment variables for Nginx: /data/nginx/nginx-certbot.env

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

Application Installation Process

The Django application is installed and initialized using the django_client project name. The process involves:

  1. Installing system dependencies including python3, python3-pip, libpq-dev, and python3-venv (Debian) or python3-pexpect (Ubuntu).

  2. Creating a Python virtual environment at /root/django_client/venv (Debian only) or installing Django globally.

  3. Initializing the Django project structure at /root/django_client/project.

  4. Configuring settings.py to include:

  5. BASE_DIR set to the parent of the settings file.

  6. STATIC_ROOT set to /root/django_client/project/static/.

  7. ALLOWED_HOSTS set to ['*'].

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

  9. Running database migrations using manage.py migrate --noinput.

  10. Collecting static files using manage.py collectstatic --noinput.

  11. Creating a superuser account with the username root and a predefined password.

Docker Containers and Their Deployment

A Docker container is deployed to manage the Nginx web server and SSL certificates via Certbot.

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

  • Service Name: nginx

  • Docker Image: jonasal/nginx-certbot:latest

  • Restart Policy: unless-stopped

  • Network Mode: host

  • Environment Variables:

  • CERTBOT_EMAIL: [email protected]

  • Additional configuration is loaded from /data/nginx/nginx-certbot.env.

  • Volumes:

  • nginx_secrets: Mapped to /etc/letsencrypt (external volume).

  • /data/nginx/user_conf.d: Mapped to /etc/nginx/user_conf.d.

The container is started using the command docker compose up -d executed from the /root/nginx directory.

Proxy Servers

The application is served through an Nginx container configured with Certbot for automatic SSL certificate management.

  • SSL/TLS: Enabled via the jonasal/nginx-certbot image.

  • Domain Configuration: The Nginx site configuration for django<Server ID>.hostkey.in is located in /data/nginx/user_conf.d.

  • Routing: The proxy is configured to handle requests at the root path / for the specified domain, forwarding them to the internal Django application.

Starting, Stopping, and Updating

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

Service Management Commands:

  • 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

  • Reload systemd configuration: systemctl daemon-reload

Docker Service Management:

  • Start/Update Nginx container: docker compose up -d (execute in /root/nginx)

  • Stop Nginx container: docker compose down (execute in /root/nginx)

Location of Configuration Files and Data

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

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

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

  • Custom Nginx Config: /data/nginx/user_conf.d/django<Server ID>.hostkey.in.conf

  • SSL Certificates: Managed within the nginx_secrets Docker volume mounted at /etc/letsencrypt.

Available Ports for Connection

  • Port 8000: Internal Django development server (bound to 0.0.0.0).

  • Port 443: External HTTPS access via Nginx proxy.

Access Rights and Security

  • The Django application runs under the root user via the systemd service.

  • The Nginx container runs with network_mode: host, allowing direct access to host ports.

  • Firewall rules must permit inbound traffic on port 443 for external access.

  • Database credentials and SSL certificates are managed within the Docker environment and specific configuration files.

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