Webpy + Nginx with FastCGI

本文介绍如何使用Nginx作为反向代理服务器,并配合WebPy轻量级Python Web框架进行网站部署的方法。文中详细解释了配置Nginx、启动FastCGI进程及创建简单的Hello World应用的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Requirements

* Nginx 0.8.* or 0.7.* (with fastcgi and rewrite module).
* Webpy 0.32
* Spawn-fcgi 1.6.2
* Flup

Older versions may work, but aren't tested.
Resources

* Nginx wiki
* Spawn-fcgi
* Flup

Notes

* You may replace index.py with your own file name.
* /path/to/www Is the path to the directory where your webpy application is located.
* /path/to/www/index.py is the full path to your python file.
* Do not run anything until you are at Run.

Nginx configuration

location / {
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass 127.0.0.1:9002;
}

To serve static files add this:

    location /static/ {
root /path/to/www;
if (-f $request_filename) {
rewrite ^/static/(.*)$ /static/$1 break;
}
}

Note: the address and port may be different.
Spawn-fcgi

You can start a process with:

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


Start and shutdown script

Start:

#!/bin/sh
spawn-fcgi -d /path/to/www -f /path/to/www/index.py -a 127.0.0.1 -p 9002


Shutdown:

#!/bin/sh
kill `pgrep -f "python /path/to/www/index.py"`


Note: You're free to choose which address, port, directory and filename to use, but be sure to adjust the Nginx configuration.
Hello world!

Save the following code in your www directory and call the file index.py (or whatever you like). The following line is required: web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr).

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import web

urls = ("/.*", "hello")
app = web.application(urls, globals())

class hello:
def GET(self):
return 'Hello, world!'

if __name__ == "__main__":
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
app.run()


Note: make your file executable by doing chmod +x index.py. You'll get errors if it isn't executable.
Run

1. Start a process with spawn-fcgi.
2. Start Nginx.

To check if it runs do ps aux | grep index.py or simply visit the page in your browser.

To reload your configuration:

/path/to/nginx/sbin/nginx -s reload


And to stop:

/path/to/nginx/sbin/nginx -s stop
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值