Docs / Programming & Development / Running a Production Django Application on a VPS

Running a Production Django Application on a VPS

By Admin · Feb 25, 2026 · Updated Apr 24, 2026 · 48 views · 2 min read

Prerequisites

sudo apt install -y python3 python3-pip python3-venv nginx

Deploy Your Application

cd /var/www
git clone https://github.com/yourname/myapp.git
cd myapp

# Create virtual environment
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pip install gunicorn

Configure Django for Production

In settings.py:

DEBUG = False
ALLOWED_HOSTS = ["example.com", "www.example.com"]
STATIC_ROOT = "/var/www/myapp/staticfiles"

# Run collectstatic
python manage.py collectstatic --noinput
python manage.py migrate

Gunicorn Service

Create /etc/systemd/system/gunicorn.service:

[Unit]
Description=Gunicorn Django
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/myapp
ExecStart=/var/www/myapp/venv/bin/gunicorn --workers 3 --bind unix:/run/gunicorn.sock myapp.wsgi:application

[Install]
WantedBy=multi-user.target
sudo systemctl enable --now gunicorn

Nginx Configuration

server {
    listen 80;
    server_name example.com;

    location /static/ {
        alias /var/www/myapp/staticfiles/;
    }

    location /media/ {
        alias /var/www/myapp/media/;
    }

    location / {
        proxy_pass http://unix:/run/gunicorn.sock;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

SSL

sudo certbot --nginx -d example.com

Environment Variables

Store secrets in an .env file or use systemd environment variables — never commit secrets to git:

# In gunicorn.service
Environment=DJANGO_SECRET_KEY=your-secret-key
Environment=DATABASE_URL=postgres://...

Was this article helpful?