Skip to content

Python uWSGI Installation and Configuration

This article primarily introduces how to deploy simple WSGI applications and common web frameworks.

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

apt-get install build-essential python-dev

1. Via pip:

pip install uwsgi

2. Download the installation script:

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

This installs the uWSGI binary to /tmp/uwsgi, which you can modify.

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 installation, you will get a uwsgi binary file in the current directory.


Let’s start with a simple “Hello World”. Create a 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 uWSGI Python loader will search for the default function named application.

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

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

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

You can add more processes with the –processes option, or add more threads with the –threads option, or use both simultaneously.

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

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

If you want 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 monitoring data:

pip install uwsgitop

We can combine uWSGI 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 means that 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 generates 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

Django is the most commonly used Python web framework. Assuming 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

–chdir is used to specify the project path.

We can turn 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

Then you only need to execute the following command:

uwsgi yourfile.ini

Flask is a popular Python web framework.

Create a 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