Nginx + Web. py and FastCGI build the Web. py Environment

Source: Internet
Author: User

Web. py is a lightweight Python web framework, which is simple and powerful. Web. py is an open-source project.

1. Required software:

Nginx nginx-1.4.7.tar.gz (must contain fastcgi and rewrite modules ).

Webpy 0.32

Spawn-fcgi 1.6.2

Flup

Note: Flup is the most common software that you forget to install.

Older versions should also work, but have not been tested. The latest version can work.

2. Install software

Install nginx:

Wget http://nginx.org/download/nginx-1.4.7.tar.gz

Tar zxvf nginx-1.4.7.tar.gz

Cd nginx-1.4.7

Yum-y install pcre-devel

./Configure -- prefix =/usr/local/nginx -- with-http_ssl_module -- with-http_stub_status_module -- with-http_gzip_static_module -- with-http_stub_status_module

Make & make install

Install web. py, Spawn-fcgi, and Flup

Install spawn-fcgi

Wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz

Tar zxvf spawn-fcgi-1.6.3.tar.gz

./Configure

Make & make install

Install flup

Pip install flup

Install web. py

Pip install web. py

Nginx configuration file

Server {

Listen 80;

Server_name localhost;

Root/usr/local/nginx/html/webpy;

Location /{

Include fastcgi_params;

Fastcgi_param SCRIPT_FILENAME $ fastcgi_script_name;

Fastcgi_param PATH_INFO $ fastcgi_script_name;

Fastcgi_pass 127.0.0.1: 9002;

}

Error_page 500 502 503 x.html;

Location =/50x.html {

Root html;

}

Location/static /{

If (-f $ request_filename ){

Rewrite ^/static/(. *) $/static/$1 break;

}

}

}


Check the configuration file and start nginx

[Root @ test controllers] #/usr/local/nginx/sbin/nginx-t

Nginx: the configuration file/usr/local/nginx/conf/nginx. conf syntax is OK

Nginx: configuration file/usr/local/nginx/conf/nginx. conf test is successful

[Root @ test controllers] #/usr/local/nginx/sbin/nginx

Create a python file in the web and Directory

Save the following code as index. py (or whatever you like), note that if you use Nginx configuration, web. wsgi. runwsgi = lambda func, addr = None: web. wsgi. the runfcgi (func, addr) line of code is required.

#! /Usr/bin/env python

#-*-Coding: UTF-8 -*-

Import web

Urls = ("/. *", "hello ")

App = web. application (urls, globals ())

Class hello:

Def GET (self ):

Return 'hello, world! 3305'

If _ name _ = "_ main __":

Web. wsgi. runwsgi = lambda func, addr = None: web. wsgi. runfcgi (func, addr)

App. run ()

Note: you also need to set permissions for the Code. The Code is as follows: chmod + x index. py.

Start and disable Spawn-fcgi

Start spawn-fcgi

Spawn-fcgi-d/path/to/www-f/path/to/www/index. py-a 127.0.0.1-p 9002

Disable Spawn-fcgi

Kill 'pgrep-f "python/path/to/www/index. py "'

Enter an IP address in the browser to access the server, as shown in:

Use Gunicorn to deploy the web. py Application

Nginx + uWSGI + web. py build example

This article permanently updates the link address:

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.