部署网页.py应用

FASTCGI

网页.py使用 flup 支持fastcgi的库。确保它已安装。

您只需要确保您的应用程序文件是可执行的。通过添加以下一行命令系统使用python执行它来实现:

#! /usr/bin/env python3

并在文件上设置exeutable标志:

chmod +x /path/to/yourapp.py

配置LightTPD

下面是一个示例lighttpd配置文件网页.py应用程序使用fastcgi。:

# Enable mod_fastcgi and mod_rewrite modules
server.modules   += ( "mod_fastcgi" )
server.modules   += ( "mod_rewrite" )

# configure the application
fastcgi.server = ( "/yourapp.py" =>
    ((
        # path to the socket file
        "socket" => "/tmp/yourapp-fastcgi.socket",

        # path to the application
        "bin-path" => "/path/to/yourapp.py",

        # number of fastcgi processes to start
        "max-procs" => 1,

        "bin-environment" => (
            "REAL_SCRIPT_NAME" => ""
        ),
        "check-local" => "disable"
    ))
)

 url.rewrite-once = (
    # favicon is usually placed in static/
    "^/favicon.ico$" => "/static/favicon.ico",

    # Let lighttpd serve resources from /static/.
    # The web.py dev server automatically servers /static/, but this is
    # required when deploying in production.
    "^/static/(.*)$" => "/static/$1",

    # everything else should go to the application, which is already configured above.
    "^/(.*)$" => "/yourapp.py/$1",
 )

使用这种配置,lighttpd负责启动应用程序。web服务器通过unix域套接字使用fastcgi与您的应用程序对话。这意味着这台机器和Web服务器将同时运行。

nginx+Gunicorn公司

Gunicorn'Green Unicorn'是一个用于UNIX的pythonwsgihttp服务器。它是从Ruby的Unicorn项目移植的fork-worker模型。

使网页.py应用程序与gunicorn一起工作,您需要从网页.py应用程序对象。:

import web
...
app = web.application(urls, globals())

# get the wsgi app from web.py application object
wsgiapp = app.wsgifunc()

更改完成后,gunicorn服务器将使用以下命令启动:

gunicorn -w 4 -b 127.0.0.1:4000 yourapp:wsgiapp

这从gunicorn开始,有4个工人,在本地主机上的端口4000进行监听。

最好在HTTP代理服务器后面使用Gunicorn。gunicorn团队强烈建议使用nginx。下面是一个nginx配置示例,它代理运行在上面的应用程序 127.0.0.1:4000 . ::

server {
  listen 80;
  server_name example.org;
  access_log  /var/log/nginx/example.log;

  location / {
      proxy_pass http://127.0.0.1:4000;

      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}