Easy Tutorial
❮ Ref Math Tau Ref Math Fabs ❯

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 necessary dependencies:

apt-get install build-essential python-dev

Installing uWSGI with Python

1. Via pip command:

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. Install from source code:

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.


First WSGI Application

Let's start with a simple "Hello World," create a file named 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 application.

Next, we start uWSGI to run an HTTP server, deploying the application 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 add more processes with the --processes option or more threads with the --threads option, or both.

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, which outputs data in JSON format:

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

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

pip install uwsgitop

Integrating with Web Servers

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 indicates that Nginx forwards web requests to the uWSGI service on port 3031.

Now, we can start 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, you must tell uWSGI to use the HTTP protocol locally (this is different from the --http option that generates a proxy):

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

Deploying Django

Django is a 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

The --chdir option specifies the project path. We can put the above commands 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

Deploying 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
❮ Ref Math Tau Ref Math Fabs ❯