RackUp IT - View Cloud Hosting Plans

Get started now or speak directly with our engineering team for specialized assistance.

Logo
DevelopmentWEB & APPCalculatorTOOLServicesProductBlogOur TeamCareers
View Plans
Home>Blog>How to Deploy Django to a VPS With Gunicorn and Nginx (Ubuntu)
VPS Hosting

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

Alex Vance
Alex Vance
Published Oct 22, 2026· Updated Oct 26, 2026•✓ Verified Expert Reviewed•3 min read
Deploy Django with Gunicorn and Nginx cover

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 nginx

If 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 enable

More 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 gunicorn

Step 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 migrate

Then 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 --deploy

Step 4: Test Gunicorn

gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application

Open 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.target

RuntimeDirectory 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 gunicorn

Step 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 nginx

Note 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 gunicorn

Troubleshooting

  • 502 Bad Gateway: Gunicorn is not running or the socket path differs between the service and Nginx. Check sudo journalctl -u gunicorn -n 50 and 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.

← Previous ArticleHow to Update Node.js to the Latest Version (nvm, Windows, macOS, Linux)Next Article →How to Install an SSL Certificate on WordPress (and Fix Mixed Content)
Interactive 60-Second Tool

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.

Free Migration 99.9% Uptime Target
Calculate My Hosting Build
Alex Vance
Written ByAlex Vance

Cloud Infrastructure Engineer.

99.9% Uptime Target

RackUp IT - View Cloud Hosting Plans

View Hosting Plans

View Hosting Plans

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?

Categories

  • VPS Hosting
  • DevOps & Tutorials
  • WordPress Hosting
  • Cloud Hosting
  • Server Stack
  • Networking & Security
  • Web Hosting / Managed Cloud
  • E-Commerce
  • Hosting & SEO
  • Hosting Benchmarks
  • Cloud Backup & DevOps
  • Managed VPS

Recent Posts

  • Vultr Alternatives in 2026: VPS Hosting for Developers Compared
  • Docker Port Mapping Explained: -p, EXPOSE and Compose ports
  • Bluehost Alternatives (2026): WordPress Hosting Without the Renewal Price Jump

Tags

DjangoPythonGunicornNginxDeployment

Related Posts

Vultr alternatives coverVPS Hosting
Nov 27, 2026

Vultr 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.

Read Article →
What is a VPS? Virtual private servers explainedVPS Hosting
Nov 20, 2026

What 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.

Read Article →
Linode alternatives coverVPS Hosting
Nov 19, 2026

Linode (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.

Read Article →
Logo

Expert Web & App Development, UI/UX Design, Branding, and Consulting to scale your business.

Company

  • About Us
  • Blog
  • Contact
  • Our Team
  • Careers

Services

  • Web Development
  • App Development
  • UI/UX Design
  • Consulting
  • Managed WordPress VPS
  • Website Speed Test

Subscribe to our newsletter

We accept
  • bKash
  • Nagad
  • Rocket
  • Upay
  • CellFin
  • Meghna Pay
  • OK Wallet
  • Tap
  • mCash
  • DBBL Nexus
  • Visa
  • Mastercard
  • American Express
  • Crypto
Pay with Visa, Mastercard, American Express, DBBL Nexus, CellFin, Rocket, bKash, Tap, Meghna Pay, Nagad, mCash, Upay and OK Wallet. Verified by EPS

© 2026 Rackup IT Solution. All rights reserved.

Privacy PolicyTerms of Service