Installing Django, Python, PostgreSQL and Nginx on a server

4 min


The interpreter installation process Python, framework Django, Database PostgreSQL and HTTP proxy NGINX on server Ubuntu 20.04

Initial data

The installation will be done on a remote server, but the same can be deployed on localhost… Replace 111.111.111.111 with your Internet IP address.

Freshly installed Ubuntu 20.04 LTS server.

We create a user in Linux.

Install Python on the server

Before any installation, we will update the operating system packages:

Install python:

sudo apt-get install python3-pip python3-dev

Let’s create a symlink for using pip instead of pip3:

ln -s /usr/bin/pip3 /usr/bin/pip
pip install –upgrade pip

PostgreSQL

Install PostgreSQL on the server:

sudo apt-get install postgresql postgresql-contrib libpq-dev

Next, install the package psycopg2which is python-postgreSQL adapter:

Create and configure a user and database in PostgreSQL.

Create a virtual environment and install Django

We go to the user’s folder. From here we will work further.

Create a virtual environment in the directory myproject:

mkdir myproject
virtualenv –python= python3 myproject/

Let’s go to this directory and activate the environment:

cd myproject/
source bin/activate

Now in this environment we will install Django and gunicorn:

pip install django gunicorn

Let’s start a new project with PostgreSQL

Let’s start the hello_django project with the following command:

django-admin startproject hello_django

This will create a new directory hello_django with python files.

Let’s edit the configuration file there setting.py:

nano hello_django/hello_django/settings.py

Let’s register our data.

Find the line and write your IP there:

ALLOWED_HOSTS = [[‘111.111.111.111’]

Next, we will enter the settings of our database:

DATABASES = {
‘default’: {
# ‘ENGINE’: ‘django.db.backends.sqlite3’,
# ‘NAME’: os.path.join (BASE_DIR, ‘db.sqlite3’),

‘ENGINE’: ‘django.db.backends.postgresql_psycopg2’,
‘NAME’: ‘ploshadka’,
‘USER’: ‘ploshadka’,
‘PASSWORD’: ‘password’,
‘HOST’: ‘localhost’,
‘PORT’: ,
}
}

and at the end where

add line:

# STATIC_ROOT = os.path.join (BASE_DIR, ‘static /’)
STATIC_ROOT = ‘/ home / ploshadka / static /’

Save the file and exit.

Let’s migrate data to PostgreSQL table

We must be in the directory with the file manage.py:

cd /home/ploshadka/myproject/hello_django

There we start the migration:

On successful migration:

Let’s create a super user. It will be used to log in to our website:

python manage.py createsuperuser

Next, select a name for the project user, email and password.

We collect our project into static files:

python manage.py collectstatic

Sometimes an error occurs:

PermissionError: [[Errno 13] Permission denied: ‘/ static’

In this case, you should exit, re-enter and repeat:

exit
cd /home/ploshadka/myproject/
source bin/activate
cd hello_django
python manage.py collectstatic

If successful, a folder will be created static and a message will appear:

130 static files copied to ‘/ home / ploshadka / myproject / hello_django / static’

Let’s open the port on which the project will be launched:

Let’s start our project:

python manage.py runserver 111.111.111.111:8080

Errors of the form:

Command ‘python’ not found, did you mean:
command ‘python3’ from deb python3
command ‘python’ from deb python-is-python3

May occur when you exit the work area. We go back and run:

su ploshadka
source bin/activate
cd hello_django/
python manage.py runserver 111.111.111.111:8080

Now you can go to the page:

http://111.111.111.111:8080/

Where will we see the pre-installed Django page:

To the admin panel:

http://111.111.111.111:8080/admin/

After authorization, we get to the admin panel:

Setting up Django to work with the Gunicorn HTTP server

Let’s go to our project to use gunicorn to load the WSGI module:

cd /home/ploshadka/myproject/hello_django
gunicorn -b 111.111.111.111:8080 hello_django.wsgi: application

Errors of the form:

[67658] [ERROR] Exception in worker process
Traceback (most recent call last):
File

lib / python3.8 / site-packages / gunicorn / util.py “, line 358, in import_app
mod = importlib.import_module (module)
File “/usr/lib/python3.8/importlib/__init__.py”, line 127, in import_module
return _bootstrap._gcd_import (name[level:], package, level)
File ““, Line 1014, in _gcd_import
File ““, Line 991, in _find_and_load
File ““, Line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named ‘hello_django.wsgi’
[2020-05-24 22:44:37 +0200] [67658] [INFO] Worker exiting (pid: 67658)
[2020-05-24 22:44:37 +0200] [67656] [INFO] Shutting down: Master
[2020-05-24 22:44:37 +0200] [67656] [INFO] Reason: Worker failed to boot.
(myproject) root @ nomerdoma1: / home / ploshadka / myproject #

Will occur if you try to link to the wrong directory. This should be the directory where the file is located settings.py

If launched successfully, the console will display:

[2020-05-24 22:50:00 +0200] [67705] [INFO] Starting gunicorn 20.0.4
[2020-05-24 22:50:00 +0200] [67705] [INFO] Listening at: http://111.111.111.111:8080 (67705)
[2020-05-24 22:50:00 +0200] [67705] [INFO] Using worker: sync
[2020-05-24 22:50:00 +0200] [67707] [INFO] Booting worker with pid: 67707

We can go back to the site and test.

Django setup is complete. Now you can exit the virtual environment:

Configuring systemd

Let’s set up systemd to automate starting and stopping the Django application.

To do this, let’s create and configure a systemd file for Gunicorn:

sudo nano /etc/systemd/system/gunicorn.service

Let’s insert:

[[Unit]
Description= gunicorn daemon
After= network.target

[[Service]
User= root
Group= www-data
WorkingDirectory=/home/ploshadka/myproject/hello_django
ExecStart=/home/ploshadka/myproject/bin/gunicorn –access-logfile–workers 3 –bind unix:/home/ploshadka/myproject/hello_django/myproject.sock hello_django.wsgi: application

[[Install]
WantedBy= multi-user.target

Save and exit. If gunicorn.service was already running and this is re-editing, then before the next command run:

systemctl daemon-reload
sudo systemctl restart gunicorn

Now we can start the Green Unicorn:

sudo systemctl start gunicorn
sudo systemctl enable gunicorn

Check the server status:

systemctl status gunicorn

If successful, the file will appear in the hello_django directory where manage.py is myproject.sock

Nginx

Install additional tools:

sudo apt-get install nginx curl

Let’s create and edit the file:

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

Let’s insert the following:

server {
listen 80;
server_name 111.111.111.111;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ploshadka;
}

location / {
include proxy_params;
proxy_pass http://unix:/home/ploshadka/myproject/hello_django/myproject.sock;
}
}

Please note that in proxy_pass you need to specify the path to the above sock file. Otherwise nginx will show 502 error.

We link this file to the site directory:

sudo ln -s /etc/nginx/sites-available/ploshadka /etc/nginx/sites-enabled

Let’s check the nginx configuration for errors:

If there are no errors:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If there are no errors:

sudo systemctl restart nginx

If we no longer need the development server, port 8000, then we will remove it in order to redirect all traffic to port 80:

sudo ufw delete allow 8000
sudo ufw allow ‘Nginx Full’

This completes the installation. Now our server is working offline. And at the address of our website we will see the same rocket taking off:

Finally

In order for the changes in the settings.py file to become visible, restart the “unicorn”:

sudo systemctl restart gunicorn


0 Comments

Leave a Reply