This guide walks through deploying a Django application to a Linux VPS using the standard production stack: Gunicorn as the application server, Nginx as the reverse proxy, and PostgreSQL as the database. It assumes an Ubuntu or Debian VPS with root access.
1. Prepare the server
sudo apt update && sudo apt upgrade -y
sudo apt install python3-venv python3-pip nginx postgresql -y
2. Set up PostgreSQL
sudo -u postgres psql
CREATE DATABASE myapp;
CREATE USER myappuser WITH PASSWORD 'strong-password';
GRANT ALL PRIVILEGES ON DATABASE myapp TO myappuser;
\q
3. Deploy the code and virtualenv
cd /var/www && git clone https://your-repo.git myapp && cd myapp
python3 -m venv venv && source venv/bin/activate
pip install -r requirements.txt gunicorn psycopg2-binary
Set your production settings: DEBUG = False, a strong SECRET_KEY from the environment, and ALLOWED_HOSTS including your domain. Then collect static files and run migrations:
python manage.py collectstatic --noinput
python manage.py migrate
4. Run Gunicorn under systemd
Create /etc/systemd/system/myapp.service so Gunicorn starts on boot and restarts on failure:
[Unit]
Description=myapp gunicorn
After=network.target
[Service]
User=www-data
WorkingDirectory=/var/www/myapp
ExecStart=/var/www/myapp/venv/bin/gunicorn myapp.wsgi:application --bind 127.0.0.1:8000 --workers 3
[Install]
WantedBy=multi-user.target
sudo systemctl enable --now myapp
5. Configure Nginx
server {
listen 80;
server_name example.com;
location /static/ { alias /var/www/myapp/staticfiles/; }
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
6. Add HTTPS
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com
You're live
Your Django app now runs behind Nginx with HTTPS, managed by systemd. On a OneHost Linux VPS with NVMe SSD and full root access, this stack deploys in minutes and scales by adding Gunicorn workers or upgrading your plan.