Summary of deployment methods for 9 python web programs

Source: Internet
Author: User
Mainstream Web server can count on a slap, Apache,lighttpd,nginx,iis.

Application, the Chinese name is to do the application service, is you write based on a web framework application code DB Server refers to the storage services, web development with MySQL more, in recent years because of the scale of the site, Memcache, Such key-value such as Redis are also popular
Top-of-the-Web server has 3 features

Efficient processing of static files, Web server is developed with C, call is native function, the IO, file transfer are targeted optimization

As a simple network firewall, you can Denny some IP, simply control the number of concurrent connections, and so on, better than nothing

Handles high concurrent short connection requests, forwarding thousands of users ' request through dozens of long connections on the intranet, one reason is that Web server handles high concurrency and is professional, and another reason is that most application frameworks do not have the ability to handle high concurrency

In fact, there are some web frameworks in the market because of the built-in support epoll/kqueue and other efficient network library, but with the ability to handle high concurrency, such as the Python Tornado,java system tomcat,jetty and so on, Someone will get rid of the front-end Web server, directly naked, but in the deployment of public network applications, it is best not to do so, because the aforementioned 1, 22 reasons, user brower to Web server network condition is strange, you can not imagine,

Web server strongly recommends using Nginx for three reasons

Excellent performance, very stable
Simple installation, less dependent packages
conf files are very easy to configure and easier than APACHE/LIGHTTPD
There are 9 ways to deploy a Python-developed web program

Mod_python, this is the Apache built-in module, very heavily dependent on Mod_python compiled using the Python version, and Apache companion use, not recommended

CGI, this is too old, not recommended, and Nginx does not support CGI mode, only with LIGHTTPD or Apache

FastCGI, this is currently the most popular practice, through the Flup module to support, in Nginx corresponding configuration instructions are Fastcgi_pass

SPAWN-FCGI, this is fastcgi multi-process management program, LIGHTTPD installation package comes with, and flup effect, the difference is Flup is the Python code-level introduction, SPAWN-FCGI is an external program. SPAWN-FCGI uses a wide range of code to support any language development, Php,python,perl, as long as your code implements the FastCGI interface, it can help you manage your process

SCGI, the full name is simple Common Gateway Interface, but also the alternative version of CGI, SCGI protocol is very easy, I think and fastcgi almost, just not how to promote, nginx corresponding configuration command is Scgi_pass, If you want to use it, Flup also supports it.

HTTP, nginx use Proxy_pass forwarding, this requires the backend appplication must be built to handle high-concurrency HTTP server, in the Python web framework, only select Tornado.

Python programmers like to invent wheels, tornado in addition to being a web framework, it can also provide high-performance HTTP server alone, so if you use other Python framework to write code, such as bottle, you can also through the import Tornado to start a high-performance HTTP server, the same can be used with the HTTP protocol and Nginx deployment. Extension, Python packages can handle high-concurrency HTTP server There are many, such as gevent, can also be referenced by other frameworks to support the deployment of HTTP.

In reality, Java is used to do web programs, usually with HTTP and nginx mates, application Server select Tomcat or jetty

Uwsgi, consisting of 4 parts,

UWSGI protocol
Web server built-in Support protocol module
Application Server protocol Support module
Process Control Procedures

Nginx from 0.8.4 start built-in Support UWSGI protocol, UWSGI protocol is very simple, a 4 byte header+ a body,body can be a lot of protocol packages, such as http,cgi (through the header field), I used to do a small-scale performance comparison test, and the results showed that performance was less obvious than Uwsgi and fastcgi, and could be the reason for the smaller data set.

Uwsgi is characterized by its own process Control program. It is written in C, using the Natvie function, which is actually similar to SPAWN-FCGI/PHP-FPM. So UWSGI can support a variety of application frameworks, including (PYTHON,LUA,RUBY,ERLANG,GO), etc.

Gunicorn, and Uwsgi similar tools, migrated from the Rails deployment Tool (Unicorn). But the protocol it uses is WSGI, the full name is Python Web Server Gateway Interface, which is the official standard defined at python2.5 (PEP 333), Root red Miaozheng, and deployment is relatively simple, http://gunicorn.org /On a detailed tutorial

Mod_wsgi, a module of Apache, is also supporting the WSGI protocol, https://code.google.com/p/modwsgi/

Comparison of FASTCGI protocol and HTTP protocol in code deployment

Although FASTCGI is a binary protocol, it does not conserve resources relative to the HTTP protocol. Binary protocol, can only save the expression of numbers, such as 1234567, with a string to represent the need for 7 byte, with a number is 4 byte, and the string to where all the same

FastCGI in the transfer 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, with the fastcgi transmission of data does not save, but more

FastCGI the only advantage is that it is a long connection, the user concurrent 1000 request,fastcgi may be used 10 links to the back end of the appplication, if the HTTP protocol, how much to give, will be back to the end Appplication Initiate 1000 requests

HTTP proxy forwarding, in the face of ultra-high concurrency in the case of problems, because, TCP stack, port is int16 integer you create a local connect, you need to consume a port, up to 65536. External concurrent hundreds of thousands of requests, port pool dry, your server can only refuse to respond

Summarize

My personal habit is to use fastcgi protocol to deploy Python program, simple and easy, choose technical solution, must choose the simplest most common, this blog's fastcgi run script 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

Recommend everyone try Gunicorn, this is the future development direction

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.