第47章 Python uWSGI 安装配置教程

本文主要介绍如何部署简单的 WSGI 应用和常见的 Web 框架。

以 Ubuntu/Debian 为例,先install 依赖包:


    apt-get install build-essential python-dev

Python install uWSGI

1、通过 pip 命令:


    pip install uwsgi

2、download install 脚本:


    curl http://uwsgi.it/install | bash -s default /tmp/uwsgi

将 uWSGI 二进制install 到 /tmp/uwsgi ,可以修改它。

3、源代码install :


    wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz
    tar zxvf uwsgi-latest.tar.gz
    cd uwsgi-latest
    make

install 完成后,在当前目录下,会获得一个 uwsgi 二进制文件。


第一个 WSGI 应用

让咱们从一个简单的 “Hello World” 开始,创建文件 foobar.py,代码如下:


    def application(env, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return [b"Hello World"]

uWSGI Python 加载器将会搜索的默认函数 application 。

接下来咱们启动 uWSGI 来运行一个 HTTP server ,将程序部署在HTTP端口 9090 上:


    uwsgi --http :9090 --wsgi-file foobar.py

添加并发和监控

默认情况下,uWSGI 启动一个单一的进程和一个单一的线程。

可以用 --processes 选项添加更多的进程,或者使用 --threads 选项添加更多的线程 ,也可以两者同时使用。


    uwsgi --http :9090 --wsgi-file foobar.py -

你可能感兴趣的:(Python,python,开发语言,linux)