Deploy Django on VPS: Complete Nginx + Gunicorn Guide
Django is one of the most popular Python web frameworks for building production-grade websites and APIs. However, deploying Django on a VPS correctly requires understanding several system components. This guide will walk you through deploying a Django application on an Ubuntu VPS using Gunicorn as the WSGI server and Nginx as the reverse proxy — from start to finish.
Why Gunicorn + Nginx?
Django includes a built-in development server (python manage.py runserver), but it is not suitable for production because:
- It handles only one request at a time (single-threaded)
- It has no security hardening
- It cannot efficiently serve static files
Gunicorn (Green Unicorn) is a WSGI server that handles Python applications efficiently with multiple worker processes. Nginx acts as a reverse proxy — accepting requests from the internet, forwarding them to Gunicorn, serving static files, and handling SSL termination.
System Requirements
- VPS running Ubuntu 22.04 LTS or Ubuntu 24.04 LTS
- Python 3.11 or newer
- At least 1 GB RAM (2 GB recommended)
- Root or sudo access on the server
- A domain name pointing to your VPS IP (optional but recommended)
Step 1: Update the System
Start by updating the package list and upgrading existing packages:
apt update && apt upgrade -y
Step 2: Install Python, venv, and Nginx
Install the required packages:
apt install python3 python3-venv python3-pip nginx -y
Verify the Python version:
python3 --version
Step 3: Create a System User for Django
It is best practice to run Django under a non-root system user for security:
adduser --system --group django
Step 4: Create the Project Directory
mkdir -p /var/www/myapp
chown django:django /var/www/myapp
Step 5: Set Up a Virtual Environment
Create and activate a virtualenv:
python3 -m venv /var/www/myapp/venv
source /var/www/myapp/venv/bin/activate
Step 6: Install Django and Gunicorn
pip install django gunicorn psycopg2-binary
If you're using MySQL instead of PostgreSQL, install:
pip install mysqlclient
Step 7: Create a Django Project
cd /var/www/myapp
django-admin startproject myapp .
chown -R django:django /var/www/myapp
Step 8: Configure Django Settings
Edit /var/www/myapp/myapp/settings.py:
# Set ALLOWED_HOSTS
ALLOWED_HOSTS = ['yourdomain.com', 'www.yourdomain.com', 'your-vps-ip']
# Static files
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/myapp/staticfiles'
# Media files
MEDIA_URL = '/media/'
MEDIA_ROOT = '/var/www/myapp/media'
# Database (PostgreSQL)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'myapp_db',
'USER': 'myapp_user',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '5432',
}
}
Step 9: Collect Static Files
cd /var/www/myapp
source venv/bin/activate
python manage.py collectstatic --noinput
python manage.py migrate
Step 10: Configure Gunicorn Systemd Service
Create the file /etc/systemd/system/gunicorn.service:
[Unit]
Description=Gunicorn WSGI server for Django
After=network.target
[Service]
User=django
Group=django
WorkingDirectory=/var/www/myapp
ExecStart=/var/www/myapp/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/var/www/myapp/gunicorn.sock \
myapp.wsgi:application
[Install]
WantedBy=multi-user.target
Enable and start the service:
systemctl daemon-reload
systemctl enable gunicorn
systemctl start gunicorn
systemctl status gunicorn
Choosing the Right Number of Workers
The general formula is (CPU cores × 2) + 1. For example, a 2 vCPU VPS should use 5 workers. If your server has less than 1 GB of RAM, start with 2–3 workers and tune based on real usage.
Step 11: Configure Nginx
Create the file /etc/nginx/sites-available/myapp:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/myapp;
}
location /media/ {
root /var/www/myapp;
}
location / {
include proxy_params;
proxy_pass http://unix:/var/www/myapp/gunicorn.sock;
}
}
Enable the site and reload Nginx:
ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx
Static Files and Media Files
Set the correct permissions for the static and media directories:
mkdir -p /var/www/myapp/staticfiles
mkdir -p /var/www/myapp/media
chown -R django:www-data /var/www/myapp/staticfiles
chown -R django:www-data /var/www/myapp/media
chmod 755 /var/www/myapp/staticfiles
chmod 755 /var/www/myapp/media
www-data is the Nginx user. Nginx needs read access to static files, while Django (user: django) should remain the owner.
Verify Django Is Running Correctly
Use these commands to check the status of your deployment:
# 1. Check whether the Gunicorn socket was created
ls -la /var/www/myapp/gunicorn.sock
# 2. View Gunicorn logs
journalctl -u gunicorn -n 50
# 3. Test Nginx configuration
nginx -t
# 4. Watch Nginx error log
tail -f /var/log/nginx/error.log
# 5. Test HTTP response with curl
curl -I http://yourdomain.com
Common Troubleshooting
Error: "502 Bad Gateway"
Cause: The Gunicorn socket is not running or the path is incorrect.
# Check Gunicorn status
systemctl status gunicorn
# If inactive, restart it
systemctl restart gunicorn
# Verify the socket file exists
ls -la /var/www/myapp/gunicorn.sock
Error: "DisallowedHost at /"
Cause: Your domain or IP is not in ALLOWED_HOSTS in settings.py.
# Edit settings.py
ALLOWED_HOSTS = ['yourdomain.com', 'www.yourdomain.com', '123.456.789.000']
# Restart Gunicorn after editing
systemctl restart gunicorn
Error: "No module named 'django'"
Cause: Gunicorn is not using the virtualenv, or the path in the service file is wrong.
# ExecStart in gunicorn.service must point to the venv:
ExecStart=/var/www/myapp/venv/bin/gunicorn ...
# Not the system binary:
ExecStart=/usr/bin/gunicorn ...
Error: "Permission denied on socket"
Cause: Nginx does not have permission to read the Gunicorn socket file.
# Add www-data to the django group
usermod -aG django www-data
# Or set socket permissions in gunicorn.service
ExecStart=/var/www/myapp/venv/bin/gunicorn \
--workers 3 \
--bind unix:/var/www/myapp/gunicorn.sock \
--umask 007 \
myapp.wsgi:application
systemctl daemon-reload
systemctl restart gunicorn
Frequently Asked Questions
django.db.backends.sqlite3.apt install certbot python3-certbot-nginx -y, then run certbot --nginx -d yourdomain.com. Certbot will automatically modify your Nginx config and install a free SSL certificate.(2 × number of CPU cores) + 1. For a 2 vCPU VPS, that means 5 workers. If your server has less than 1 GB of RAM, start with 2–3 workers and adjust based on actual traffic.