How can we help?
Categories
< All Topics
Print

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> 0.0.0.0:8000

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:

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=yourusername
Group=www-data
WorkingDirectory=/home/yourusername/mydjangoapp
ExecStart=/home/yourusername/mydjangoapp/env/bin/gunicorn \
–access-logfile – \
–workers 3 \
–bind unix:/run/gunicorn.sock \
myproject.wsgi:application

[Install]
WantedBy=multi-user.target

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 =https://cdn.serverstadium.com/favicon.ico { 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>

Conclusion

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