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:
-
System Preparation: The system packages are updated and upgraded via
apt. Required dependencies includingpython3,python3-pip,libpq-dev, andpython3-venvare installed. -
Environment Setup: A Python virtual environment is created at
/root/django_client/venv/(on Debian) or the application is installed globally viapip(on Ubuntu). Thedjangoandpexpectpackages are installed within this environment. -
Project Initialization: The Django project structure is generated in
/root/django_client/project. -
Configuration:
-
settings.pyis modified to define theBASE_DIR,STATIC_ROOT, andALLOWED_HOSTS. -
CSRF_TRUSTED_ORIGINSis configured to allow requests from the application's specific domain. -
Database and Static Files:
-
Database migrations are applied using
manage.py migrate. -
Static files are collected into the static directory using
manage.py collectstatic. -
User Creation: A Django superuser is created with the username
rootto allow administrative access via the web interface. -
Service Configuration: A systemd service named
django.serviceis created and enabled to ensure the application starts automatically on boot.
Access Rights and Security¶
-
Firewall: Ensure port
443is open for HTTPS traffic. -
User Permissions: The Django service runs under the
rootuser 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:
-
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:
-
Navigate to the project directory:
cd /root/django_client/project. -
If using a virtual environment, activate it:
source ../venv/bin/activate. -
Pull any new code changes (if applicable).
-
Apply database migrations:
python3 manage.py migrate. -
Collect static files:
python3 manage.py collectstatic --noinput. -
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.