February 3, 2023
Updated June 7, 2023

How to set up a system for a Django project (Postgres; Ngnix; Gunicorn) on Linux Ubuntu 20.04

Linux Ubuntu


Django is a free and open source framework for developing web applications in Python. It provides powerful tools to develop complete websites and web applications. It allows developers to create applications quickly and efficiently without losing quality.

Django is famous for its versatility and power in web development. It is a powerful framework for building applications in Python, which helps reduce development time with its library of tools and modules. It is also known for its powerful templating and automatic URL construction features. This framework also provides good application security as it automatically checks incoming data and prevents attacks.

Comparison with other frameworks

  • Flask (Python) Disadvantages: you have to write most of the code yourself and not enough tools for large projects;
  • Ruby on Rails Disadvantages: Slow start-up, high memory overhead and high threshold for entry;
  • Pyramid Disadvantages: Difficult to set up and not enough tools for large projects;
  • Laravel (PHP) Disadvantages: difficult to configure and no easy-to-use tool to build a "database".

Postgres is known for its high reliability and performance. It also has advanced features for data analysis, including analytical functions and tools for dealing with geospatialization. There is also a wide range of plugins that can complement its functionality.

Gunicorn is renowned for its ease of use and high performance. It is a simple Python process manager that can handle multiple requests on a single server. Gunicorn has many built-in features such as support for the HTTP/1.1 protocol, automatic restarting and scaling vorkers. It can also be used in combination with other web frameworks such as Django and Flask.

Nginx is one of the most popular and reliable web servers. It is easy to install and configure, has high performance and low resource usage, supports web applications and dynamic websites, and has many different features, including static content pushing, proxying, URL redirection and routing. It can mainly be used to handle web traffic, as well as for fault tolerance and high performance.

Getting started

Update the software packages.

sudo apt update

Install the following software.

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

Configure database

Login to Postgres.

sudo -u postgres psql

Command to create a database for the project.

CREATE DATABASE "yournameproject";

Create a database, a user, and optimize our database to make it work faster.

CREATE DATABASE "yournameproject"


The database has been created and we need to make changes on certain parameters.


CREATE USER "YourUsername" WITH PASSWORD 'password';

The user was successfully created and I decided to optimize the "DB" by changing the parameters to speed up our "DB".


ALTER ROLE "Yourproject" SET timezone TO 'UTC';
ALTER ROLE "Yourproject" SET default_transaction_isolation TO 'read committed';
ALTER ROLE "Yourproject" SET client_encoding TO 'utf8';

These parameters are created to give the user an account to connect to the database with a password.

Next, the role for the user will be configured with a default time zone of UTC, a default transaction isolation of "Read Completely" and a default client encoding of UTF-8.

Granted access to the user to administer the "DB".


GRANT ALL PRIVILEGES ON DATABASE "yourproject" TO "myprojectuser";

Creating a workspace for project

I have created a "DB" and now we can move on to the remaining requirements of the project. To make things a little easier, I have set up all the requirements we need in a virtual environment.

Let's write these commands.

sudo -H pip3 install --upgrade pip
sudo -H pip3 install virtualenv

After installing the "virtualenv" tool, I start shaping our project.
I have created a directory where we will store our files for the project, and navigate to it

mkdir ~/yourprojectdir
cd ~/myprojectdir


In the directory created a virtual environment.

virtualenv yourprojectenv


virtualenv is a command that allows you to create isolated environments for different projects and use different versions of libraries and applications.

This allows the same technology stack to be maintained for all projects in the organisation, reducing the risk of incompatibilities and bugs.

This directory will immediately contain the local version of Python and the package management system.
Before we assign requirements to our project we need to run the virtual environment. Type this command.

source yourprojectenv/bin/activate

The command line will change, showing that you are now running in the Python virtual environment. It will be like this.

As you run the virtual environment, install the required components.

pip install django gunicorn psycopg2-binary


Project creation

A project directory has already been created for the framework, so we need to specify the path for initialization, because the second directory contains the subdirectory with the control script code inside it.

django-admin startproject yourproject ~/yourprojectdir

After that, you will need to change a couple of parameters in the generated project file. Open settings file with any text editor.

nano ~/yourprojectdir/yourproject/

Now we need to change the settings.


Then scroll below and find the section that provides the password, the name is "DB".

Also look for string DATABASES, configure this string for your data.


Scroll down and add the parameter to specify where you want to store static files.


Final configuration

Using the management scripts I will move the "DB" schema.

~/yourprojectdir/ migrate

Create an administrative user for the project.

~/yourprojectdir/ createsuperuser

Collected all the static content in the given directory.

~/myprojectdir/ collectstatic

The command needs to be validated. After that, all the files will be moved to the "static" directory of the project.

Also open the port which will be used for the project.

sudo ufw allow 2222


Check our project by starting the framework server.

~/myprojectdir/ runserver your_ip:2222

Go to the web browser to check if the framework works.


After authorization in to the web resource, you are directed to the control panel.



In this post I have shown you how to prepare your Ubuntu 20.04 system to start a Django project with a Postgres database; Gunicorn web server and configured Ngnix proxy.


5 out of 5
Аverage rating : 5
Rated by: 1
19703 Claymont 2093 Philadelphia Pike #2345, USA
+1 302 425-97-76
700 300
700 300
We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.