通过fastcgi和lighttpd部署

技术文档网 2021-04-21

如果你对这个主题有任何问题,可以点击下面的链接访问相应的话题:

http://www.mail-archive.com/webpy@googlegroups.com/msg02800.html

下面的代码基于lighttpd 1.4.18,更高版本也可以工作

##Note:

  • 你可以重命名 code.py为任何你自己愿意的名字,该例子还是以code.py为例。
  • /path-to/webpy-app 为包含你的 code.py代码的路径。
  • /path-to/webpy-app/code.py 应该是你的python file的完整路径。

如果你还不确定你的lighttpd版本的话,你可以在命令行中使用lighttpd -v查看相应的版本信息。

Note: 较早版本的lighttpd可能会按照不同的方式组织.conf文件,但是它们应该遵循的是相同的原则。

###ligghttpd 在 Debian GNU/Linux 下的配置文件

Files and Directories in /etc/lighttpd:
---------------------------------------

lighttpd.conf:
         main configuration file

conf-available/
        This directory contains a series of .conf files. These files contain
        configuration directives necessary to load and run webserver modules.
        If you want to create your own files they names should be
        build as nn-name.conf where "nn" is two digit number (number
        is used to find order for loading files)

conf-enabled/
        To actually enable a module for lighttpd, it is necessary to create a
        symlink in this directory to the .conf file in conf-available/.

Enabling and disabling modules could be done by provided
/usr/sbin/lighty-enable-mod and /usr/sbin/lighty-disable-mod scripts.
对于web py, 你需要允许 mod_fastcgi 模块和 mod_rewrite模块, 运行: /usr/sbin/lighty-enable-mod 启用 fastcgi (Mac OS X可能不需要) (mod_rewrite 模块可能需要启用 10-fastcgi.conf文件).

##下面是文件的基本结构(Mac OS X不同):

  • /etc/lighttpd/lighttpd.conf
  • /etc/lighttpd/conf-available/10-fastcgi.conf
  • code.py

对于Mac OS X或任何以Mac Ports邓方式安装的lighttpd,可以直接在路径下编写.conf文件并用lighttpd -f xxx.conf启动lighttpd,而无需去修改或考虑任何文件结构。

/etc/lighttpd/lighttpd.conf

server.modules              = (
            "mod_access",
            "mod_alias",
            "mod_accesslog",
            "mod_compress",
)
server.document-root       = "/path-to/webpy-app"

对我来说,我使用 postgresql,因此需要授予对的数据库权限,可以添加行如下(如果不使用则不需要)。

server.username = "postgres"

/etc/lighttpd/conf-available/10-fastcgi.conf

server.modules   += ( "mod_fastcgi" )
server.modules   += ( "mod_rewrite" )

 fastcgi.server = ( "/code.py" =>
 (( "socket" => "/tmp/fastcgi.socket",
    "bin-path" => "/path-to/webpy-app/code.py",
    "max-procs" => 1,
   "bin-environment" => (
     "REAL_SCRIPT_NAME" => ""
   ),
   "check-local" => "disable"
 ))
 )

如果本地的lighttpd跑不起来的话,需要设置check-local属性为disable。

 url.rewrite-once = (
   "^/favicon.ico$" => "/static/favicon.ico",
   "^/static/(.*)$" => "/static/$1",
   "^/(.*)$" => "/code.py/$1",
 )

/code.py
在代码头部添加以下代码,让系统环境使用系统环境中当前的python

#!/usr/bin/env python

最后不要忘记了要对需要执行的py代码设置执行权限,否则你可能会遇到“permission denied”错误。

$ chmod 755 /path-to/webpy-app/code.py

相关文章

  1. supervisor使用教程

    一、安装 1:easy_install 安装: easy_install supervisor 2:pip 安装: pip install supervisor 3:Debian / Ubuntu

  2. gitlab的ssh key不生效的问题

    在用 gitlab 的管理代码时发现一个问题:如果用 http 协议,每次 push 的时候都需要输入用户名和密码,如果 用 ssh 协议,先要生成公钥: ssh-keygen -t rsa -C

  3. UOJ安装指南

    这是一个UOJ的docker版本。在安装之前,请确认Docker已经安装在您的操作系统中。这个docker的映像是64位的版本,在32位的系统上安装可能会出现错误。 安装 请先下载 JDK7u76

  4. untu14.04下创建用户并赋予执行sudo命令的权限

    untu14.04下创建用户并赋予执行sudo命令的权限 创建用户:adduser +用户名(该命令在home下生成用户目录并创建用户) 1. 切换到root用户下 2. /etc/sud

  5. 如何查看当前apache的工作模式prefork worker还是event模式?

    查看apache工作模式 $ apachectl -V (注:apachectl可理解为apache control,其实是一段bash脚本) Server version: Apache/2.4.

随机推荐

  1. supervisor使用教程

    一、安装 1:easy_install 安装: easy_install supervisor 2:pip 安装: pip install supervisor 3:Debian / Ubuntu

  2. gitlab的ssh key不生效的问题

    在用 gitlab 的管理代码时发现一个问题:如果用 http 协议,每次 push 的时候都需要输入用户名和密码,如果 用 ssh 协议,先要生成公钥: ssh-keygen -t rsa -C

  3. UOJ安装指南

    这是一个UOJ的docker版本。在安装之前,请确认Docker已经安装在您的操作系统中。这个docker的映像是64位的版本,在32位的系统上安装可能会出现错误。 安装 请先下载 JDK7u76

  4. untu14.04下创建用户并赋予执行sudo命令的权限

    untu14.04下创建用户并赋予执行sudo命令的权限 创建用户:adduser +用户名(该命令在home下生成用户目录并创建用户) 1. 切换到root用户下 2. /etc/sud

  5. 如何查看当前apache的工作模式prefork worker还是event模式?

    查看apache工作模式 $ apachectl -V (注:apachectl可理解为apache control,其实是一段bash脚本) Server version: Apache/2.4.