Python uWSGI Installation and Configuration

This article mainly introduces how to deploy simple WSGI applications and common Web frameworks.

Taking Ubuntu/Debian as an example, first install the dependency packages:

apt-get install build-essential python-dev

Installing uWSGI with Python

1. Via the pip command:

pip install uwsgi

2. Download the installation script:

curl http://uwsgi.it/install | bash -s default /tmp/uwsgi

Install the uWSGI binary to /tmp/uwsgi, you can modify it.

3. Source code installation:

wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz
tar zxvf uwsgi-latest.tar.gz
cd uwsgi-latest
make

After the installation is complete, you will get a uwsgi binary file in the current directory.


The first WSGI application

Let's start with a simple "Hello World" by creating the file foobar.py with the following code:

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

The default function the uWSGI Python loader will search forapplication 。

Next, we start uWSGI to run an HTTP server, deploying the program on HTTP port 9090:

uwsgi --http :9090 --wsgi-file foobar.py

Adding Concurrency and Monitoring

By default, uWSGI starts a single process and a single thread.

You can use the--processesoption to add more processes, or use the--threadsoption to add more threads, or both at the same time.

uwsgi --http :9090 --wsgi-file foobar.py --master --processes 4 --threads 2

The above command will generate 4 processes, each with 2 threads.

If you need to perform monitoring tasks, you can use the stats subsystem. The monitoring data format is JSON:

uwsgi --http :9090 --wsgi-file foobar.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191

We can install uwsgitop (similar to the Linux top command) to view the monitoring data:

pip install uwsgitop

Using with Web Servers

We can use uWSGI together with the Nginx Web server to achieve higher concurrency performance.

A common nginx configuration is as follows:

location / {
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:3031;
}

The above code indicates that the Web requests received by nginx are passed to the uWSGI service on port 3031 for processing.

Now, we can generate uWSGI to use the uwsgi protocol locally:

uwsgi --socket 127.0.0.1:3031 --wsgi-file foobar.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191

If your Web server uses HTTP, then you must tell uWSGI to use the http protocol locally (this is different from --http, which creates its own proxy):

uwsgi --http-socket 127.0.0.1:3031 --wsgi-file foobar.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191

Deploy Django

Django is the most commonly used Python web framework. Assume the Django project is located at /home/foobar/myproject:

uwsgi --socket 127.0.0.1:3031 --chdir /home/foobar/myproject/ --wsgi-file myproject/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191

--chdirUsed to specify the project path.

We can put the above command into a yourfile.ini configuration file:

[uwsgi]
socket = 127.0.0.1:3031
chdir = /home/foobar/myproject/
wsgi-file = myproject/wsgi.py
processes = 4
threads = 2
stats = 127.0.0.1:9191

Next, you only need to execute the following command:

uwsgi yourfile.ini

Deploy Flask

Flask is a popular Python web framework.

Create the file myflaskapp.py with the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "<span style='color:red'>I am app 1</span>"

Execute the following command:

uwsgi --socket 127.0.0.1:3031 --wsgi-file myflaskapp.py --callable app --processes 4 --threads 2 --stats 127.0.0.1:9191
Other extensions