Run production Django backends with virtual environments, Gunicorn Unix sockets, and static file caching
By following this guide, you will deploy a production Python Django web application on Ubuntu. Gunicorn will serve dynamic requests over a protected Unix socket while Nginx serves static media assets and handles SSL encryption.
Spin up an Ubuntu 24.04 instance with instant activation to test this tutorial:
Deploy IndSoft VPSAn Ubuntu 22.04 or 24.04 VPS with Python 3 and pip installed.
Isolate your Python project dependencies in a dedicated virtualenv.
sudo apt install python3-venv python3-dev libpq-dev nginx -y
python3 -m venv /var/www/myproject/env
source /var/www/myproject/env/bin/activate
pip install django gunicorn psycopg2-binaryCreate /etc/systemd/system/gunicorn.socket and gunicorn.service to automate process lifecycle.
sudo nano /etc/systemd/system/gunicorn.service
# ExecStart=/var/www/myproject/env/bin/gunicorn --workers 3 --bind unix:/run/gunicorn.sock myproject.wsgi:application
sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socketRoute static files directly from disk and proxy dynamic requests to the Unix socket.
sudo nano /etc/nginx/sites-available/myproject
# location /static/ { alias /var/www/myproject/static/; }
# location / { proxy_pass http://unix:/run/gunicorn.sock; }
sudo systemctl reload nginxRun this quick verification check to confirm your installation is responding properly:
sudo systemctl status gunicornQuick resolutions for frequent setup pitfalls and configuration issues.
ls -l /run/gunicorn.sock. The socket must be accessible by www-data.
ALLOWED_HOSTS in your Django settings.py file.
python manage.py collectstatic and verify the path matches the Nginx location /static/ directive.