Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure Python's web project using Nginx and uWSGI

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

The knowledge of this article "how to use Nginx and uWSGI to configure Python's web project" is not understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Nginx and uWSGI to configure Python's web project" article.

Common deployment methods for python-based web projects are:

Fcgi: use spawn-fcgi or the tools included in the framework to generate listening processes for each project, and then interact with http services.

Wsgi: use the mod_wsgi module of the http service to run each project.

But there is also a uwsgi, which uses neither wsgi nor fcgi, but creates a uwsgi protocol that, according to the authors, is about 10 times faster than fcgi. The main features of uwsgi are as follows:

Ultra-fast performance.

Low memory footprint (about half of apache2's mod_wsgi measured).

Multi-app management.

Detailed logging capabilities (which can be used to analyze app performance and bottlenecks).

Highly customizable (memory size limit, restart after a certain number of services, 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 server_name 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 the uwsgi plug-ins, you can install the uwsgi-plugin-all package

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

Python 127.0.0.1:9001 / var/www/example/app/ wsgi_configuration_module 4 8 1 2000 512 256 192

The parameters in the uwsgi configuration file can also be specified through uwsgi on the command line. In addition to xml format, the configuration file can also be written in ini format. After the 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/pythonimport osimport syssys.path.append ('/ var/www/example/app') os.environ ['python_egg_cache'] =' / var/www/example/.python-egg'def application (environ, start_response): status = '200 ok' output =' hello worldview' Response_headers = [('content-type',' text/plain'), ('content-length', 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

Parameters of uwsgi:

-m start the master process

-p 4 start 4 processes

-Port or socket address used by s

-d runs as daemon. Note that after using-d, you need to add the address of the log file, such as-d / var/log/uwsgi.log

-r 10000 after 10000 processes are started, automatically respawn

-t 30 sets a timeout of 30s. After the timeout, the link will be abandoned automatically.

-limit-as 32 limits the total memory of the process to 32m

-x uses profile mode

Concurrently 4 threads

Uwsgi-s: 9090-w myapp-p 4

Main control thread + 4 threads

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

Execute client for more than 30 seconds and give up directly.

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

Limit memory space to 128m

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

Serve more than 10000 req automatic respawn

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

Running 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 with the script under init.d, but you need to modify the path of the default configuration file in / etc/default/u wsgi first, and then start with / etc/init.d/uwsgi start

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

Start nginx

/ etc/init.d/nginx start

The effect is as follows:

Test if uwsgi is available

Test script test.py

#! / usr/bin/pythondef application (env,start_response): start_response ('200ok', [(' content_type','text/html')]) return "congradation examples! uwsgi testing okshelf launch # launch web serveruwsgi-- http: 9090-- wsgi-file test.py

Browser input ip: Port: 192.168.1.99pur9090

You can see "congradation games! uwsgi testing cookies!"

The above is the content of this article on "how to use Nginx and uWSGI to configure Python's web project". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 277

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report