Summary of 9 ways to deploy python web applications

  • 2020-04-02 13:45:21
  • OfStack

The major web servers can be counted in one hand: apache, lighttpd, nginx, iis

Application, Chinese name is called application service, is you based on a web framework written application code DB server generally refers to storage services, web development using mysql more, in recent years because of the expansion of the site scale, memcache, redis such as key-value storage is also popular
The foremost web server has three functions

Efficient processing of static files,web server is developed in c, calls are native functions, IO, file transfer are targeted optimization

Acting as a simple network firewall, you can Denny some IP, simply control the number of concurrent connections, etc., better than nothing

Handle high-concurrency short connection requests and forward requests from thousands of users through dozens of long connections on the Intranet. One reason is that web server is specialized in handling high-concurrency and the other reason is that most application frameworks do not have the ability to handle high-concurrency

In fact, there are some web framework with built-in support epoll/kqueue efficient network library, and have the ability to deal with high concurrency, such as tornado of python, Java tomcat, jetty, etc., some people just get rid of the front-end web server, streaking directly, but in the deployment of public application, best not to do this, because 1, 2, two reasons mentioned above, the user brower to web server network status is strange, you can't imagine,

Nginx is strongly recommended for web server for three reasons

Very good performance, very stable
Simple installation with few dependencies
Conf files are very easy to configure, simpler than apache/lighttpd
There are nine ways to deploy a web application developed in python

Mod_python, which is a built-in module of apache, depends heavily on the python version used for mod_python compilation. It is not recommended to use it with apache

Cgi, this is too old, is not recommended, and nginx does not support cgi, can only use lighttpd or apache

fastcgi   , which is currently the most popular practice, supported by the flup module, the corresponding configuration instruction in nginx is fastcgi_pass

Spawn -fcgi, this is the fastcgi multi-process manager, lighttpd installation package shipped, and flup effect, the difference is that flup is python code level introduction, spawn-fcgi is the external program. Spawn -fcgi is very versatile, can support any language development code, PHP,python,perl, as long as your code to implement the fastcgi interface, it can help you manage your process

Scgi, whose full name is Simple Common Gateway Interface, is also an alternative version of cgi. Scgi protocol is very Simple. I think it is similar to fastcgi, but it has not been widely popularized.

HTTP, nginx USES proxy_pass forwarding, which requires that the back-end appplication must be built with an HTTP server that can handle high concurrency. In python's web framework, tornado can only be selected.

Python programmers love to invent wheels. Tornado is not only a web framework, it can also provide high-performance HTTP server separately. Therefore, if you use other python frameworks to write code, such as bottle, you can also start a high-performance HTTP server by importing tornado. By extension, there are many other HTTP servers in the python package that can handle high concurrency, such as gevent, and can be referenced by other frameworks to support HTTP deployment.

In real life, using Java for web applications, usually using HTTP and nginx, the application server chooses tomcat or jetty

Uwsgi consists of four parts,

Uwsgi protocol
Web server has built-in support for protocol modules
Application server protocol support module
Process control program

Nginx start 0.8.4 built-in support uwsgi protocol, uwsgi protocol is very simple, a 4 byte header + a body, the body can be a lot of package of the agreement, such as HTTP, cgi (through the header fields marked), such as I used to do a small performance contrast test, the results show that uwsgi compared with fastcgi performance not too obvious advantages, also may be the reason for the smaller data sets

Uwsgi is characterized by its own process control program, which is written in c language and USES the natvie function, which is actually similar to spawn-fcgi/php-fpm. So uwsgi can support a variety of application framework, including (python, lua, ruby, Erlang, go, and so on

Gunicorn, a similar tool to uwsgi, was ported from rails deployment tool Unicorn. But the protocol it USES is WSGI, the full name of the Python Web Server Gateway Interface, which is the official standard defined by Python 2.5 (PEP 333)   ), the root red seedling is positive, and the deployment is relatively simple, (link: http://gunicorn.org/)   There is a detailed tutorial

Mod_wsgi, an apache module, and also supports WSGI agreement, (link: https://code.google.com/p/modwsgi/)

The pros and cons of the fastcgi protocol versus the HTTP protocol in code deployment

Although fastcgi is a binary protocol, it does not save resources compared to the HTTP protocol. Binary protocol, can only save the expression of Numbers, such as 1234567, in a string of seven bytes, in a number of four bytes, and the string is the same everywhere

Fastcgi in the transmission of data, in order to be compatible with the cgi protocol, but also with a bunch of cgi environment variables, so compared with the HTTP protocol, using fastcgi data transmission is not save, but a few more

The only advantage of fastcgi is that it is a long connection, and the user concurrently sends 1000 requests. Fastcgi may forward 10 links to the back-end appplication, and if the HTTP protocol is used, it will issue 1000 requests to the back-end appplication

HTTP proxy forwarding is problematic in the face of high concurrency because port is an int16 integer in the TCP stack   To create a new connect locally, you need to consume a port up to 65536. Hundreds of thousands of requests are being made externally, the port pool is drying up, and your server can only refuse to respond

conclusion

My personal habit is to use the fastcgi protocol to deploy python program, simple, choose the technical solution, be sure to choose the simplest and most common, this blog's fastcgi run script is as follows


kill - `cat / tmp / django.pid`
echo 'restart django....' 
python . / manage.py runfcgi - - settings = lutaf.settings_r maxchildren =  maxspare = minspare =  method = prefork pidfile = / tmp / django.pid host = 127.0 . 0.1  port = outlog = / tmp / dj.out errlog = / tmp / dj.error

I recommend you try Gunicorn. This is the future


Related articles: