Kamal Hosen
Kamal Hosen
Geospatial Developer | Data Science | Python

Sep 11, 2023

Deploy Django with PostgreSQL, Gunicorn, Nginx, and Supervisor on Ubuntu 22.04 LTS


Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It follows the "batteries-included" philosophy, which means it provides many built-in features and libraries for common web development tasks. Django includes an Object-Relational Mapping (ORM) system for database interaction, a templating engine, an authentication system, and various utilities for handling common web-related tasks. It promotes the use of the Model-View-Controller (MVC) architectural pattern and is known for its emphasis on DRY (Don't Repeat Yourself) and convention over configuration principles. Django is well-suited for building a wide range of web applications, from simple blogs to complex, data-driven platforms.

What is NGINX?


Nginx (pronounced "engine-x") is a high-performance, open-source web server and reverse proxy server. It's often used as a front-end web server to serve static content, handle SSL/TLS termination, and act as a load balancer. Nginx is known for its efficiency in handling concurrent connections and its ability to scale well under heavy loads. It's commonly used to improve the performance, security, and reliability of web applications. When used in conjunction with Gunicorn and Django, Nginx can serve static files directly and pass requests for dynamic content to Gunicorn.


What is Gunicorn?

Gunicorn is a Python Web Server Gateway Interface (WSGI) HTTP server. It's designed to serve Python web applications, like those built with Django. Gunicorn is responsible for handling HTTP requests, interfacing with your Django application, and managing worker processes to serve your application. It's a WSGI HTTP server that follows the WSGI specification, allowing it to seamlessly integrate with Django and other Python web frameworks. Gunicorn is often used because of its simplicity and reliability. It's a good choice for deploying Django applications in a production environment.


What is Supervisor?

In the context of web application deployment using Gunicorn, a "supervisor" refers to a process management tool that is used to monitor and control the Gunicorn process or multiple Gunicorn instances. Supervisors are especially useful for ensuring that your Gunicorn-powered web applications run reliably and can be automatically restarted in case of failures or crashes.


What is PostgreSQL?

PostgreSQL is a powerful, open-source relational database management system (RDBMS) known for its robust features, extensibility, and scalability. It is often referred to as "Postgres" for short. Here are some key aspects and features of PostgreSQL.


Step -1: Update & Upgrade Ubuntu System Files

sudo apt update
sudo apt upgrade
sudo apt autoremove 
sudo apt autoclean

Step -2: Install Necessary Packages

sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl

Step -3: Create and Activate Virtual Environment

Navigate to your project's directory:

mkdir myproject
cd myproject
python3 -m venv myenv
source myenv/bin/activate

Step -4: Install Django, Gunicorn and Other Dependencies

git clone https://your-repo-link.git
cd your-project-name
pip install -r requirements.txt
pip install gunicorn

Step -5: Setup PostgreSQL

sudo -u postgres psql
CREATE USER myuser WITH PASSWORD 'mypassword';


Step -6: Update your Django settings to use this database.

When you install Django, it comes with default sqlite3 database.

'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',

If you want to use PostgreSQL, you need to update the database configuration.

    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': 'localhost',
        'PORT': '5432',

Step -7: Install and Configure Supervisor

sudo apt install supervisor
sudo systemctl enable supervisor
sudo systemctl start supervisor

Now, create a Supervisor configuration file for your project:

sudo nano /etc/supervisor/conf.d/myproject.conf

// Add this
command=/path/to/myproject/myenv/bin/gunicorn myproject.wsgi:application --bind

Update Supervisor with the new configuration:

sudo supervisorctl reread
sudo supervisorctl update


Step -8: Configure Nginx for Django

First, remove default Nginx configuration:

sudo rm /etc/nginx/sites-enabled/default

Now create a new configuration for your project:

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

// Add this
server {
    listen 80;
    server_name <ADD_YOUR_DOMAIN_OR_IP_ADDRESS>;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    location /static/ {
        alias /path/to/myproject/static/;

    location /media/ {
        alias /path/to/myproject/media/;

Activate the new configuration:

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

sudo nginx -t
sudo systemctl restart nginx


Share To

About Author
  • Kamal Hosen
  • Kamal Hosen
    Geospatial Developer | Data Science | Python

    A passionate geospatial developer and analyst whose core interest is developing geospatial products/services to support the decision-making process in climate change and disaster risk reduction, spatial planning process, natural resources management, and land management sectors. I love learning and working with open source technologies like Python, Django, LeafletJS, PostGIS, GeoServer, and Google Earth Engine.