How to Install and Deploy python django application using Nginx and Postgres in Ubuntu

Prerequisites :

1- Postgres database
2- Nginx
3- Gunicorn

Step 1: After installed Python 3.6 and upgraded the pip version with the following command.

sudo -H pip3 install –upgrade pip

 

Step 2: Install and create a python environment with the name of env inside a workstation folder.

cd /opt/workstation
sudo -H pip install virtualenv
python3.6 -m venv env

 

Task 1  : Install Postgres database, configure and create a database.

Step 1: Use below command to install the postgres database.

sudo apt update
sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl

 

Step 2: Now login to postgres database.

sudo -u postgres psql

 

Step 3:  Create a database with the name of db-name.

CREATE DATABASE db-name;

 

Step 4: After that change the root and postgres password with the use of the following command.

\password postgres
\password

Step 5: Now change some permissions of the database user postgres user.

ALTER ROLE username SET client_encoding TO ‘utf8’;
ALTER ROLE username SET default_transaction_isolation TO ‘read committed’;
ALTER ROLE username SET timezone TO ‘UTC’;

Step 6: Grant all the privileges to the db to the postgres user.

 

GRANT ALL PRIVILEGES ON DATABASE db TO username;

\q (command to exit from postgres

 

Task 2: Install gunicorn and configuration.
Step 1: Install gunicorn with below command.

pip install django gunicorn psycopg2-binary

 

Step 2: Activate python environment.

cd </path of the environment> . env/bin/activate

 

Step 3: Migrate the database to the database and collect the static files with below commands.

cd /opt/workstation/code/btre_project/
sudo python3 manage.py makemigrations
sudo python3 manage.py migrate
sudo python3 manage.py collectstatic

 

Step 4: After installed the gunicorn needs to test that.

sudo gunicorn –bind 0.0.0.0:8000 btre.wsgi:application
Note: run it whether your code exists.
Step 5: Now create a gunicorn socket file.

sudo nano /etc/systemd/system/gunicorn.socket
[Copy and Paste the contents in socket file]
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock[Install]
WantedBy=sockets.target
Step 6: Create a service file.

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

[Copy andPaste the contents in service file]

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

[Service]
User=ubuntu(username)
Group=www-data
WorkingDirectory=/home/(path of the code content directory)
ExecStart=/usr/local/bin/gunicorn(Path of the gunicorn) \
–access-logfile – \
–workers 3 \
–bind unix:/run/gunicorn.sock \
btre.wsgi:application

[Install]
WantedBy=multi-user.target
Step 7: Now enable the socket configuration.

sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket

 

Step 8: Check the status of the gunicorn.socket

sudo systemctl status gunicorn.socket

 

Step 9: Then run the socket file.

file /run/gunicorn.sock
Step 10: Now check gunicorn service.

sudo systemctl status gunicorn
Step 11: Curl the localhost with a port number to check if the gunicorn service is running or not.

curl –unix-socket /run/gunicorn.sock localhost
Step 12:  Enable and restart the daemon & gunicorn services.

sudo systemctl daemon-reload
sudo systemctl restart gunicorn

 

Task 3: Now install and configure the nginx conf file.

Step 1: Install the nginx server.

sudo apt install nginx
Step 2: Create and configure the conf file.

cd /etc/nginx/sites-available
sudo nano /etc/nginx/sites-available/btre

[Copy and Paste the content in file]

server {
listen 80;
server_name localhost;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /opt/workstation/code/btre_project;
}location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}

Step 3: Now create a soft link of the configure file.

sudo ln -s /etc/nginx/sites-available/file-name /etc/nginx/sites-enabled
Step 4: Test the nginx configuration if it is correct and restart nginx.

sudo nginx -t
sudo systemctl restart nginx

 

Now application will work on localhost or ip of the server and can test it on browser.

Leave a Reply