UBUNTN Configuration Webpy Nginx

Source: Internet
Author: User
Tags nginx server

WEBPY Environment Construction

Before starting to build webpy, it is necessary to familiarize yourself with what is fastcgi, because the environment is to use this mode to run the WEBPY program, the specific fastcgi description can refer to various encyclopedia, FASTCGI Protocol Official website (http:/ www.fastcgi.com/drupal/) above describes some basic information, fastcgi API, development, FASTCGI implementation program, support FASTCGI Web server and so on.

In addition to fastcgi there are some other related nouns: CGI, fastcgi, Wsgi. GI is the abbreviation of Gateway interface, so the first 3 are all belong to a large type, a specification, as to the specific norms of who, how to standardize the will have their own differences.

Both the CGI and FASTCGI specifications are the communication between the HTTP server and the corresponding interpreter, such as Apache and Python, Nginx and Python. But they are not the same way of specification; CGI is the most primitive public communication protocol, it specifies that each request will be a new application process to process, close the application process after the request ends "causing the repeated new process, shutdown process, consume system resources"; fastcgi is a CGI extension, The purpose is to solve the problem of this resource consumption of CGI, which stipulates that the HTTP server starts at the same time to start several application process "application process can be configured", then resident memory waits to receive the request and processing, after processing the request to continue waiting does not exit.

Since CGI is directly connected to those with standard output, SH, tcl and so on; So when using the CGI protocol, you can invoke the application directly to start the application process and get the standard output as the returned HTML content after execution finishes. FastCGI, however, implements a fixed communication mechanism that cannot execute results by invoking the application directly, so a web server/gateway that supports the FASTCGI protocol is required to communicate with it The request is transmitted through the FASTCGI process to a specific Web request handler "Flup", which processes the request and returns the result based on the business logic, and eventually returns the HTTP server to the front end through fastcgi.

And then look at the relationship between FastCGI and Wagi, in fact, they are not related, although they are normative, but they are not a place to regulate, so there is no direct association, the only thing to associate is that some programs will implement the 2 kinds of protocols, such as: Flup

FASTCGI is a communication specification, which specifies the mode and protocol of communication, while WSGI is an interface specification, which specifies function definition and invocation.

Finally, there is a spawn-fcgi, this is a fastcgi process manager "originally LIGHTTPD Submodule, LIGHTTPD is also a similar to Nginx HTTP Server", the role is equivalent to other HTTP The fastcgi module in server, but it achieves better, in some cases the performance will be relatively high, so it is widely used by everyone, so as to separate out as a project. "So there's apache+spawn-fcgi, nginx+spawn-fcgi and so on."

The following summarizes a picture, which probably covers what is said above.


Usually build webpy are want to use its fastcgi mode, so build webpy can have 2 sets of ways to run: One is the Web services +flup, and the other is the Web services +spawn-fcgi+flup. And because spawn-fcgi only has a Linux version, Windows can only use the first way to build Webpy, while Linux uses the second way. The following is a brief description of the 2 ways to build the process, the Web server using Nginx, the official website more webpy service installation See HTTP://WEBPY.ORG/COOKBOOK/INDEX.ZH-CN Deployment Chapter, which also includes Apache, LIGHTTPD and other servers fastcgi build. \

2, Nginx configuration familiar
Because the use of Nginx service, it is necessary to familiarize yourself with the simple configuration of the Nginx server, otherwise the configuration process problems will be rushed, after the installation of Nginx in its default configuration file will have several default configuration and comment description, but in English, Here's a more detailed Chinese version: http://my.oschina.net/duxuefeng/blog/34880
Note: back up an original file before modifying

The relevant downloads and configurations in Flup are as follows: http://tool.oschina.net/uploads/apidocs/nginx-zh/PythonFlup.htm


3. Environment deployment
WEBPY Official Website Construction instructions see: HTTP://WEBPY.ORG/COOKBOOK/FASTCGI-NGINX.ZH-CN

