Deployment Overview of Django on Server¶
Prerequisites and Basic Requirements¶
The deployment requires a Linux server running either Debian or Ubuntu. The following system-level prerequisites must be met before installation:
-
Operating System: Debian or Ubuntu
-
Privileges: Root access or
sudoprivileges are required for package installation and service management -
Domain: The server must be configured to resolve the
hostkey.indomain -
Ports:
-
Port
8000for the internal Django application -
Port
443for external HTTPS access via the proxy
FQDN of the Final Panel¶
The application is accessible via the following Fully Qualified Domain Name (FQDN) format:
django<Server ID>.hostkey.in:443
The external path for the application is /django_client.
File and Directory Structure¶
The application files, configuration, and data are organized in the following locations:
-
Project Root:
/root/django_client/project -
Virtual Environment:
/root/django_client/venv(Debian only) -
Static Files:
/root/django_client/project/static -
Systemd Service File:
/etc/systemd/system/django.service -
Nginx Configuration Directory:
/data/nginx/user_conf.d -
Nginx Compose File:
/root/nginx/compose.yml -
Nginx Environment File:
/data/nginx/nginx-certbot.env -
SSL Certificates: Stored in the
nginx_secretsvolume mounted at/etc/letsencrypt
Application Installation Process¶
The Django application is installed using the following steps:
-
Package Installation:
- Required packages are installed via
apt:python3,python3-pip,libpq-dev,python3-venv(Debian), orpython3-pexpect(Ubuntu).
- Required packages are installed via
-
Virtual Environment Setup (Debian):
-
A Python virtual environment is created at
/root/django_client/venv. -
Django is installed within this environment using
pip.
-
-
Project Initialization:
-
The Django project named
django_clientis created in/root/django_client/project. -
The
settings.pyfile is configured with:-
BASE_DIRandSTATIC_ROOTpaths. -
ALLOWED_HOSTSset to['*']. -
CSRF_TRUSTED_ORIGINSset to['https://django<Server ID>.hostkey.in'].
-
-
-
Database and Static Files:
-
Database migrations are applied using
manage.py migrate. -
Static files are collected using
manage.py collectstatic.
-
-
Superuser Creation:
- A default superuser is created with the username
root, email[email protected], and a password defined by the system configuration.
- A default superuser is created with the username
Docker Containers and Their Deployment¶
A Docker container is used to run the Nginx reverse proxy and manage SSL certificates via Certbot.
-
Image:
jonasal/nginx-certbot:latest -
Service Name:
nginx -
Restart Policy:
unless-stopped -
Network Mode:
host -
Volumes:
-
nginx_secrets(external) mounted at/etc/letsencrypt -
/data/nginx/user_conf.dmounted at/etc/nginx/user_conf.d
-
-
Environment:
-
CERTBOT_EMAIL:[email protected] -
Environment variables are loaded from
/data/nginx/nginx-certbot.env
-
The container is managed using the docker compose file located at /root/nginx/compose.yml.
Proxy Servers¶
Nginx acts as the reverse proxy for the Django application, handling SSL termination and routing.
-
Configuration Location:
/data/nginx/user_conf.d/django<Server ID>.hostkey.in.conf -
Routing:
-
The Nginx configuration routes requests to the Django application running on port
8000. -
The location block is configured to handle the root path
/for the application context.
-
-
SSL/TLS:
-
Managed automatically by the
nginx-certbotcontainer. -
Certificates are stored in the
nginx_secretsvolume.
-
Access Rights and Security¶
-
System User: The Django service runs as the
rootuser. -
Firewall: Ensure ports
8000(internal) and443(external) are open. -
CSRF Protection: The
CSRF_TRUSTED_ORIGINSsetting insettings.pyrestricts trusted origins to the specifichostkey.indomain. -
Allowed Hosts: The
ALLOWED_HOSTSsetting is configured to accept requests from any host (['*']).
Permission Settings¶
-
Project Directory:
/root/django_client/projectis owned byroot. -
Nginx Directory:
/root/nginxis owned byrootwith permissions0644. -
Compose File:
/root/nginx/compose.ymlis owned byrootwith permissions0644. -
Service File:
/etc/systemd/system/django.serviceis owned byroot.
Location of Configuration Files and Data¶
| File/Directory | Path | Description |
|---|---|---|
| Django Project | /root/django_client/project | Main application code and settings |
| Virtual Environment | /root/django_client/venv | Python dependencies (Debian) |
| Static Files | /root/django_client/project/static | Collected static assets |
| Systemd Service | /etc/systemd/system/django.service | Service definition for Django |
| Nginx Config | /data/nginx/user_conf.d/django<Server ID>.hostkey.in.conf | Nginx site configuration |
| Docker Compose | /root/nginx/compose.yml | Docker service definition |
| Nginx Env | /data/nginx/nginx-certbot.env | Environment variables for Nginx |
Available Ports for Connection¶
| Port | Protocol | Description |
|---|---|---|
8000 | TCP | Internal Django application server |
443 | TCP | External HTTPS access via Nginx |
Starting, Stopping, and Updating¶
The Django application is managed as a systemd service. Use the following commands to control the service:
-
Start the service:
-
Stop the service:
-
Restart the service:
-
Enable service on boot:
To update the Nginx proxy configuration or restart the container: