BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Serving Django app with uWSGI and Nginx

django.png




Bookmark and Share





bogotobogo.com site search:




WSGI

This article will show how we can serve a Django app with uWSGI and Nginx.

We're going to configure the uWSGI application container server to interface with our applications by setting up Nginx to reverse proxy to uWSGI.

Nginx provides a HTTP connection management, load balancing, content caching, and traffic security, etc.

uWSGI is an application server container and communicates with the application using the methods defined by the WSGI spec, and with other web servers over a variety of other protocols. The uWSGI translates requests from a conventional web server into a format that the application can process.

In other words, the WSGI spec defines the interface between the web server (uWSGI server) and application. The uWSGI server is responsible for passing client requests to the application using the WSGI spec.

the web client <-> the web server <-> the socket <-> uwsgi <-> Django

"uWSGI operates on a client-server model. Your Web server (e.g., nginx, Apache) communicates with a django-uwsgi "worker" process to serve dynamic content." - How to use Django with uWSGI.






Set up a virtualenv and WSGI for an App

In this section, we will setup a virtualenv for our sample app.

$ mkdir ~/sample-app/
$ cd sample-app

$ virtualenv venv
$ source venv/bin/activate
(venv)k@laptop:~/sample-app$ 

install the uWSGI server into our environment using pip:

(venv)k@laptop:~/sample-app$ pip install uwsgi

WSGI is an interface between a web server and the application. It ensures a standardized way between various servers and application frameworks.

A wsgi.py provides an application object which is callable to be used by the server.





Create a WSGI Application

Let's create an application in a file called wsgi.py in our application directory:

def application(environ, response):
    response('200 OK', [('Content-Type', 'text/html')])
    return ["Hello WSGI Python App!"]

The uWSGI will look for a callable called application which takes two parameters.

The first is an environmental variable-like key-value dictionary. The second is the name the app will use internally to refer to the web server (uWSGI) callable that is sent in.

Our application takea this information and do the following:

  1. It calls the callable it received with an HTTP status code and any headers it wants to send back. In this case, we are sending a "200 OK" response and setting the Content-Type header to text/html.
  2. It returns an iterable to use as the response body. Here, we've just used a list containing a single string of HTML. Strings are iterable as well, but inside of a list, uWSGI will be able to process the entire string with one iteration.

In Django projects include a wsgi.py file by default that translates requests from the web server (uWSGI) to the application (Django). The simplified WSGI interface stays the same regardless of how complex the actual application code is. This is one of the strengths of the interface.

Let's start up uWSGI which will tell it to use HTTP for the time being and to listen on port 8888. We will pass it the name of the script (suffix removed):

(venv)k@laptop:~/sample-app$ uwsgi --socket 0.0.0.0:8888 --protocol=http -w wsgi

WSGI-Python-8888.png



uWSGI Config File : sample-app.ini

The uWSGI Config File (~/sample-app/sample-app.ini) looks like this:

[uwsgi]
module = wsgi:application

master = true
processes = 5

socket = sample-app.sock
chmod-socket = 664
vacuum = true

die-on-term = true

We'll use the init file with an Upstart script that we'll see in the next section.





Managing app - Upstart file

We can launch a uWSGI instance at boot so that our application is always available (/etc/init/sample-app.conf):

description "uWSGI instance to serve sample-app"

start on runlevel [2345]
stop on runlevel [!2345]

setuid k
setgid www-data

script
    cd /home/k/sample-app
    . venv/bin/activate
    uwsgi --ini sample-app.ini
end script

We start out with the system runlevels of 2 through 5 and stop the service when it's on any runlevel outside of the level.

The configuration file tells Upstart about which user (k) and group (www-data) to run the process as.

Next, we'll run the actual commands to start uWSGI via a script block.

Now that our Upstart script is complete, we can start the service:

(venv)k@laptop:~/sample-app$ sudo start sample-app
sample-app start/running, process 7228

(venv)k@laptop:~/sample-app$ ps aux | grep sample-app
k         7232  0.5  0.1  50164  7008 ?        S    15:06   0:00 uwsgi --ini sample-app.ini
k         7233  0.0  0.1  53028  5304 ?        S    15:06   0:00 uwsgi --ini sample-app.ini
k         7234  0.0  0.1  53028  5304 ?        S    15:06   0:00 uwsgi --ini sample-app.ini
k         7235  0.0  0.1  53028  5304 ?        S    15:06   0:00 uwsgi --ini sample-app.ini
k         7236  0.0  0.1  53028  5304 ?        S    15:06   0:00 uwsgi --ini sample-app.ini
k         7237  0.0  0.1  53028  5304 ?        S    15:06   0:00 uwsgi --ini sample-app.ini

We can also see :

(venv)k@laptop:~/sample-app$ ls -la
...
-rw-rw-r--   1 k k          142 May 21 16:37 sample-app.ini
srw-rw-r--   1 k www-data     0 May 21 19:06 sample-app.sock
drwxrwxr-x   6 k k         4096 May 21 12:49 venv
-rw-rw-r--   1 k k          142 May 21 12:37 wsgi.py

tree.png

The app will start automatically on boot. We can stop the service at any time by typing:

(venv)k@laptop:~/sample-app$ sudo stop sample-app




Serving an app with Nginx as a reverse proxy

Now we have a WSGI app and have verified that uWSGI can read and serve it.

Also, we have a configuration file and an Upstart script.

Our uWSGI process will listen on a socket and communicate using the uwsgi protocol.

Note that we can use Nginx as a proxy via uwsgi protocol for communicating with uWSGI which is a faster protocol than HTTP and have better performance.

Let's create a new file /etc/nginx/sites-available/sample-app:

server {
    listen 80;
    server_name sample-app.com;

    location / {
        include         uwsgi_params;
        uwsgi_pass      unix:/home/k/sample-app/sample-app.sock;
    }
}

Enable the server configuration we just made by linking it to the sites-enabled directory:

$ sudo ln -s /etc/nginx/sites-available/sample-app /etc/nginx/sites-enabled

Check the configuration file for syntax errors:

$ sudo service nginx configtest
 * Testing nginx configuration

If it reports back that no problems were detected, restart the server to implement new changes:

$ sudo service nginx restart

Once Nginx restarts, we should be able to go to our server's domain name or IP address (without a port number) and see the application we configured:


sample-app-com.png



Real world samples

Here are two samples:

  1. Local dev with Apache mod_wsgi
  2. Local dev with Nginx and uWSGI








Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Django 1.8



Introduction - Install Django and Project Setup

Creating and Activating Models

Hello World A - urls & views

Hello World B - templates

Hello World C - url dispatcher

Hello World D - Models and SQLite Database

MVC - Hello World

Hello World on a Shared Host A

Hello World on a Shared Host B

Hello World - Admin Site Setup

virtualenv

Creating test project on virtualenv

Test project's settings.py

Creating Blog app and setting up models

Blog app - syncdb A

Blog app - syncdb B

Blog app - views and urls

Blog app - templates

Blog app - class based templates

Image upload sample code - local host

Authentication on Shared Host using FastCGI

User Registration on Shared Host A

User Registration with a Customized Form on Shared Host B

Blogs on Shared Host

Serving Django app with uWSGI and Nginx

Image upload sample code - shared host

Managing (Deploying) Static files (CSS, Images, Javascript) on Shared Host

Forum application on a Shared Host

Django Python Social Auth : Getting App ID (OAuth2) - Facebook, Twitter, and Google

Django: Python social auth, Facebook, Twitter, and Google Auth

Django: Python social auth, Facebook, Twitter, and Google Auth with Static files

...

Django 1.8 hosted on Linode VPS ==>

1. Setup CentOS 7 hosted on VPS

1B. Setup CentOS 7 hosted on VPS (multi-domain hosting setup) - Name server and Zone File settings (from GoDaddy to Linode)

2. ssh login and firewall

3. Apache Install

4. Install and Configure MariaDB Database server & PHP

5. Install and Configure Django

6. Model

7. Model 2 : populate tables, list_display, and search_fields

8. Model 3 (using shell)

9. Views (templates and css)

10. Views 2 (home page and more templates)

11. TinyMCE

12. TinyMCE 2

13. ImageField/FileField : Serving image/video files uploaded by a user

14. User Authentication 1 (register & forms)

15. User Authentication 2 (login / logout)

16. User Authentication 3 (password reset) - Sent from Email (gmail) setup etc.

17. User Authentication 4 (User profile & @login_required decorator)

18. User Authentication 5 (Facebook login)

19. User Authentication 6 (Google login)

20. User Authentication 7 (Twitter login)

21. User Authentication 8 (Facebook/Google/Twitter login buttons)

22. Facebook open graph API timeline fan page custom tab 1

23. Facebook Open Graph API Timeline Fan Page Custom Tab 2 (SSL certificate setup)

24. Facebook open graph API timeline fan page custom tab 3 (Django side - urls.py, settings.py, and views.py)

...

A sample production site Django 1.8.7: sfvue.com / einsteinish.com ==>

A sample production app (sfvue.com) with virtualenv and Apache

2. Upgrading to Django 1.8.7 sfvue.com site sample with virtualenv and Apache

(*) Django 1.8.7 einsteinish.com site - errors and fixes

Django 1.8.12 pytune.com site - local with Apache mod_wsgi

Django 1.8.12 pytune.com site - local with Nginx and uWSGI

Django 1.8.12 pytune.com site - deploy to AWS with Nginx and uWSGI

Django Haystack with Elasticsearch and Postgres

Django Compatibility Cheat Sheet

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Python tutorial



Python Home

Introduction

Running Python Programs (os, sys, import)

Modules and IDLE (Import, Reload, exec)

Object Types - Numbers, Strings, and None

Strings - Escape Sequence, Raw String, and Slicing

Strings - Methods

Formatting Strings - expressions and method calls

Files and os.path

Traversing directories recursively

Subprocess Module

Regular Expressions with Python

Regular Expressions Cheat Sheet

Object Types - Lists

Object Types - Dictionaries and Tuples

Functions def, *args, **kargs

Functions lambda

Built-in Functions

map, filter, and reduce

Decorators

List Comprehension

Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarism

Hashing (Hash tables and hashlib)

Dictionary Comprehension with zip

The yield keyword

Generator Functions and Expressions

generator.send() method

Iterators

Classes and Instances (__init__, __call__, etc.)

if__name__ == '__main__'

argparse

Exceptions

@static method vs class method

Private attributes and private methods

bits, bytes, bitstring, and constBitStream

json.dump(s) and json.load(s)

Python Object Serialization - pickle and json

Python Object Serialization - yaml and json

Priority queue and heap queue data structure

Graph data structure

Dijkstra's shortest path algorithm

Prim's spanning tree algorithm

Closure

Functional programming in Python

Remote running a local file using ssh

SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table

SQLite 3 - B. Selecting, updating and deleting data

MongoDB with PyMongo I - Installing MongoDB ...

Python HTTP Web Services - urllib, httplib2

Web scraping with Selenium for checking domain availability

REST API : Http Requests for Humans with Flask

Blog app with Tornado

Multithreading ...

Python Network Programming I - Basic Server / Client : A Basics

Python Network Programming I - Basic Server / Client : B File Transfer

Python Network Programming II - Chat Server / Client

Python Network Programming III - Echo Server using socketserver network framework

Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn

Python Coding Questions I

Python Coding Questions II

Python Coding Questions III

Python Coding Questions IV

Python Coding Questions V

Python Coding Questions VI

Python Coding Questions VII

Python Coding Questions VIII

Python Coding Questions IX

Python Coding Questions X

Image processing with Python image library Pillow

Python and C++ with SIP

PyDev with Eclipse

Matplotlib

Redis with Python

NumPy array basics A

NumPy Matrix and Linear Algebra

Pandas with NumPy and Matplotlib

Celluar Automata

Batch gradient descent algorithm

Longest Common Substring Algorithm

Python Unit Test - TDD using unittest.TestCase class

Simple tool - Google page ranking by keywords

Google App Hello World

Google App webapp2 and WSGI

Uploading Google App Hello World

Python 2 vs Python 3

virtualenv and virtualenvwrapper

Uploading a big file to AWS S3 using boto module

Scheduled stopping and starting an AWS instance

Cloudera CDH5 - Scheduled stopping and starting services

Removing Cloud Files - Rackspace API with curl and subprocess

Checking if a process is running/hanging and stop/run a scheduled task on Windows

Apache Spark 1.3 with PySpark (Spark Python API) Shell

Apache Spark 1.2 Streaming

bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...

Flask app with Apache WSGI on Ubuntu14/CentOS7 ...

Selenium WebDriver

Fabric - streamlining the use of SSH for application deployment

Ansible Quick Preview - Setting up web servers with Nginx, configure enviroments, and deploy an App

Neural Networks with backpropagation for XOR using one hidden layer

NLP - NLTK (Natural Language Toolkit) ...

RabbitMQ(Message broker server) and Celery(Task queue) ...

OpenCV3 and Matplotlib ...

Simple tool - Concatenating slides using FFmpeg ...

iPython - Signal Processing with NumPy

iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github

iPython and Jupyter Notebook with Embedded D3.js

Downloading YouTube videos using youtube-dl embedded with Python

Machine Learning : scikit-learn ...

Django 1.6/1.8 Web Framework ...









Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master