Privacy policyContactsAbout siteOpinionsGitHubDonate
© EVILEG 2015-2018
Recommend hosting

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 .

Virtual hosting with 10 percent discount
Virtual hosting with 10 percent discount
EVILEG offers reliable hosting with a 10% discount for virtual hosting and 5% for VPS

С чем может быть связана ошибка? ставил всё на свою машину, статический 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
March 20, 2019, 9:01 a.m.
Геннадий Костоянский

C++ - Test 002. Constants

  • Result:0points,
  • Rating points-10
March 20, 2019, 8:46 a.m.
Геннадий Костоянский

C++ - Test 002. Constants

  • Result:25points,
  • Rating points-10
Last comments
March 20, 2019, 3:43 p.m.
Maciej Urmański

It's possible to simply add vote option for non logged users?
March 20, 2019, 9:45 a.m.
Евгений Легоцкой

Добрый день. Поппробуйте домен localhost, а url соответственно http://localhost Возможно, потребуется указать порт. Например, так http://localhost:8000
March 19, 2019, 12:57 p.m.

Добрый день. А проверить работоспособность локально как-то можно не указывая реальнй сайт (еще в разработке)
March 16, 2019, 1:55 p.m.

Спасибо за статью. Давно итересует следующий вопрос: с помощью переменных QMAKE_TARGET_COMPANYQMAKE_TARGET_PRODUCTQMAKE_TARGET_DESCRIPTIONможно задать свойства компилируемой программы, о...
March 12, 2019, 10:19 a.m.
Jean Stefanovich

Большое спасибо за разъяснения!
Now discuss on the forum
March 20, 2019, 12:26 p.m.
Евгений Легоцкой

Лучше стараться избегать этого. Нормального механизма нет. Я просто выдёргиваю из бэкенда перевёденные куски шаблона, если нужно что-то задействовать в JS.
March 17, 2019, 10:47 p.m.
Евгений Легоцкой

Добрый день. Вот, нашлось у меня немного времени. Делается это через шаблон проектирования наблюдатель.
March 15, 2019, 9:52 p.m.
Чарльз Грин

спасибо, попробую, отпишусь
March 15, 2019, 7:41 p.m.

Сори догадался)
March 12, 2019, 4:57 p.m.

Большооооое спасибо!!!!!Не передать как я вам благодарен, спасибо что всегда отзываетесь.Теперь я смогу продолжить работу в QT!!! (пробую писать бота (Я как вы могли догадаться немного не пр...
Join us in social networks

For registered users on the site there is a minimum amount of advertising