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 DATABASE myproject;

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.

\q

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:

...

DATABASES = {
    '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:

...

DATABASES = {
    '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_pass http://127.0.0.1:8000; 
        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 = '127.0.0.1:8000'
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

[program:myproject]
command=/home/user/myprojectenv/bin/gunicorn myproject.wsgi:application -c /home/user/myprojectenv/myproject/myproject/gunicorn.conf.py
directory=/home/user/myprojectenv/myproject
user=nobody
autorestart=true
redirect_stderr=true

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

Notes

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
A

С чем может быть связана ошибка? ставил всё на свою машину, статический 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 адрес роутера, за которым Вы сидите со своим ПК?

A

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

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

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

sudo pip install django psycopg2





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

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

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

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

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

Comments

Only authorized users can post comments.
Please, Log in or Sign up
m
May 19, 2019, 1:49 a.m.
mahhaki

Qt - Test 001. Signals and slots

  • Result:78points,
  • Rating points2
S
May 17, 2019, 1:14 p.m.
SunBro

Qt - Test 001. Signals and slots

  • Result:42points,
  • Rating points-8
b
May 17, 2019, 4:18 a.m.
banana

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

  • Result:57points,
  • Rating points-2
Last comments
May 21, 2019, 8:10 p.m.
Дмитрий

Приветствую! Я думаю дойдёт и до этого, но пока изучать его у меня нет желания.
May 20, 2019, 7:20 p.m.
Евгений Легоцкой

Добрый день! Вы не думали разместить репозиторий проекта на GitHub?
P.
May 18, 2019, 2:03 p.m.
PELMYACH .

Спасибо большое! Вскоре буду разбираться!
May 18, 2019, 9:13 a.m.
Евгений Легоцкой

Добрый день! Отнимать значение общего счётчика можно в деструкторе класса кнопки QDynamicButton::~QDynamicButton(){ ResID--;} При этом я бы ещё переустанавливал значения вс...
P.
May 14, 2019, 10:33 p.m.
PELMYACH .

Здравствуйте!А не подскажите, как можно при удалении какой либо кнопки, у щётчика отнять значение?Дабы например четвёртой кнопке соответствовал ID 4, а не 5 скажем
Now discuss on the forum
May 24, 2019, 6:48 a.m.
Евгений Легоцкой

Если там будут только перечисления внутри namespace, то жа, достаточно будет заголовочного файла
May 24, 2019, 6:28 a.m.
Андрей Янкович

работает любой http сервер, и можно использовать обсалютно любой портпример <RemoteRepositories> <Repository> <Url>http://178.124.160.6:3030/A/B&l...;
May 23, 2019, 2:40 p.m.
Михаиллл

Попробовал сделать этот запрос по http и получил json файл. request.setUrl(QUrl("https://jsonplaceholder.typicode.com/todos/1")); Как Вы думаете, почему https не работает и как это и...
May 23, 2019, 10:42 a.m.
Михаиллл

Спасибо, помогло.
May 23, 2019, 6:31 a.m.
Евгений Легоцкой

Для задач и граф-то не нужен. Достаточно будет таблицы в локальной базе данных SQLite, в которой указывается задача, время и т.д. В этом разделе есть примеры по работа с базой д...

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

EVILEG
About
Services
Join us
© EVILEG 2015-2019
Recommend hosting TIMEWEB