Home  >  Article  >  Operation and Maintenance  >  How to configure a Python web project using Nginx and uWSGI

How to configure a Python web project using Nginx and uWSGI

WBOY
WBOYforward
2023-05-17 20:52:041682browse

For python-based web projects, common deployment methods are:

  • fcgi: Use spawn-fcgi or the tools that come with the framework to generate listening processes for each project, and then use them with http Service interaction.

  • wsgi: Use the mod_wsgi module of the http service to run each project.

But there is also uwsgi, which neither uses the wsgi protocol nor the fcgi protocol. Instead, it creates its own uwsgi protocol. According to the author, this protocol is about 10 times as powerful as the fcgi protocol. so fast. The main features of uwsgi are as follows:

  • Ultra-fast performance.

  • Low memory usage (measured to be about half of apache2’s mod_wsgi).

  • Multi-app management.

  • Detailed log function (can be used to analyze app performance and bottlenecks).

  • Highly customizable (memory size limit, service restart after a certain number of times, etc.).

Environment ubuntu 12.04 ip:10.1.6.79

Install nginx

apt-get install nginx-full nginx-common

nginx configuration/etc/nginx/sites- enabled/example

server {
    listen     80;
    server_name   10.1.6.79;
    access_log   /var/log/nginx/example_access.log;
    error_log    /var/log/nginx/example_error.log;
    root      /var/www/example;
    location / {
      uwsgi_pass   127.0.0.1:9001;
      include     uwsgi_params;
      uwsgi_param   uwsgi_scheme $scheme;
      uwsgi_param   server_software  nginx/$nginx_version;
    }
}

Install uwsgi

apt-get install uwsgi uwsgi-plugin-python

If you want to install all uwsgi plug-ins, you can install the uwsgi-plugin-all package

uwsgi configuration/etc/uwsgi/apps-enabled/default.xml

<uwsgi>
  <plugin>python</plugin>
  <socket>127.0.0.1:9001</socket>
  <pythonpath>/var/www/example/app/</pythonpath>
  <app mountpoint="/">
    <script>wsgi_configuration_module</script>
  </app>
  <master/>
  <processes>4</processes>
  <reload-mercy>8</reload-mercy>
  <cpu-affinity>1</cpu-affinity>
  <max-requests>2000</max-requests>
  <limit-as>512</limit-as>
  <reload-on-as>256</reload-on-as>
  <reload-on-rss>192</reload-on-rss>
  <no-orphans/>
  <vacuum/>
</uwsgi>

The parameters in the uwsgi configuration file can also be specified through uwsgi on the command line. In addition to the xml format, the configuration file can also be written in ini format. After the software package is installed, there will be some examples of xml and ini format configuration files in the /usr/share/doc/uwsgi/examples/conffile directory.

wsgi_configuration_module.py script content

#!/usr/bin/python
import os
import sys
sys.path.append(&#39;/var/www/example/app&#39;)
os.environ[&#39;python_egg_cache&#39;] = &#39;/var/www/example/.python-egg&#39;
def application(environ, start_response):
  status = &#39;200 ok&#39;
  output = &#39;hello world!&#39;
  response_headers = [(&#39;content-type&#39;, &#39;text/plain&#39;),
          (&#39;content-length&#39;, str(len(output)))]
  start_response(status, response_headers)
  return [output]

Start uwsgi

uwsgi -x /etc/uwsgi/apps-enabled/default.xml --daemonize /var/log/uwsgi/app/default.log

uwsgi parameters:
-m Start the master process
-p 4 Start 4 processes
-s The port or socket address used
-d Use daemon mode to run. Note that after using -d, you need to add the log file address, such as -d /var/log/uwsgi .log
-r 10000 After opening 10,000 processes, automatically respawn
-t 30 Set a timeout of 30s. After the timeout, the link will be automatically abandoned
–limit-as 32 The total memory amount of the process Controlled at 32m
-x Use configuration file mode

Concurrent 4 threads

uwsgi -s :9090 -w myapp -p 4

Main control thread 4 threads

uwsgi -s :9090 -w myapp -m -p 4

Execution exceeds The client will give up directly after 30 seconds

uwsgi -s :9090 -w myapp -m -p 4 -t 30

Limit the memory space to 128m

uwsgi -s :9090 -w myapp -m -p 4 -t 30 --limit-as 128

Serve more than 10,000 reqs and automatically respawn

uwsgi -s :9090 -w myapp -m -p 4 -t 30 --limit-as 128 -r 10000

run in the background, etc.

uwsgi -s :9090 -w myapp -m -p 4 -t 30 --limit-as 128 -r 10000 -d uwsgi.log

In addition to starting directly with the uwsgi command, you can also start it with the script under init.d. However, you need to modify the path of the default configuration file in /etc/default/u wsgi first, and then use / etc/init.d/uwsgi startStart

#inherited_config=/usr/share/uwsgi/conf/default.ini
inherited_config=/etc/uwsgi/apps-enabled/default.xml

Start nginx

/etc/init.d/nginx start

The effect is as follows:

How to configure a Python web project using Nginx and uWSGI

Test whether uwsgi is available
Test script test.py

#!/usr/bin/python
def application(env,start_response):
  start_response(&#39;200 ok&#39;,[(&#39;content_type&#39;,&#39;text/html&#39;)])
  return "congraduation!!! uwsgi testing ok!!!
#启动web server
uwsgi --http :9090 --wsgi-file test.py

Browser input ip: port: 192.168.1.99:9090
You can see "congraduation !!!uwsgi testing ok!!!”

The above is the detailed content of How to configure a Python web project using Nginx and uWSGI. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete