How to setup and deployed Python Application using Nginx and Gunicorn

Posted By :Nitin Kumar Kashyap |14th May 2021

 

 

Prerequisites :

1- Create folders for the deployment

2- Python 3.6

3- Postgres database

4- Nginx

5- Gunicorn


 

Task 1: Create a folder for the project setup inside an opt directory.

 

Step 1: Create a workstation folder that contains all the folders and other data of the project-

Command:

sudo mkdir workstation

 

Step 2: Now, create a folder code inside a workstation folder.

Command:

sudo mkdir code

 

Step 3: Clone the code from GitHub to the production branch of the repository.

Command:

sudo git clone --branch production https://github.com/project URL

 

 

Task 2: Install Python3.6 and also create a python environment for Python 3.6.

 

Step 1: First, update and upgrade the system.

Command:

sudo apt get update

sudo apt get upgrade

 

Note: It will ask you to continue pressing Y or type, Yes to continue the upgrade.

 

Step 2: Install the prerequisites of Python 3.6 to run the following commands.

Commands:

sudo apt-get install build-essential checkinstall

sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev

libgdbm-dev libc6-dev libbz2-dev

 

Step 3: Download the Python 3.6 packages in the following location.

Commands:

cd /usr/src

sudo wget

https://www.python.org/ftp/python/3.6.10/Python-3.6.10.tgz

 

Step 4: Extract the downloaded package of python 3.6.

Command:

sudo tar xzf Python-3.6.10.tgz

 

Step 5: After extracting, enter the extracted folder and add these commands

Commands:

cd Python-3.6.10

sudo ./configure --enable-optimizations

sudo make alt install

 

Note: Use the 'make alt install' command to prevent replacing the default python binary

file/usr/bin/python.

 

Step 6: Now, type the following command to check the python3 version.

Command:

python3.6 -V

 

Step 7: After installing Python 3.6, upgrade the pip version with the following command.

Command:

sudo -H pip3 install --upgrade pip

 

Step 8: Create and Install a python environment with the env inside a workstation folder.

Commands:

cd /opt/workstation

sudo -H pip install virtualenv

python3.6 -m venv env

 

Task 3: Installing Postgres database

 

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

Commands:

sudo apt update

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

 

Step 2: Now, enter the Postgres database.

Commands:

sudo -u postgres psql

 

Step 3: We need to create a database with the name of btredb.

Command:

CREATE DATABASE btredb;

 

Step 4: After making the changes, apply the root and Postgres password with the following command.

Commands:

\password postgres

\password

 

Step 5:  Now, add some permissions of the database user Postgres.

Command:

ALTER ROLE postgres SET client_encoding TO 'utf8';

ALTER ROLE postgres SET default_transaction_isolation TO 'read committed';

ALTER ROLE postgres SET timezone TO 'UTC';

 

Step 6: Now grant all the privileges from the btredb to the Postgres user.

Command:

GRANT ALL PRIVILEGES ON DATABASE db-name TO postgres;

\q


 

Task 4: Installing gunicorn and their configuration.

 

Step 1: Install the gunicorn with the use of the following commands.

Command:

pip install django gunicorn psycopg2-binary

 

Step 2: Activate the python environment.

Commands:

cd /opt/workstation/

. env/bin/activate

 

Step 3: Migrate the database to the btredb and collect the static files using the following commands.

Commands:

cd /opt/workstation/code/Project/

sudo python3 manage.py makemigrations

sudo python3 manage.py migrate

sudo python3 manage.py collectstatic

 

Step 4: Run a test after installing the gunicorn.

Command:

sudo gunicorn --bind 0.0.0.0:8000 Project.wsgi:application

 

Note: run it whether your code exists.

 

Step 5: Now we need to create a gunicorn socket file.

Command:

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

[Paste the contents inside a file.]

[Unit]

Description=gunicorn socket

[Socket]ListenStream=/run/gunicorn.sock

[Install]

WantedBy=sockets.target

 

Step 6: In this step, we'll create a service file.

Command:

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

[Paste the contents inside a file.]

[Unit]

Description=gunicorn daemon

Requires=gunicorn.socket

After=network.target

[Service]

User=ubuntu

Group=www-data

WorkingDirectory=/opt/workstation/code/project_location

#ExecStart=/opt/workstation/env/bin/gunicorn \

ExecStart=/usr/local/bin/gunicorn \(Gunicorn location)

--access-logfile - \

--workers 3 \

--bind unix:/run/gunicorn.sock \

btre.wsgi:application

[Install]

WantedBy=multi-user.target

 

Step 7: Now, enable the socket configuration.

Command:

sudo systemctl start gunicorn.socket

sudo systemctl enable gunicorn.socket

 

Step 8: Check the status of the gunicorn socket.

Command:

sudo systemctl status gunicorn.socket

 

Step 9: Try running the socket file using the following command.

Command:

file /run/gunicorn.sock

 

Step 10: Now, we need to check the gunicorn service file.

Command:

sudo systemctl status gunicorn

 

Step 11: Curl the localhost with a port number to confirm if the gunicorn service is running.

Command:

curl --unix-socket /run/gunicorn.sock localhost

 

Step 12: At the end, enable and restart the daemon & gunicorn services.

Commands:

sudo systemctl daemon-reload

sudo systemctl restart gunicorn


 

Task 5: Install and configure the nginx conf file.

 

Step 1: Install the nginx server.

Command:

sudo apt install nginx

 

Step 2: Create and configure the conf file.

Command:

cd /etc/nginx/sites-available

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

[Paste the content in the file]

server {

listen 80;

server_name localhost;

location = /favicon.ico { access_log off; log_not_found off; }

location /static/ {

root /opt/workstation/code/project;}(Project path)

location / {

include proxy_params;

proxy_pass http://unix:/run/gunicorn.sock;

}

}

 

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

Command:

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

 

Step 4: Test the nginx configuration and that restart nginx.

Command:

sudo nginx -t

sudo systemctl restart nginx

 

Conclusion:

At the end of the blog, we've learned how to set up and deploy python-based applications with the simple use of the Nginx server and the gunicorn services.

 


About Author

Nitin Kumar Kashyap

Nitin is DevOps engineer. He isfamiliar with AWS and Azure Cloud. He also have an hands on work over docker and orchestrations tools.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us