Python, Webpy, Flup, nginx respectively installed by default, after the completion of testing the software default installation is normal, if normal start to modify Nginx configuration, add configuration a virtual host (in/etc/nginx/ Sites-available directory to create a new file, the file name can be customized, such as: example), the content is as follows:

  1. server{
  2. Listen 80;
  3. server_name www.test.com;
  4. Root/path/to/web/root;
  5. Access_log Logs/www.xx.com.access.log Main;
  6. Location/{
  7. Include Fastcgi_params; # #包含默认的fastcgi参数
  8. Fastcgi_param script_filename $fastcgi _script_name;
  9. Fastcgi_param path_info $fastcgi _script_name;
  10. Fastcgi_pass 127.0.                   0.1:9002; # #把请求通过fastcgi传送给本机的9002端口
  11. }
  12. location/static/{ #配置静态文件的访问
  13. if (-F $request _filename) { #如果请求文件名是一个文件
  14. Rewrite ^/static/(. *) $/static/$1 break ; #直接跳转到对应的资源, interrupt transmission of fastcgi
  15. }
  16. }
  17. }

After the heavy-duty nginx configuration, Nginx-s reload, for the sake of insurance, you can also do the following:

1, first test the nginx.conf file:./nginx-t
2, no problem in stop service./nginx-s stop
3, start the service./nginx

At this point, you can directly access the www.test.com, if the following page appears to indicate that the configuration Nginx fastcgi format is correct, you can continue to the following configuration

Create a new py file, such as code.py, to receive the Web request and process it with the following contents:

[Python]View PlainCopy  
  1. #!/usr/bin/env python
  2. #-*-Coding:utf-8-*-
  3. Import Web
  4. URLs = ("/.*", "Hello")
  5. App = Web.application (URLs, globals ())
  6. Class Hello:
  7. def GET (self):
  8. return ' Hello, world! '
  9. if __name__ = = "__main__":
  10. App.run ()

Run the Python program, Python code.py 9002 fastcgi
Note: The above nginx configuration file is configured to 9002, the opening is 9002, to maintain the same or not be connected.
Visit localhost through the browser to see if it returns Hello, world!

There will be some problems, as well as the following configuration:

(1) Create a file map in sites-enabled with the custom files in the Nginx/sites-available

sudo ln-s/etc/nginx/sites-available/example/etc/nginx/sites-enabled/example

(2) Delete the default in sites-enabled

Rm/etc/nginx/sites-enabled/default

(3) Restart Nginx service and related applications

Service Nginx Restart




Above is a way to build, if the use of two building, you also need to install spawn-fcgi, and then modify the contents of the code.py file, add a code, all the following:

  1. #!/usr/bin/env python
  2. #-*-Coding:utf-8-*-
  3. Import Web
  4. URLs = ("/.*", "Hello")
  5. App = Web.application (URLs, globals ())
  6. Class Hello:
  7. def GET (self):
  8. return ' Hello, world! '
  9. if __name__ = = "__main__":
  10. Web.wsgi.runwsgi = lambda func, addr=None:web.wsgi.runfcgi (func, addr) # #这行是新增的
  11. App.run ()


Then start the SPAWN-FCGI program, that is, do not need to run the code.py file directly in fastcgi mode, but by running the SPAWN-FCGI program to start the code.py program indirectly, the command to start the following:
Spawn-fcgi-d/path/to/www-f/path/to/www/code.py-a 127.0.0.1-p 9002 #端口和nginx配置里要一致
-f Specifies the Web file that invokes fastcgi, the portal file of the Web program, which is the code.py file
-d Specifies the home directory of the Web program, which is the directory where code.py resides
-a bind to address addr
-P binding to port ports
-f Specifies the number of processes produced by the FastCGI
-p Specifies the PID file path of the resulting process
-U and-G FastCGI use what identity to run
You can save the process PID to close the process:
Kill ' Cat/tmp/zcut.pid '

Like what:
Spawn-fcgi-u www-g www-p/tmp/spwn.pid-f 10-d/path/to/www-f/path/to/www/code.py-a 127.0.0.1-p 9002
In 127.0.0.1 Port 9002 to start the Webpy service as WWW, webpy the main program path to the/PATH/TO/WWW/CODE.PY,WEBPY program home directory is/path/to/www/, a total of 10 spawn processes started, All process IDs will be stored in the/tmp/spwn.pid file.

UBUNTN Configuration Webpy Nginx

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.