blog-image

Jun 03, 2024

14 min read

How to Create and Deploy a Django Application Using Gunicorn and Nginx on Ubuntu 22.04

Written by

Abdelhadi Dyouri

Introduction

Django is a web framework that allows you to create web applications using the Python language. Gunicorn is a Python Web Server Gateway Interface HTTP server, allowing you to serve your Python web application in an efficient, production-ready environment. NGINX is an open-source web server delivering web content through the internet, as well as reverse proxying, caching, load balancing, media streaming, and more.

This tutorial will show you how to create a small Django application, serve it using Gunicorn, and configure Nginx as a reverse proxy that sits in front of Gunicorn and forwards client requests to it, which improves scalability, performance, resilience, and security.

Prerequisites

To follow this tutorial you need:

  • Basic knowledge of the Linux command line.
  • An Ubuntu 22.04 server with a non-root user with sudo privileges. You can get affordable, and powerful Ubuntu servers from our website, and you can check out our How to access your server using SSH guide to learn how to access your server and create a sudo user.
  • Nginx installed on your Ubuntu server, follow our Installing LEMP on Ubuntu 22.04 guide to install it on your server.

Update The Package Cache

Start by updating the packages in the package manager cache to the latest available versions using the following command:

sudo apt update

Installing Core Dependencies

Before you start writing your Django application, you need to install some core dependencies for your Python programming environment. To do this, use the following command:

sudo apt install python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools python3-venv

With this, you can now create a Python virtual environment for your project.

Creating a Django Application Inside a Virtual Environment

Before you install Django and Gunicorn, you will first create a project folder and then create a Python virtual environment inside it to isolate your project code and dependencies from the rest of the system.

Python virtual environments allow you to have multiple isolated Python environments on one computer. This is useful for projects that require different versions of Python, or for projects that you don't want to contaminate with code from other projects. This ensures that each Python project is isolated from your other projects, so that conflict between dependencies does not occur.

First, create the project folder, we'll call it myapp:

mkdir myapp

Navigate to it:

cd myapp

Create a Python virtual environment inside your myapp project folder:

python3 -m venv env

Activate it:

source env/bin/activate

Your command line should now have an (env) prefix indicating that your virtual environment is activated.

Install Django and Gunicorn:

pip install django gunicorn

With Django and Gunicorn now installed, you can now create a small application.

Building a Small Django Application

We'll create a small Django application that displays a Hello World! web page.

First, in your myapp folder, create a new Django project using the Django admin:

django-admin startproject mysite

This creates a new mysite directory containing the Django files that are needed for our demo application.

Navigate to this new mysite folder:

cd mysite

Next, create a new application inside this Django project with the following

Continue reading this article
by subscribing to our newsletter.
Subscribe now

A note about tutorials: We encourage our users to try out tutorials, but they aren't fully supported by our team—we can't always provide support when things go wrong. Be sure to check which OS and version it was tested with before you proceed.

If you want a fully managed experience, with dedicated support for any application you might want to run, contact us for more information.

Leave a Reply