Deployment Overview of Django on Server¶
Prerequisites and Basic Requirements¶
The deployment requires a Linux server running either Debian or Ubuntu. The following system components and privileges are necessary:
- Operating System: Debian or Ubuntu with APT package manager.
- Privileges: Root access or
sudoprivileges to install packages and manage system services. - Python Environment: Python 3,
python3-pip,libpq-dev, andpython3-venv(for Debian) orpython3-pexpect(for Ubuntu). - Ports: Port 8000 is used internally by the Django application. Ports 80 and 443 are required for the Nginx reverse proxy and SSL termination.
- Domain: The application is configured to accept requests from
https://{{ prefix }}{{ server_id }}.hostkey.in.
File and Directory Structure¶
The application and its supporting files are organized in the following locations:
- Project Root:
/root/django_client/project - Contains the main Django application code,
manage.py, and configuration files. - Virtual Environment:
/root/django_client/venv - Stores the isolated Python environment with Django and dependencies (Debian deployment).
- Static Files:
/root/django_client/project/static - Directory where collected static assets are stored.
- Nginx Configuration:
/root/nginx - Contains the
compose.ymlfile for Docker deployment. - Nginx User Config:
/data/nginx/user_conf.d - Stores custom Nginx configuration files, specifically
{{ prefix }}{{ server_id }}.hostkey.in.conf. - SSL Certificates:
/etc/letsencrypt - Mounted volume for Let's Encrypt certificates managed by the Nginx container.
- Systemd Service:
/etc/systemd/system/django.service - Defines the service unit for managing the Django application.
Application Installation Process¶
The Django application is installed and configured as follows:
- Package Installation: System packages including Python 3, pip, and development libraries are installed via APT.
- Environment Setup:
- On Debian, a virtual environment is created at
/root/django_client/venv. - Django is installed within this virtual environment using
pip. - On Ubuntu, Django is installed globally using
pip.
- On Debian, a virtual environment is created at
- Project Initialization: A new Django project named
django_clientis created in/root/django_client/project. - Configuration:
settings.pyis modified to setBASE_DIRandSTATIC_ROOT.ALLOWED_HOSTSis set to['*'].CSRF_TRUSTED_ORIGINSis configured to['https://{{ prefix }}{{ server_id }}.hostkey.in'].
- Database Setup: Database migrations are applied using
python manage.py migrate --noinput. - Static Files: Static assets are collected into the
staticdirectory usingpython manage.py collectstatic --noinput. - Superuser Creation: An administrative user is created with the username
root, email[email protected], and a password defined by the system configuration.
Docker Containers and Their Deployment¶
The reverse proxy and SSL management are handled by Docker containers using Docker Compose.
- Docker Installation: The Docker engine is installed on the server.
- Compose File: A
compose.ymlfile is generated at/root/nginx/compose.yml. - Service Definition:
- Image:
jonasal/nginx-certbot:latest - Restart Policy:
unless-stopped - Environment:
CERTBOT_EMAILis set to[email protected].- Environment variables are loaded from
/data/nginx/nginx-certbot.env.
- Network Mode:
host - Volumes:
nginx_secrets(external) mounted to/etc/letsencrypt./data/nginx/user_conf.dmounted to/etc/nginx/user_conf.d.
- Execution: The container stack is started using
docker compose up -dfrom the/root/nginxdirectory.
Proxy Servers¶
Nginx acts as the reverse proxy and handles SSL termination for the Django application.
- Configuration Location: Custom Nginx configuration is located at
/data/nginx/user_conf.d/{{ prefix }}{{ server_id }}.hostkey.in.conf. - Routing: The configuration is modified to route all traffic (
location /) to the backend Django application. - SSL/TLS: SSL certificates are managed automatically by the
nginx-certbotcontainer using Let's Encrypt. - Domain Mapping: The proxy is configured to serve the domain
{{ prefix }}{{ server_id }}.hostkey.in.
Starting, Stopping, and Updating¶
The Django application is managed as a systemd service named django.
- Start the Service:
- Stop the Service:
- Restart the Service:
- Enable on Boot:
- Check Status:
The service is configured to restart automatically if it fails (Restart=always). After any configuration changes or updates to the Nginx proxy, the Django service may need to be restarted to apply changes.