How can we help?
< All Topics

Making Your Django Project Production-Ready on Ubuntu 22.04: Gunicorn and Nginx Setup

This continuation of our guide will show you how to make your Django project production-ready on a ServerStadium VM using Gunicorn and Nginx.

Step 8: Install and Configure Gunicorn

Install Gunicorn with pip:

pip3 install gunicorn

Test Gunicorn’s ability to serve the project:

gunicorn –workers 3 myproject.wsgi:application –<span class="hljs-built_in">bind</span>

Now, visit http://your_server_ip:8000 to confirm it’s serving your Django site correctly.

Step 9: Create a Gunicorn Systemd Service File

Create a systemd service file for Gunicorn to manage your process:

sudo nano /etc/systemd/system/gunicorn.service

Add the following content, adjusting paths and usernames as necessary:

Description=gunicorn daemon

ExecStart=/home/yourusername/mydjangoapp/env/bin/gunicorn \
–access-logfile – \
–workers 3 \
–bind unix:/run/gunicorn.sock \


Enable and start the Gunicorn service:

sudo systemctl start gunicorn</div>
<div class="p-4 overflow-y-auto">sudo systemctl <span class="hljs-built_in">enable</span> gunicorn

Step 10: Install and Configure Nginx

Install Nginx:

sudo apt install nginx

Configure Nginx to proxy pass to Gunicorn. Create a new server block:

sudo nano /etc/nginx/sites-available/mydjangoapp

Add the following configuration:

server {
listen 80;
server_name your_server_ip;

location = { access_log off; log_not_found off; }
location /static/ {
root /home/yourusername/mydjangoapp;

location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;

Link this file to the sites-enabled directory:

sudo <span class="hljs-built_in">ln</span> -s /etc/nginx/sites-available/mydjangoapp /etc/nginx/sites-enabled

Test your Nginx configuration:

sudo nginx -t

Restart Nginx:

sudo systemctl restart nginx

Step 11: Secure Your Server with a Firewall

Enable the Nginx Full profile on ufw:

sudo ufw allow <span class="hljs-string">'Nginx Full'</span>


Your Django application is now configured with Gunicorn and Nginx on your ServerStadium VM, ready for production.

For additional support, check out our knowledge base or contact our support team.

Table of Contents