Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python uWSGI Installation and Configuration

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

Install uWSGI for Python

1. Using 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 change it.

3. Install from source:

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.


The first WSGI application

Let's start with a simple "Hello World" and 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 default function application that the uWSGI Python loader will search for.

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

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

Add 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 use both at the same time.

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

The command above will generate 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 the monitoring data:

pip install uwsgitop

Use with a web server

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 code above means that web requests received by nginx are passed to the uWSGI service on port 3031 for handling.

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, you must tell uWSGI to use the http protocol locally (this differs from --http, which generates a proxy by itself):

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

--chdir is used to specify 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

Then you only need to execute the following command:

uwsgi yourfile.ini

Deploy Flask

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 "I am app 1"

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