Django / Django Deployment and Production

Using Gunicorn and Nginx for Production

In this tutorial, you will learn how to use Gunicorn and Nginx to serve your Django application in a production environment. We'll cover installation, configuration, and how to st…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers deploying Django applications to production environments with security and performance in mind.

1. Introduction

In this tutorial, our main goal is to guide you through the process of setting up a Django application to run in a production environment using Gunicorn and Nginx. Gunicorn is a Python WSGI HTTP Server that usually stands between a web framework (like Django) and an HTTP client. Nginx, on the other hand, is a web server which can act as a reverse proxy for Gunicorn, serving static files more efficiently than Gunicorn.

By the end of this tutorial, you will learn how to:

  • Install Gunicorn and Nginx
  • Configure Gunicorn to run a Django application
  • Configure Nginx to reverse proxy to Gunicorn
  • Serve your Django application using Gunicorn and Nginx

Prerequisites:

  • Basic knowledge of Python and Django
  • A Django application to deploy
  • A Linux server where the application will be hosted

2. Step-by-Step Guide

Installing Gunicorn and Nginx

First, we need to install Gunicorn and Nginx. On an Ubuntu server, you can do this using the following commands:

sudo apt-get update
sudo apt-get install python3-gunicorn nginx

Configuring Gunicorn

Next, we need to configure Gunicorn to serve our Django application. To do this, navigate to your Django project's directory and run the following command:

gunicorn myproject.wsgi:application --bind 127.0.0.1:8000

Replace myproject with the name of your Django project. This command tells Gunicorn to serve the Django application on localhost at port 8000.

Configuring Nginx

After Gunicorn is set up, we need to configure Nginx to reverse proxy to Gunicorn. Open the Nginx configuration file using the following command:

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

In this file, add the following configuration:

server {
    listen 80;
    server_name your_domain www.your_domain;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/username/myproject;
    }

    location / {
        include proxy_params;
        proxy_pass http://127.0.0.1:8000;
    }
}

Replace your_domain with your server's domain name, and replace username and myproject with your username and the name of your Django project, respectively.

3. Code Examples

Example 1: Running Gunicorn

Here's an example of how you can run Gunicorn to serve your Django application:

cd /home/username/myproject
gunicorn myproject.wsgi:application --bind 127.0.0.1:8000

In this example, we first navigate to our Django project's directory. Then, we run the gunicorn command to serve our application.

Example 2: Nginx Configuration

Here's an example of an Nginx configuration file:

server {
    listen 80;
    server_name your_domain www.your_domain;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/username/myproject;
    }

    location / {
        include proxy_params;
        proxy_pass http://127.0.0.1:8000;
    }
}

In this file, we configure Nginx to listen on port 80 (the default HTTP port), and to reverse proxy to Gunicorn at 127.0.0.1:8000.

4. Summary

In this tutorial, we've learned how to install Gunicorn and Nginx, how to configure Gunicorn to run a Django application, and how to configure Nginx to reverse proxy to Gunicorn. We've also seen some examples of these configurations.

Next steps for learning include exploring more about deploying Django applications, learning about different ways to configure Gunicorn and Nginx, and understanding how to secure your application in a production environment.

5. Practice Exercises

  1. Install Gunicorn and Nginx on your own server and deploy a simple Django application.
  2. Experiment with different Gunicorn configurations, such as changing the number of worker processes.
  3. Try to configure Nginx to serve your Django application over HTTPS.

Remember, practice is key when it comes to learning new concepts. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

JWT Decoder

Decode and validate JSON Web Tokens (JWT).

Use tool

EXIF Data Viewer/Remover

View and remove metadata from image files.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Image Converter

Convert between different image formats.

Use tool

Image Compressor

Reduce image file sizes while maintaining quality.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help