Python installation uwsgi

pip install uwsgi

First WSGI application

Starting with a simple “Hello World” file, create foobar.py as follows:

def application(env, start_response) :
    start_response('200 OK', [('Content-Type'.'text/html')])
    return [b"Hello World"]
Copy the code

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

Add concurrency and monitoring

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

You can use — Processes to add more processes, — Threads to add more threads, or both.

Uwsgi — HTTP :9090 –wsgi-file foobar.py –master –processes 4 — Threads 2

If you want to perform a monitoring task, you can use the STATS subsystem to monitor the data in JSON format: 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

Deploying Django

Django is the most commonly used Python Web framework, assuming that the Django project is located at /var/www/html/mysite/:

Collecting Static Resources

  1. settings.py
The starting URL for static resource access
STATIC_URL = '/static/'
# specify the directory where the static resource resides
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')]Set the path to collect static resources (for deployment)
STATIC_ROOT = os.path.join(BASE_DIR, 'collect_static/')
Copy the code
  1. Collecting Static Resources
python manage.py collectstatic
Copy the code

Configuration uwsgi

  1. The installationpip install uwsgi
  2. New file uwsgi.ini; Same level as manage.py
[uwsgi]
; Listening port
http = :8000

; Specifies the mode of socket communication with nginx: port or file
; The socket = 127.0.0.1:8001
; socket = /home/kzzf/project/OfferHelp/OfferHelp.sock

; The project is located in the same directory as manage.py
chdir=/var/www/html/mysite/
#chdir = /home/kzzf/project/OfferHelp

; Directory where the virtual environment resides
home=/root/django
PYTHONHOME = /root/django/bin/

; Wsgi files in the main application
wsgi-file = mysite/wsgi.py

# request size
#buffer-size = 65536

; Failed to use the routing proxy static resource
; static-safe=/home/kzzf/project/OfferHelp/static/
; route = /static/(.*) static:/home/kzzf/project/OfferHelp/static/$1

; Proxy static resources: path maps
#static-map = /static=/home/kzzf/project/OfferHelp/collect_static

; Start a master process to manage the remaining child processes
master=True
processes = 4
threads = 2

; Save the PID of the main process to control the UWSGI service
pidfile=/var/www/html/mysite/uwsgi.pid
; Start the project uwsGi uwsgi.ini
; Uwsgi --stop/reload XXx. pid Stops/restarts the UWSgi

; Set up background running and save logs
daemonize=/var/www/html/mysite/log/uwsgi.log
; deamonize=1 ; Used to configure the background run

; Set an upper limit on the number of requests each worker process can handle. When this limit is reached, the process will be reclaimed (restarted). Prevents memory leaks
max-requests=5000

Unix Socket and PID files are automatically removed when the service stops
vacuum=true
Copy the code
  1. Start the project
uwsgi uwsgi.ini

# stop
uwsgi --stop uwsgi.pid
pkill -f uwsgi -9

Copy the code

Used with a Web server

We can combine uWSGI with Nginx Web server for higher concurrency performance.

A common nginx configuration is as follows:

location / {
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:3031;
}
Copy the code

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 (which is different from the HTTP protocol that 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