• linux平台下使用 nginx + spawn-cgi 部署webpy程序


    需要安装的工具:

    • nginx
    • webpy
    • flup(在spawn-fcgi安装之前安装)
    • spawn-fcgi

    注意:

    • 你可以重命名index.py为任何你想要的文件名。
    • /path/to/www 为代码路径。
    • /path/to/www/index.py为python代码的完整路径。

    nginx配置:

    这里在vhosts目录下新建一个webpy.conf配置文件

    添加以下配置信息:

    location / {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;  # [1]
        fastcgi_param PATH_INFO $fastcgi_script_name;        # [2]
        fastcgi_pass 127.0.0.1:9002;
    }


    对于静态文件配置:

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

    注意:别忘了添加网站根目录配置

     配置nginx后,可以运行以下命令来检查配置文件是否正确

    [root@server www]# nginx -t
    nginx: the configuration file /path/server/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /path/server/nginx/conf/nginx.conf test is successful

    运行python程序,python index.py 9002 fastcgi

    通过浏览器可查看配置成功,配置成功在浏览器显示hello world

    Spawn-fcgi配置和使用

    运行一下命令打开spawn-fcgi命令:

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

    注意:添加-P /tmp/webpy.pid可以记录进程id 添加-n可查看错误明细

    成功运行显示一下信息:

    spawn-fcgi: child spawned successfully: PID: 1562

    你也可以在系统中添加启动和停止脚本

    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"`

    Hello world!

    将下面的代码保存为index.py或者任何你喜欢的名字

    注意,使用nginx配置,这一行是必须的 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()

     注意,同样需要给代码配置权限,命令如下chmod +x index.py

    运行

    1. 打开一个spawn-fcgi进程
    2. 启动nginx

    检查程序是否启动成功,可以使用以下命令 ps aux|grep index.py 进行查看

    重启nginx命令

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

    停止nginx

    /path/to/nginx/sbin/nginx -s stop
    

    常见问题解决

    问题1 child exited with 2

    solution: insert #!/usr/bin/env python into header of main.py

    问题2 spawn-fcgi child exited with 126

    solution: chmod +x main.py

    spawn-fcgi的常用参数说明

    -f 指定调用 FastCGI 的进程的执行程序位置,根据系统上所装的 PHP 的情况具体设置
    -a 绑定到地址 addr
    -p 绑定到端口 port
    -F 指定产生的 FastCGI 的进程数  (很多人以为是-C,其实那是PHP专用的,这里要用-F)
    -P 指定产生的进程的 PID 文件路径
    -u 和 -g FastCGI 使用什么身份运行

  • 相关阅读:
    Silverlight文本元素—高级修饰
    C#常用集合总结2
    Silverlight图片处理——(伸展,裁剪,蒙版)
    选择“Asp.Net Web应用程序”还是“Asp.Net网站”?
    HTML5能给软件初学者带来什么呢?
    性格的弱点
    (原)jvoiplib中的examples的编译和运行
    开源的好东西
    C++编绎器编绎C语言的问题
    gcc生成静态库和动态库(转自http://blog.csdn.net/ast_224/archive/2009/03/13/3988244.aspx)
  • 原文地址:https://www.cnblogs.com/ggjjl1/p/4145377.html
Copyright © 2020-2023  润新知