0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python 那些事儿 (二)

Last updated at Posted at 2016-05-26

mod_wsgi って

正如我们常说的,对于 mod_wsgi 来说,他对于 apache 来说就是一个 module.
而对于它的使用和配置,通常会有两种选择,

  1. 嵌入模式
  2. daemon 模式

第一个模式简单说就是它有关 python 的东西请求过来就🈶它负责了.而这个都是有 apache 的普通 worker 进程来处理的.

第二种就是 mod_wsgi 它自己本身会启动进程来处理请求, 当apache 服务器接到 python 的请求处理时,就委派给这些常驻进程来处理了, 这样做的好处有很多, 和 apahce 的 worker 进程分离,不需要重启 apache 当我们修改了 python 的脚本, 或者性能不会影响 apahce 本身因为往往一个 apahce 会负责好多子进程.

今天这里先来说说第一个嵌入模式, 其实这个很简单了. 就是

  1. 安装 apache 如果没有
  2. 安装 mod_wsgi 模块
  3. 配置 vhost( 假设我们要通过 vhost 的方式)
  4. 重启 apache

下面就是一个参考链接:

  1. http://www.cyberciti.biz/faq/linux-install-and-start-apache-httpd/
  2. https://www.digitalocean.com/community/tutorials/installing-mod_wsgi-on-ubuntu-12-04
  3. 这里算是本文的重点,当然只是相对来说,毕竟对于安装 apache 或者 mod_wsgi 来说都是很简单的.
<VirtualHost *:80>
    ServerName python.xiangzhuyuan.com:80

    WSGIScriptAlias / /var/www/flaskdemo1/flaskdemo1.wsgi

    <Directory /var/www/flaskdemo1>
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

How to setup Embedded mode

这里就是一个简单的嵌入模式了. 假设我们配置一个 python.xiangzhuyuan.com的子域名,
然后就是映射具体的 URL WSGI 文件. 关于如何书写这个 WSGI 文件, 这个主要是遵循PEP333接口定义,一个接受2个参数,返回一个可遍历结果的名为 application 的函数.
如最简单的 hello world 例子
:thumbsup:

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]
    

这个就是. 试着跑一下吧.

How to setup daemond mode

再看看怎么配这个吧,和刚才的类似,我们这样配:

<VirtualHost *:80>
    ServerName python.xiangzhuyuan.com:80

    WSGIDaemonProcess flaskdemo1 user=matt group=matt processes=2 threads=5
    WSGIProcessGroup flaskdemo1
    WSGIScriptAlias / /var/www/flaskdemo1/flaskdemo1.wsgi

    <Directory /var/www/flaskdemo1>
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

这里主要的变化就是:

    WSGIDaemonProcess flaskdemo1 user=matt group=matt processes=2 threads=5
    WSGIProcessGroup flaskdemo1

详细看 http://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html . 这具体的目的就是当配置为 daemon 模式之后, apache 会启动单独的进程用来处理 apache 委托给它的请求. 具体怎么启动这个进程, 起多少这些就在这里声明.

是不是很简单. 不过虽然它跑起来了, 而对于程序员来说, 还是有很多地方需要深入. 怎么调优 apache 和 mod_wsgi 之间的协作关系, 多少进程线程这都是需要优化的.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?