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

Ubuntu, Gunicorn, Django, PostgreSQL, 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 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/

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 makemigrations
python 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 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 file.

Edit it:

nano ~/myproject/

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 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

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/

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 устанавливает внутри виртуального окружения?

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

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

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

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


Only authorized users can post comments.
Please, Log in or Sign up

Hello, Dear Users of EVILEG!!!

If the site helped you, then support the development of the site financially, please.

You can do it by following ways:

Thank you, Evgenii Legotckoi

Feb. 17, 2020, 2:41 p.m.

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

  • Result:85points,
  • Rating points6
Feb. 17, 2020, 6:02 a.m.

C++ - Test 006. Enumerations

  • Result:80points,
  • Rating points4
Feb. 17, 2020, 5:49 a.m.

C++ - Test 001. The first program and data types

  • Result:80points,
  • Rating points4
Last comments
Feb. 17, 2020, 3:22 a.m.
Evgenij Legotskoj

Добрый день. Это кастомный тег, помещается в файл, который находится в каталоге templatetags myapp/ templatetags/
Feb. 16, 2020, 1:36 p.m.

Добрый вечер! Монжно по подробней о теге get_companion? ссылка не работает.
Feb. 16, 2020, 9:35 a.m.
Evgenij Legotskoj

Добрый день. На GitHub исходники, можете посмотреть в официальном репозитории
Feb. 16, 2020, 9:29 a.m.

Здравстсвуйте Евгений, непонятно мне где эти исходники найти?
Now discuss on the forum
Feb. 20, 2020, 1:24 a.m.
Igor' Poroshin

QSqlQuery query("SELECT CONCAT("#", LPAD(HEX(`t`.RGB), 6, 0)) FROM table AS t");query.exec(); while ( { QColor(query.value(0).toString()); }
Feb. 19, 2020, 11:55 p.m.
Igor' Poroshin

qDebug() << model->lastError().text() - возвращает текст и код ошибки, который возвращает база данных, чаще всего ошибка бывает из-за неправильного SQL запроса qDebug() << mo…
Feb. 19, 2020, 8:55 a.m.

Можно через сервер сделать
Feb. 19, 2020, 7:09 a.m.

Да, прямо сходу не заработало, а сейчас просто некогда разбираться, да и я уже решил не использовать в этом приложении WebEngine. Ошибка: WebEngineContext used before QtWebEngine::initialize(…
Feb. 19, 2020, 7:01 a.m.

Просто реально не вижу тут каких то проблем в модели, вот вообще ничего. Но она все равно не отображается, то есть ладно бы если данные бы не появлялись а сама таблица была бы. Так и таблиц…
© EVILEG 2015-2019
Recommend hosting TIMEWEB