How to Deploy Django to a VPS With Gunicorn and Nginx (Ubuntu)

Key Takeaways
- ✓Django's runserver is for development only; Gunicorn runs it in production.
- ✓systemd keeps Gunicorn running and restarts it on failure.
- ✓Nginx serves static files directly and proxies everything else to Gunicorn.
- ✓Most failures are a wrong socket path, permissions or missing collectstatic.
Table of Contents
- •How to Deploy Django to a VPS With Gunicorn and Nginx (Ubuntu)
- •What You Need Before You Start
- •Step 1: Install the Packages
- •Step 2: Set Up the Project and Virtual Environment
- •Step 3: Production Settings
- •Step 4: Test Gunicorn
- •Step 5: Run Gunicorn With systemd
- •Step 6: Configure Nginx
- •Step 7: Add HTTPS
- •Deploying Updates
- •Troubleshooting
- •Frequently Asked Questions
- •Prefer Push-to-Deploy?
How to Deploy Django to a VPS With Gunicorn and Nginx (Ubuntu)
The standard way to deploy Django to a VPS is Gunicorn (a Python WSGI server) behind Nginx. Gunicorn runs your Django code with several worker processes; Nginx handles HTTPS, serves static files fast and passes the dynamic requests to Gunicorn. This guide uses Ubuntu, a project in /srv/myproject and a Django project package called myproject; replace those names with yours.
What You Need Before You Start
An Ubuntu VPS with root or sudo access.
A domain name pointed at the server’s IP address (an A record).
SSH access. Working as a non-root user with sudo is safer than logging in as root.
Your Django project in Git, with a
requirements.txt.
Step 1: Install the Packages
sudo apt update
sudo apt install python3-venv python3-pip nginxIf UFW is enabled, allow SSH and web traffic before anything else, so you don’t lock yourself out:
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enableMore on firewall rules in our guide to opening ports on a VPS.
Step 2: Set Up the Project and Virtual Environment
cd /srv/myproject
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt gunicornStep 3: Production Settings
In settings.py (or your production settings file):
DEBUG = False
ALLOWED_HOSTS = ["example.com", "www.example.com"]
CSRF_TRUSTED_ORIGINS = ["https://example.com", "https://www.example.com"]
STATIC_ROOT = BASE_DIR / "staticfiles"
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")Keep the secret key and database password in environment variables, not in the repository. Then collect static files and run migrations:
python manage.py collectstatic --noinput
python manage.py migrateThen run Django’s own deployment check, which lists settings that are unsafe in production (such as a missing SECURE_HSTS_SECONDS or insecure cookies):
python manage.py check --deployStep 4: Test Gunicorn
gunicorn --bind 0.0.0.0:8000 myproject.wsgi:applicationOpen http://SERVER_IP:8000 (temporarily allow the port if your firewall blocks it). Pages load without styling at this point because Nginx will serve the static files. Stop it with Ctrl+C.
Step 5: Run Gunicorn With systemd
Create /etc/systemd/system/gunicorn.service:
[Unit]
Description=Gunicorn for myproject
After=network.target
[Service]
User=deploy
Group=www-data
WorkingDirectory=/srv/myproject
RuntimeDirectory=gunicorn
ExecStart=/srv/myproject/venv/bin/gunicorn --workers 3 --bind unix:/run/gunicorn/gunicorn.sock myproject.wsgi:application
Restart=on-failure
[Install]
WantedBy=multi-user.targetRuntimeDirectory creates /run/gunicorn on start; the www-data group lets Nginx reach the socket. Gunicorn's documentation suggests (2 x CPU cores) + 1 workers as a starting point.
sudo systemctl daemon-reload
sudo systemctl enable --now gunicorn
sudo systemctl status gunicornStep 6: Configure Nginx
Create /etc/nginx/sites-available/myproject:
server {
listen 80;
server_name example.com www.example.com;
location /static/ {
alias /srv/myproject/staticfiles/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn/gunicorn.sock;
}
}sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginxNote the trailing slashes on /static/ and the alias path; mixing up root and alias is the classic cause of 404s on static files (see Nginx 404 Not Found).
Step 7: Add HTTPS
Point your domain at the server, then follow Nginx + Let's Encrypt to get a free certificate and redirect HTTP to HTTPS.
Deploying Updates
cd /srv/myproject && git pull
source venv/bin/activate
pip install -r requirements.txt
python manage.py migrate
python manage.py collectstatic --noinput
sudo systemctl restart gunicornTroubleshooting
- 502 Bad Gateway: Gunicorn is not running or the socket path differs between the service and Nginx. Check
sudo journalctl -u gunicorn -n 50and see fixing 502 errors. - Permission denied on the socket: the service's group is not
www-data, or the project lives in a home folder Nginx cannot traverse (see 403 Forbidden). - CSRF verification failed over HTTPS: add your https origins to
CSRF_TRUSTED_ORIGINS. - Bad Request (400): the domain is missing from
ALLOWED_HOSTS.
Frequently Asked Questions
Why use Gunicorn with Django?
Django's built-in runserver is single-process and not built for security or load. Gunicorn runs multiple worker processes and is designed for production.
Do I need Nginx in front of Gunicorn?
Yes in most setups. Nginx handles HTTPS, slow clients and static files efficiently, which Gunicorn is not designed to do.
How many Gunicorn workers should I use?
Start with (2 x CPU cores) + 1 and adjust based on memory use and response times.
Prefer Push-to-Deploy?
RackUp IT hosting runs Python apps on managed servers with SSL, monitoring and backups handled, so you spend less time on systemd and Nginx files.
Unsure Which Cloud Hosting Plan Fits Your Site?
Answer 4 quick diagnostic questions to estimate the server CPU, RAM and storage you need with transparent pricing.
Cloud Infrastructure Engineer.
Related Posts
VPS HostingVultr Alternatives in 2026: VPS Hosting for Developers Compared
Vultr's Regular Performance prices compared with DigitalOcean, Linode, Hetzner and a fixed-plan VPS at the 8 GB size, plus when container hosting fits better.
VPS HostingWhat Is a VPS? Virtual Private Servers Explained
A VPS (virtual private server) is a virtual machine with its own operating system, resources and root access. How it works, uses, and when you need one.
VPS HostingLinode (Akamai) Alternatives in 2026: VPS Hosting Compared
Linode's shared CPU prices from Akamai's own API, compared with DigitalOcean, Vultr and a fixed-plan VPS, plus when container hosting makes more sense.