SoFunction
Updated on 2024-11-16

Detailed Procedure for Installing and Configuring uWSGI in Python

This article focuses on how to deploy simple WSGI applications and common web frameworks.

In the case of Ubuntu/Debian, install the dependencies first:

apt-get install build-essential python-dev

Python installs uWSGI

1. via the pip command:

pip install uwsgi

2. Download and install the script:

curl /install | bash -s default /tmp/uwsgi

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

3. Source code installation:

wget /downloads/
tar zxvf 
cd uwsgi-latest
make

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

First WSGI application

Let's start with a simple "Hello World" and create the file 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 

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  --master --processes 4 --threads 2

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

If you want to perform monitoring tasks, you can use the stats subsystem, where the data format for monitoring is JSON:

uwsgi --http :9090 --wsgi-file  --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

Used in conjunction with a Web server

We can use uWSGI in conjunction 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 web requests received using 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  --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 generating a proxy for -http yourself):.

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

Deploying Django

Django is the most commonly used Python web framework. Assuming that the Django project is located in /home/foobar/myproject.

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

--chdir is used to specify the project path.

We can make the above commands into a configuration file.

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

Next you just need to execute the following command:

uwsgi 

Deploying Flask

Flask is a popular Python web framework.

Create a file with the following code:

from flask import Flask
app = Flask(__name__)
@('/')
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  --callable app --processes 4 --threads 2 --stats 127.0.0.1:9191

This article on the Python uWSGI installation and configuration of the article is introduced to this, more related Python uWSGI installation and configuration of the content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!