This guide walks you through setting up a production-ready Django server using Gunicorn and Nginx on a Linux server (e.g., DigitalOcean, Ubuntu VPS).
📁 Project Setup
- Django project location:
/var/www/pythonct - Virtual environment:
/var/www/pythonct/venv - Gunicorn socket:
/var/www/pythonct/gunicorn.sock - Static files:
/var/www/pythonct/static/ - Media files:
/var/www/pythonct/media/ - Server IP:
165.22.210.213
🔧 Nginx Configuration
File: /etc/nginx/sites-available/pythonct
server {
listen 80;
server_name 165.22.210.213;
location /static/ {
alias /var/www/pythonct/static/;
}
location /media/ {
alias /var/www/pythonct/media/;
}
location / {
include proxy_params;
proxy_pass http://unix:/var/www/pythonct/gunicorn.sock;
}
}
Enable the site and restart Nginx:
sudo ln -s /etc/nginx/sites-available/pythonct /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl restart nginx
⚙️ Gunicorn systemd Service
File: /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon for Django project
After=network.target
[Service]
User=hemant
Group=www-data
WorkingDirectory=/var/www/pythonct
ExecStart=/var/www/pythonct/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/var/www/pythonct/gunicorn.sock \
ct.wsgi:application
[Install]
WantedBy=multi-user.target
Reload and start the Gunicorn service:
sudo systemctl daemon-reload sudo systemctl start gunicorn sudo systemctl enable gunicorn
✅ Final Check
Visit your site:
http://165.22.210.213/
If you see your Django homepage or login screen, the setup is working! 🎉
🔐 Optional: Enable SSL (HTTPS)
If you have a domain name, run the following:
sudo apt install certbot python3-certbot-nginx -y sudo certbot --nginx -d yourdomain.com
That's it! You've successfully deployed Django with Gunicorn and Nginx professionally and securely.