How to host Django Application using gunicorn & nginx in Production

In this post, we will see how to use nginx with gunicorn to serve django applications in production.

Django is a very powerful web framework and ships with a server which is able to facilitate development. This development server is not scalable and is not suited for production. Hence we need to configure gunicorn to get better scalability and nginx can be used as a reverse proxy and as a web server to serve static files. Let’s get started

Before you follow the steps outlined below, I will assume that you have already configured your Ubuntu server with a non root user and firewall as outlined here.

Step 1 – Installing python and nginx

Let’s update the server’s package index using the command below:

Step 2 – Install Nginx

Install Nginx using the command below:

Step 3 – Allow Nginx through the firewall

Allow Nginx through the firewall using the command below:

Step 4 – Install & Configure PostgreSQL

Let’s install PostgreSQL using the command below:

While you are logged into the server, run following commands to install, start, enable and see the status of postgresql database:

Step 5 -Postgres Database & User Setup

You should now be logged into the pg shell

Create a database

Create user

Set default encoding, tansaction isolation scheme (Recommended from Django)

Give User access to database

Quit out of Postgres

Step 6 – Installing python and nginx

Let’s update the server’s package index using the command below:

This will install python, pip and nginx server

Step 7 – Creating a python virtual environment

This will install a virtual environment package in python. Let’s create a project directory to host our Django application and create a virtual environment inside that directory.

A virtual environment named env will be created. Let’s activate this virtual environment:

Step 8 – Installing Django and gunicorn

This installs Django and gunicorn in our virtual environment

Step 9 – Possible Deployment Issues

To avoid deployment errors, we will take following actions:

Add allowed host, release media folder, run collectstatic and create requirements.txt from the local project.

  • Replace psycopg2 package with psycopg2-binary
  • Install backports.zoneinfo only if the server python version is less than 3.9
  • backports.zoneinfo==0.2.1;python_version<"3.9"
  • Add gunicorn to requirements.txt

Step 10 – Backup data from local machine and restore in to live database

Now check the remote directory, our project is pushed to the server. That’s all, we have set up Filezilla to upload the code from local machine to ftp server.

Open Django Project and check your project settings 

cd /home/dydevops/myproject

In the myproject folder, activate virtual environment 

Install the packages from requirements.txt file

Run makemigrations and migrate commands

Allow port 8000

Check status of ufw

Run the server

Remote server

Configuring Gunicorn

Install Gunicorn on our server
if you are not install Gunicorn then install ..

Other wise run tell gunicorn to bind to our Django application and start running

Test the link and make sure the site is working


Go to this location and paste below code:


Restart and enable Gunicorn socket

 Configuring Nginx as a reverse proxy


 Open port 80 and close 8000

 Go to sites-enabled directory and delete the default site

 Fix Static Files on Server

Set Your domain name server

Open Django Project and check your project settings 

Install SSL

Verify certbot installation by running which certbot

Share on:

Hello, I am Dharmendra Yadav and I am a Python Developer with experience in web development using Django, Flask, REST API, SQL, MySQL, HTML, CSS, JavaScript, WordPress, Oracle Cloud, AWS and Git. I also write technical articles where I explain web development and Software Engineering. Facebook , Linkedin

Leave a Comment