- Nikhil Bhaskar
- April 29, 2021
How to deploy Python framework application using uwsgi in Ubuntu 18.04
Introduction
In this blog we learn to deploy a python based frameworks deployment on ubuntu 18.04 server and how to setup nginx and uwsgi application on the server. We will launch the application and configure the front-end of nginx server and back-end for the uwsgi.
Prerequisites
Lets started the blog you have to setup some pre-installation:
- Login to the server and user will be non-root user with sudo privileges
- Step 1 is installed and configure nginx on ubuntu server.
- In next step we want a domain because uwsgi is required a domain name for running the application.
Step 1: Install the components and setup the environment for the project:
In this step you have to setup the git repository. Then we will have to install Python3 pip, package to setup the python package manager. After that start python development files necessary to build uWSGI.
First local package update, index and install the packages that will allow to build our python environment.
Commands:
sudo apt update
sudo apt install python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools
python3 -m pip install –user –upgrade pip
Step 2: After that install and create python environment of the python according to the version of the python. Now we are using python 3.
Commands:
sudo apt install python3-venv
python3.6 -m venv env
Note: You can change your environment name.
Step 3: Create a folder for the project and inside it create an environment and then activate the python environment.
Commands:
source env/bin/activate
Step 4: Now setup the environment and install uwsgi of the application.
Commands:
pip install wheel
pip install uwsgi
sudo -H pip3 install uwsgi
sudo apt install uwsgi-plugin-python
sudo apt-get install libpcre3 libpcre3-dev
Step 5: After the installation of the uwsgi test it.
Commands:
uwsgi –http :8000 –home /home/workstation/env(path of environment) –chdir /home//workstation/app-source_code/(path of python code) -w source_code.wsgi(name of uwsgi application)
Step 6: Setup the files and configuration of the uwsgi.
Create a directory for uwsgi configuration file.
Commands:
sudo mkdir -p /etc/uwsgi/sites
sudo mkdir -p /var/log/uwsgi-logs
sudo nano /etc/uwsgi/sites/source-code.ini
code of the project-name .ini file
[uwsgi]
uid = ubuntu
gid = ubuntu
chdir = /home/workstation/app-source_code/
home = /home//workstation/env
module = source-code.wsgi:application
#env = DJANGO_SETTINGS_MODULE=movies.settings
master = true
processes = 5
socket = /run/uwsgi/project-name.sock
logto = /var/log/uwsgi-logs/uwsgi.log
chown-socket = ubuntu:www-data
chmod-socket = 660
vacuum = true
Step 7: createa system file of the uwsgi.
Commands:
sudo nano /etc/systemd/system/uwsgi.service
Copy the code inside the file
[Unit]
Description=uWSGI Emperor service
[Service]
ExecStartPre=/bin/bash -c ‘mkdir -p /run/uwsgi; chown ubuntu:www-data /run/uwsgi’
ExecStart=/usr/local/bin/uwsgi –emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
Step 8: After that check the uwsgi is running fine or not.
Command:
sudo systemctl status uwsgi.service
Step 9: Now install the nginx server and configure the config files of the nginx for the uwsgi application.
Commands:
sudo apt update
sudo apt upgrade
sudo apt install nginx
Step 10: After that go to the path and create a config file of the uwsgi application.
Commands:
cd /etc/nginx/sites-available
nano uwsgi-application
Copy the code to the file
server {
server_name (domain-name).com www.(domain name).com;
#server_name localhost;
location = /favicon.ico { access_log off; log_not_found off; }
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/(uwsgi name).sock;
}
location /static {
root /home/workstation/app-source_code/;
}
}
Step 11: Create a soft-link of the config files of nginx serve the uwsgi application.
Commands:
sudo ln -s /etc/nginx/sites-available/uwsgi-application /etc/nginx/sites-enabled
Step 12: After that complete the configuration of the nginx configurations test the changes correct or not.
Command:
sudo nginx -t
Step 13: Then reload or restart the nginx server.
Command:
sudo systemctl restart nginx
Step 14: Then daemon and uwsgi restart.
Command:
sudo systemctl daemon-reload
sudo systemctl restart uwsgi
Step 15: To enable both of the services to start automatically at boot.
Commands:
sudo systemctl enable nginx
sudo systemctl enable uwsgi
Conclusion: Deployment Complete. You can check the url of uwsgi application servers and it will be accessible on web browsers.