Django is a high-level Python web framework that encourages rapid development and clean design. Gunicorn is a Python WSGI HTTP server used to run Django in production. Nginx acts as a reverse proxy, serving static files efficiently. This guide deploys a Django application on Ubuntu 26.04 LTS.
Tested and valid on:
- Ubuntu 26.04 LTS
Prerequisites
- Ubuntu 26.04 LTS with Python 3.14, Nginx, and MySQL or PostgreSQL installed
- A user with sudo privileges
Step 1 – Create the Project Directory and Virtual Environment
sudo mkdir -p /var/www/mydjango
sudo chown $USER:$USER /var/www/mydjango
cd /var/www/mydjango
python3 -m venv .venv
source .venv/bin/activate
Step 2 – Install Django and Gunicorn
pip install django gunicorn psycopg2-binary # or mysqlclient for MySQL
django-admin startproject mysite .
python manage.py migrate
python manage.py createsuperuser
Step 3 – Configure Django for Production
Edit mysite/settings.py:
DEBUG = False
ALLOWED_HOSTS = ['example.com', 'www.example.com']
STATIC_ROOT = '/var/www/mydjango/static/'
STATIC_URL = '/static/'
python manage.py collectstatic --noinput
Step 4 – Test Gunicorn
gunicorn --workers 3 --bind 0.0.0.0:8000 mysite.wsgi:application
Step 5 – Create a systemd Service for Gunicorn
sudo nano /etc/systemd/system/gunicorn.service
Add:
[Unit]
Description=Gunicorn daemon for Django
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/mydjango
ExecStart=/var/www/mydjango/.venv/bin/gunicorn
--workers 3
--bind unix:/run/gunicorn.sock
mysite.wsgi:application
[Install]
WantedBy=multi-user.target
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
Step 6 – Configure Nginx
sudo nano /etc/nginx/sites-available/mydjango
Add:
server {
listen 80;
server_name example.com www.example.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ { root /var/www/mydjango; }
location / {
proxy_pass http://unix:/run/gunicorn.sock;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
sudo ln -s /etc/nginx/sites-available/mydjango /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
Step 7 – Add SSL
sudo certbot --nginx -d example.com -d www.example.com
Conclusion
Your Django application is deployed on Ubuntu 26.04 LTS with Gunicorn and Nginx. Monitor Gunicorn with sudo journalctl -u gunicorn and configure Django’s caching and database connection pooling for production scale.