WSGI
Before going to uWSGI, one must know about WSGI and it’s workflow.Web Server Gateway Interface is abbreviated as WSGI.WSGI simply is a specification that describes how a web server communicates web applications and how web applications can be chained together to process one request.Thus WSGI is a standard interface to route web apps and frameworks to web servers.
While going deep, WSGI mainly has two parts:
- High profile web servers like Nginx or Apache
- Web app made from a python script
The work flow is like a server executes the webapp and sends related information and callback to the app.On the application side, the request is processed and a response is setback to the server utilizing the callback function.
The pictorial representation of WSGI is depicted as follows:
Bjoern, uWSGI, mod_wsgi, Meinheld, CherryPy and Gunicorn are some of WSGI servers available on the market today.
uWSGI
uWSGI is used for developing full stack development of Applications,Hosting services. The main advantage of uWSGI is that Hosting services,application servers,process managers,monitors all can be implemented using a common api and a common configuration style.It uses pluggable architecture so that it can be extended to support more platforms and languages.Main benefits are Versatility, performance, low-resource usage and reliability.
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design of python django development
WSGI Vs uWSGI
WSGI was mainly built to integrate and work efficiently with Apache. Those who use Apache as their primary webserver, WSGI is the better option.
UWSGI on other hand is lightweight and runs separately from your webserver.This will not overload your webserver process. Compared to WSGI,uWSGI is easy to setup.The downside of WSGI is that it only works with Apache. uWSGI on the other hand works with web servers like apache,nginx,cherokee etc.
Create a directory with uWSGI configuration files:
sudo mkdir -p /etc/uwsgi/sites
Create configuration file sample.ini with the following contents:
+++++++++++++++++++++++++++++++++++++++
[uwsgi]
project = sample
base = /home/django
chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application
master = true
processes = 2
socket = %(base)/%(project)/%(project).sock
chmod-socket = 664
vacuum = true
+++++++++++++++++++++++++++++++++++++++++
Create an Upstart job for uWSGI:
+++++++++++++++++++++++++++++++++++++++++++
description “uWSGI”
start on runlevel [2345]
stop on runlevel [06]
respawn
env UWSGI=/usr/local/bin/uwsgi
env LOGTO=/var/log/uwsgi.log
exec $UWSGI –master –emperor /etc/uwsgi/sites –die-on-term –uid django –gid www-data –logto $LOGTO
+++++++++++++++++++++++++++++++++++++++++++
UWSGI as a systemctl service:
With the help of Emperor we can integrate uwsgi django App with our init system.Emperor is a special uWSGI Django instance that will monitor specific events and will spawn/stop/reload instances (known as vassals, when managed by an Emperor) on demand.Thus,Emperor will rule all the apps itself,so that in it system will talk only with the Emperor.
- Create a systemd service file (you can save it as /etc/systemd/system/emperor.uwsgi.service)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[Unit]
Description=uWSGI Emperor
After=syslog.target
[Service]
ExecStart=/root/uwsgi/uwsgi –ini /etc/uwsgi/emperor.ini
# Requires systemd version 211 or newer
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
After that we can run,check and stop the status using the following commands:
- Run command:
systemctl start emperor.uwsgi.service
- Checking status:
systemctl status emperor.uwsgi.service
- Stopping command:
systemctl stop emperor.uwsgi.service
Nginx and uWSGI
Nginx is one of the best choice for the Python app which makes your website faster,even though low levels of traffic.With thousands of users, it performs with higher grade,fewer crashes,less downtime.The main advantage is that you can do static file caching or microcaching on Nginx,this will work better when run on a separate Nginx reverse proxy server.
The working flow is depicted as follows:
Nginx can simply be implemented as a reverse proxy server in front of your uwsgi server. So the working be like this, Nginx faces the web and passes requests to application server.This will helps in making your website runs faster, reduces downtime,consumes less server resources and improves security.