Nginx + FastCGI + Python

Source: Internet
Author: User

More articles on the Internet use Django, webpy, and other frameworks. Because I only use its CGI instead of pure web development, I do not plan to use such frameworks for the moment, directly use the flup package (in fact, most of the frameworks wsgi are also encapsulated flup ).

If you are not talking nonsense, go to the topic and check the required environment and tools:

1. linux2.6 (nonsense --!)

2. nginx (the FastCGI module must be included)

3. spawn-fcgi (Official download: http://redmine.lighttpd.net/projects/spawn-fcgi/wiki)

4. Python

5. flup (download: http://trac.saddi.com/flup)

Installation is a common method, so I will not discuss it one by one.

Just copy the FastCGI configuration of nginx to PhP.

First, draw a simple schematic diagram.

Spawn-fcgi is a tool used to enable multiple wsgiserver processes in FastCGI mode.

Example:

Spawn-fcgi-F/data/wsgiserver. py-A 127.0.0.1-P 5678-u www-F 5

-F determine the path of the wsgiserver script

-A FastCGI bound IP Address

-P FastCGI-bound Port

-What identity Does u run?

-F indicates the number of processes to be started (many people think it is-C, which is actually dedicated to PhP. Here we will use-f)

 

The part to be encoded is the wsgiserver. Here, we paste a simple test code.

 

#! /Usr/bin/ENV Python #-*-coding: UTF-8-*-import flup. server. fcgi as flupsdef application (Environ, start_response): "this function can be written as a module import" ret = "" try: uri = environ ['path _ info'] If Uri [-1] = "/": uri = URI [:-1] If uri = "": ret = STR (Environ) Elif uri = "/sleep": import time. sleep (5) ret = "sleep: 5 secends" else: ret = URI failed t exception, E: ret = STR (E) status = '200 OK 'response_headers = [('content-type', 'text/plain')] start_response (status, response_headers) return [RET] If _ name _ = "_ main _": # Run in Python # flups. wsgiserver (application, multithreaded = true, Multiprocess = false, bindaddress = ('2017. 0.0.1 ', 5678 )). run () # Run flups in FastCGI mode. wsgiserver (application ). run ()

Er, that's it ~

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.