Evgenij LegotskojSept. 16, 2016, 1:56 p.m.

Django - Tutorial 001. Deploying a site on Django + PostgreSQL + Gunicorn + Nginx

Once access to the server with Ubuntu 16.04 was set in a previous article, it is time to expand on it everything necessary for operation of the site, namely:

  • Django - framework for developing web-applications in Python;
  • PostgreSQL - SQL database;
  • Gunicorn - WSGI HTTP Python server for UNIX systems;
  • Ngnix - HTTP-server and a reverse proxy server, mail proxy server and TCP / UDP general purpose proxy;
  • Supervisor - This process manager, which greatly simplifies the management of long-running programs, such as sites that you want to automatically restart after a fall.

Step 1 - system upgrade

Let us update existing packages. You never know, the system is obsolete packages.

sudo apt-get update
sudo apt-get upgrade

Step 2 - Install all the required packages without installing a virtual environment

Establish a bundle of all the packages that will not be used by virtualenv, namely Python 3, PostgreSQL, Nginx.

sudo apt-get install python3-dev python3-setuptools libpq-dev postgresql postgresql-contrib nginx

Pip I eventually installed separately through easy_install3 utility that comes bundled python3-setuptools , firstly because then put the latest version, and secondly in the case of installation through apt-get are errors when installing packages virtualenv.

sudo easy_install3 pip

Step 3 - Creating a database and a user database

And now create the database and user database, giving extended rights postgres user via the sudo utility, which is created when you install PostgreSQL.

sudo -u postgres psql

After completing this command, we get to PostgreSQL console, where we will create the necessary database and user on whose behalf the Django app will connect to the database.

Create database:


Create user:

CREATE USER myprojectuser WITH PASSWORD 'password';

Next, the user configuration of the project. Encoding it will use UTF8, because this encoding is used in the Django, a Python files recommends hardcoding encoded UTF8, especially if they present the text to be displayed on the site. Also set the isolation level in a database. That is, when the data will be available for reading. In this case, after the transaction is in the general case. Of course, there are cases where the data is available to confirm, but it's a different story. And also set the type of time zones that the default Django UTC.

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

Next, give the right of access to the database for the user:

GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;

Exit from PostgreSQL console.


Step 4 - Installing the virtual environment

Install and activate the virtual environment:

pip3 install virtualenv
virtualenv ~/myprojectenv
source myprojectenv/bin/activate

Step 5 -Installation of PostgreSQL driver

The installation with the following command

sudo pip install django psycopg2 
Inside the virtual environment, you must use the pip command, not pip3

Step 6 - Project creating

Let's move to a folder with a virtual environment, we still have to be in operation in a virtual environment. And create a project.

cd ~/myprojectenv
django-admin.py startproject myproject

Step 7 - Setting up the database connection

Now, edit the configuration file the Django, it connects to a PostgreSQL database, but not create and further connected to the SQLite database.

nano ~/myproject/myproject/settings.py

To do this, locate the following piece of code file:


    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),


And replace it with the following, taking into account your data to connect to the database:


    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'myproject',
        'USER': 'myprojectuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',


Step 8 - Perform database migration

Django has one very big advantage - it has built-in admin panel that very easy life. But to make it work, you must perform a database migration, that is, to prepare a model of the SQL data queries that form the structure of the database.

cd ~/myproject
python manage.py makemigrations
python manage.py migrate

And even create a superuser who is the administrator with the highest access rights to your site. Run the following command and follow the instructions.

python manage.py createsuperuser

Step 9 - Install Gunicorn

Establish Gunicorn, which will act as an HTTP server for our website. Set it inside a virtual environment.

sudo pip install django gunicorn

You can verify that the site is already running:

gunicorn myproject.wsgi:application --bind 111.222.333.44:8000  # Your IP-address

Step 10 - Configuring static files

Django by default gives static files only in Debug mode, which is not used to combat the server. To combat all server static files of all projects going in a separate folder with a special collectstatic command, and the folder itself must be specified in the settings.py file.

Edit it:

nano ~/myproject/settings.py

And add a line that will indicate where to collect static files.

STATIC_ROOT = '/home/user/myprojectenv/myproject/myproject/static/'

And now will collect all statics files in this catalog:

python manage.py collectstatic

Step 11 - Configuring Nginx

Edit the Nginx configuration file.

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

We remove all the contents and replaced with the following.

server {
    listen 80;
    server_name 111.222.333.44; # your ip-address
    access_log  /var/log/nginx/example.log;

    location /static/ {
        root /home/user/myprojectenv/myproject/myproject/;
        expires 30d;

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

Restart Nginx.

sudo service nginx restart

Start gunicorn

gunicorn myproject.wsgi:application

After that, you will find that the site is already available on port 80.

Step 12 - Configuring supervisor

To ensure that the site is accessible at any time of the day, you must configure-supervisor, which hangs in the memory as a service and will make sure that the site has always worked.

sudo apt-get install supervisor

For stable operation Gunicorn need to create its configuration file. It will be located next to the file, which will run for our web-application.

cd /home/user/myprojectenv/myproject/myproject
touch gunicorn.conf.py
nano gunicorn.conf.py

Add the following configuration information:

bind = ''
workers = 3
user = "nobody"

That is, we specify which port is bound and which user starts the process. As well as the number of working processes. In this case, three. It is calculated by the following formula:

workers =  N cpu + 1

Then create a configuration of supervisor

cd /etc/supervisor/conf.d/
touch myproject.conf
nano myproject.conf

The following settings prescribe it

command=/home/user/myprojectenv/bin/gunicorn myproject.wsgi:application -c /home/user/myprojectenv/myproject/myproject/gunicorn.conf.py

Now run the supervisor. There is a caveat installation supervisor. It is not as start his service after having been installed. Therefore it is necessary to force to add it to startup and run manually if you do not want to restart the server.

sudo update-rc.d supervisor enable
sudo service supervisor start

Well, then you can update the config files, check the status of the application site, and restart them.

supervisorctl reread
supervisorctl update
supervisorctl status myproject
supervisor restart myproject

When the server reboots everything will start automatically


If you change the project files, you will need to restart gunicorn. To do this, activate the virtual environment is enough to use killall command

source ~/myprojectenv/bin/activate
sudo killall gunicorn

Supervisor automatically start Gunicorn after this command, so you can not worry that the server will fall for a long time. Not a second failure.

For Django I recommend VDS-server of Timeweb hoster .

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.
Support the author Donate

С чем может быть связана ошибка? ставил всё на свою машину, статический ip не покупал.
(myprojectenv) ubuntu@ubuntu:~/myprojectenv/myproject$ gunicorn myproject.wsgi:application --bind мойайпишник:8000
[2017-10-28 11:54:39 +0000] [31426] [INFO] Starting gunicorn 19.7.1
[2017-10-28 11:54:39 +0000] [31426] [ERROR] Invalid address: ('мойайпишник', 8000)
[2017-10-28 11:54:39 +0000] [31426] [ERROR] Retrying in 1 second.
[2017-10-28 11:54:40 +0000] [31426] [ERROR] Invalid address: ('мойайпишник', 8000)
[2017-10-28 11:54:40 +0000] [31426] [ERROR] Retrying in 1 second.
[2017-10-28 11:54:41 +0000] [31426] [ERROR] Invalid address: ('мойайпишник', 8000)
[2017-10-28 11:54:41 +0000] [31426] [ERROR] Retrying in 1 second.
[2017-10-28 11:54:42 +0000] [31426] [ERROR] Invalid address: ('мойайпишник', 8000)
[2017-10-28 11:54:42 +0000] [31426] [ERROR] Retrying in 1 second.
[2017-10-28 11:54:43 +0000] [31426] [ERROR] Invalid address: ('мойайпишник', 8000)
[2017-10-28 11:54:43 +0000] [31426] [ERROR] Retrying in 1 second.
[2017-10-28 11:54:44 +0000] [31426] [ERROR] Can't connect to ('мойайпишник', 8000)

Этот ваш IP адрес случайно не внешний IP адрес роутера, за которым Вы сидите со своим ПК?


перепробывал все ip, которые нашёл у себя на компе. не подскажите, как найти нужный?

А Вы делали вообще bind на ''?

то есть
bind = ''
gunicorn слушает порт на внутренней петле
sudo pip install django gunicorn

sudo pip install django psycopg2

По моему sudo здесь лишнее. Разве sudo устанавливает внутри виртуального окружения?

Как ни странно - Да.

Некропост, однако, хотел бы добавить, что в случае с продакшеном, миграции там делать нельзя. (точнее, конечно, можно, но нельзя)

Все миграции необходимо хранить в репозитории.

Твоя правда. Согласен. Свои миграции храню в репозитории. На продакшене только выполняю обновление структуры базы данных, после тестирования на дев сервере конечно (читай локальная машина разработки).


Здравствуйте, подскажите пожалуйста, как исправить:
в Ubuntu 20.04, в каталоге проекта пытаюсь создать вертуальную среду
virtualenv env
вылезает ошибка:
ModuleNotFoundError: No module named 'virtualenv.seed.via_app_data'

Это может быть и ошибка в вашей версии virtualenv и не совсем верная установка, и косяки в процессе следования туториалу.
Но подобные ошибки вылезали на GitHub в репозитории virtualenv.
Впрочем я сам с таким не сталкивался, так что готового ответа у меня нет.

Установите venv:

sudo apt install python3-venv

после этого можно сделать

python -m venv .venv
source .venv/bin/activate

virtualenv - устаревший, venv - часть стандартной библиотеки. в Windows - venv ставится вместе с питоном, в убунте, к сожалению, даже куски стандартной библиотеки приходится доставлять руками.


Ок, спасибо, буду пробовать.


Все заработало, немного ругнулся, что лучше:
python3 -m venv .venv
но в итоге все сработало.


Only authorized users can post comments.
Please, Log in or Sign up
How to become an author?

Contribute to the evolution of the EVILEG community.

Learn how to become a site author.

Learn it

Good day, Dear Users!!!

I am Evgenii Legotckoi, developer of EVILEG. And it is my hobby project, which helps to learn programming another programmers and developers

If the site helped you, and you want also support the development of the site, than you can donate by following ways


Let me recommend you the excellent hosting on which EVILEG is located.

For many years, Timeweb has been proving his stability.

For projects on Django I recommend VDS hosting

View Hosting

C++ - Test 005. Structures and Classes

  • Result:66points,
  • Rating points-1

C++ - Тест 003. Условия и циклы

  • Result:64points,
  • Rating points-1

C++ - Test 005. Structures and Classes

  • Result:83points,
  • Rating points4
Last comments

Qt WinAPI - Lesson 001. How to collect all DLL, which used in Qt project?

Не думал, что так оперативно ответите. Спасибо большое, буду ждать! =)

Qt WinAPI - Lesson 001. How to collect all DLL, which used in Qt project?

Да помнится так были проблемы при сборке MinGW я вроде исправил но на гит не закомитил. Если завтра будет время займусь.

Qt WinAPI - Lesson 001. How to collect all DLL, which used in Qt project?

Добрый день. Вы писали, что под х64 нужно пересобрать проект, при сборке появляются ошибки типа C:\SRC\LibraryCollector-lc_js\LibraryCollector-lc_js\wrappers\dir.cpp:149: ошибка: invalid co…

Компьютерная геометрия с Qt Creator

Вы очень сильно заблуждаетесь. Вот формула для расчёта отражённого вектора. Расчёт углов через косинусы и синусы не требуется от слова совсем . Подробнее обсуждение в этом…

Компьютерная геометрия с Qt Creator

Скалярная математика и тригонометрия - суть одно и тоже. Просто разные языки описания одного предмета. В конечном итоге для расчёта коэффициента отражения нужно знать косинус угла падения. …
Now discuss on the forum

Счечик производительности сети

Хорошо. После работы сегодня гляну ваш код внимательно.

QWebEngineView не запускается если к ПК подключено несколько мониторов

Добрый день. Попробуйте запустить приложение на другом ПК с другой графической картой. Может быть баг в OpenGL. Если запустится, то искать способ правильно инициализировать OpenGL конт…

Qt C++ и Python

Если вы хотите использовать Qt и Python. То используйте PyQt5 (неофициальная библиотека привязок из Python к Qt) или PySide2 (официально поддерживаемая бибилотека привязок из Python к Qt). …

Как в Qt в qmenu добавить scrollarea

Вот это наследованный класс меню. Но посути это обычное меню. #pragma once#include <QtWidgets>class TransMenu : public QMenu { Q_OBJECTpublic: TransMenu(QWidget* parent = …

OpenCV Android

нет, Вы берете исходники и под свою систему собираете библиотеки OpenCV(в сети полно инструкций), а потом уже эти библиотеки подключаете к совему Android проекту
© EVILEG 2015-2020
Recommend hosting TIMEWEB