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, andpython3-venv(for Debian) orpython3-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:
-
System Update: The system's package repository is updated and existing packages are upgraded via
apt. -
Dependency Installation: Required Python and system libraries are installed using the package manager.
-
Environment Setup:
-
A virtual environment is created at
/root/django_client/venv(on Debian) or a global installation is performed (on Ubuntu). -
Django is installed via
pip. -
Project Initialization: The Django project structure is generated in
/root/django_client/project. -
Configuration Setup:
-
settings.pyis modified to set theBASE_DIR,STATIC_ROOT, andALLOWED_HOSTS. -
CSRF_TRUSTED_ORIGINSis configured to allow requests from the specific hostkey.in subdomain. -
Static Assets: The
collectstaticcommand is executed to gather all static files into the designated directory. -
Database Management: Database migrations are applied using
manage.py migrate. -
User Creation: A Django superuser is created with predefined credentials for administrative access.
-
Service Configuration: A systemd service named
djangois 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 port443(HTTPS). -
Service User: The Django service runs as the
rootuser 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: hostto 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_EMAILis set to facilitate certificate renewal notifications. -
Restart Policy:
unless-stopped.
Application Update Instructions¶
To update the main Django application, perform the following steps:
-
Navigate to the project directory:
/root/django_client/project. -
Activate the virtual environment (if applicable).
-
Pull any new source code changes.
-
Apply database migrations:
-
Collect static files:
-
Restart the service to apply changes:
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_secretsDocker 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