Python uWSGI installation configuration

Python uWSGI installation 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

Python installation uWSGI

1. Through 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 , 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 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” and create 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 that the uWSGI Python loader will search for is application .

Next we start uWSGI to run an HTTP server and deploy 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 add more threads with the –threads option, or both at the same time.

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 Linux top command) to view monitoring data:

pip install uwsgitop

Use with web server

We can use uWSGI with Nginx web server to achieve higher concurrency performance.

A commonly used nginx configuration is as follows:

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

The above code indicates that the web request received by nginx is passed to the uWSGI service on port 3031 for processing.

Now we can generate uWSGI to use the uwsgi protocol natively:

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 natively (this is different from –http which generates a proxy itself):

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 the most commonly used Python web framework. Assume that 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

Next you just 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

Digression

In this era of rapidly growing technology, programming is like a ticket to a world of infinite possibilities for many people. Among the star lineup of programming languages, Python is like the dominant superstar. With its concise and easy-to-understand syntax and powerful functions, Python stands out and becomes one of the hottest programming languages in the world.


The rapid rise of Python is extremely beneficial to the entire industry, but “There are many popular people and not many people“, which has led to a lot of criticism, but it still cannot stop its popularity. development momentum.

If you are interested in Python and want to learn Python, here I would like to share with you a Complete set of Python learning materials, which I compiled during my own study. I hope it can help you, let’s work together!

Friends in need can click the link below to get it for free or Scan the QR code below to get it for free

CSDN Gift Package: Free sharing of the most complete “Python learning materials” on the entire network(safe link, click with confidence )

?

1Getting started with zero basics

① Learning route

For students who have never been exposed to Python, we have prepared a detailed Learning and Growth Roadmap for you. It can be said to be the most scientific and systematic learning route. You can follow the above knowledge points to find corresponding learning resources to ensure that you learn more comprehensively.

② Route corresponding learning video

There are also many learning videos suitable for beginners. With these videos, you can easily get started with Python~

③Exercise questions

After each video lesson, there are corresponding exercises to test your learning results haha!

2Domestic and foreign Python books and documents

① Documents and books

3Python toolkit + project source code collection

①Python toolkit

The commonly used development software for learning Python is here! Each one has a detailed installation tutorial to ensure you can install it successfully!

②Python practical case

Optical theory is useless. You must learn to type code along with it and practice it in order to apply what you have learned into practice. At this time, you can learn from some practical cases. 100+ practical case source codes are waiting for you!

③Python mini game source code

If you feel that the practical cases above are a bit boring, you can try writing your own mini-game in Python to add a little fun to your learning process!

4Python interview questions

After we learn Python, we can go out and find a job if we have the skills! The following interview questions are all from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and Alibaba bosses have given authoritative answers. I believe everyone can find a satisfactory job after reviewing this set of interview materials.

5Python part-time channels

And after learning Python, you can also take orders and make money on major part-time platforms. I have compiled various part-time channels + part-time precautions + how to communicate with customers into documents.

All the above information , if friends need it, you can scan the QR code below to get it for free
?