nginx+uwsgi+web.py的部署

1. sudo apt-get install python-dev

5. sudo apt-get install python-webpy 

6. sudo apt-get install nginx uwsgi-extra

7. install uwsgi

 $ wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz

 $ tar zxvf uwsgi-latest.tar.gz

 $ cd uwsgi-latest/

 $ python uwsgiconfig.py --build

 $ sudo python setup.py install

 

nginx配置文件:/etc/nginx/nginx.conf

http {

.....

server {
    listen 8080;
    server_name localhost;
    
    location / {
        include uwsgi_params;
        uwsgi_param UWSGI_CHDIR /home/pi/www;
        uwsgi_param UWSGI_SCRIPT hello;
        uwsgi_pass 127.0.0.1:9002;
    }

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

} }

重新载入配置:

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

停止nginx:

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


-------------------------------------------------------------------------------

2. uwsgi的参数

以上是单个project的最简单化部署,uwsgi还是有很多令人称赞的功能的,例如

并发4个线程

uwsgi -s :9090 -w myapp -p 4

主控制线程+4个线程

uwsgi -s :9090 -w myapp -M -p 4

执行超过30秒的client直接放弃

uwsgi -s :9090 -w myapp -M -p 4 -t 30

限制内存空间128M

uwsgi -s :9090 -w myapp -M -p 4 -t 30 --limit-as 128

服务超过10000个req自动respawn

uwsgi -s :9090 -w myapp -M -p 4 -t 30 --limit-as 128 -R 10000

后台运行等

uwsgi -s :9090 -w myapp -M -p 4 -t 30 --limit-as 128 -R 10000 -d uwsgi.log

更多用法见文档:http://projects.unbit.it/uwsgi/wiki/Doc

3.为uwsgi配置多个站点

为了让多个站点共享一个uwsgi服务,必须把uwsgi运行成虚拟站点:去掉“-w myapp”加上”–vhost”

uwsgi -s :9090 -M -p 4 -t 30 --limit-as 128 -R 10000 -d uwsgi.log --vhost

然后必须配置virtualenv,virtualenv是python的一个很有用的虚拟环境工具,这样安装

apt-get install python-setuptools
easy_install virtualenv

然后设置一个/多个app基准环境

virtualenv /var/www/myenv

应用环境,在此环境下安装的软件仅在此环境下有效

source /var/www/myenv/bin/activate
pip install django
pip install mako
...

最后配置nginx,注意每个站点必须单独占用一个server,同一server不同location定向到不同的应用不知为何总是失败,我猜也算是一个bug。

   server {
        listen       80;
        server_name  app1.mydomain.com;
        location / {
                include uwsgi_params;
                uwsgi_pass 127.0.0.1:9090;
                uwsgi_param UWSGI_PYHOME /var/www/myenv;
                uwsgi_param UWSGI_SCRIPT myapp1;
                uwsgi_param UWSGI_CHDIR /var/www/myappdir1;
        }
    }
    server {
        listen       80;
        server_name  app2.mydomain.com;
        location / {
                include uwsgi_params;
                uwsgi_pass 127.0.0.1:9090;
                uwsgi_param UWSGI_PYHOME /var/www/myenv;
                uwsgi_param UWSGI_SCRIPT myapp2;
                uwsgi_param UWSGI_CHDIR /var/www/myappdir2;
        }
    }

如此这般,重启nginx服务,两个站点就可以共用一个uwsgi服务了。

4.实战应用

最初的设置完毕以后,再添加的应用,只需要在nginx里面进行少量修改,无需重启uwsgi,就能立刻部署完毕。uwsgi自带了基于django的监控uwsgi运行状态的工具,就拿它来部署好了:

server {
    listen 80;
    root   /var/www/django1.23;
    index  index.html index.htm;
    server_name uwsgiadmin.django.obmem.info;
    access_log  /var/log/nginx/django.access.log;
    location /media/ {
        root /var/www/django1.23/adminmedia;
        rewrite ^/media/(.*)$ /$1 break;
    }
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:9090;
        uwsgi_param UWSGI_PYHOME /var/www/django1.23/vtenv;
        uwsgi_param UWSGI_CHDIR /var/www/django1.23/uwsgiadmin;
        uwsgi_param UWSGI_SCRIPT uwsgiadmin_wsgi;
    }
}

于是uwsgi的监控信息可以在http://uwsgiadmin.django.obmem.info 看到用户名密码都是admin。

再比如LBForum论坛程序的部署:根据安装说明安装完毕,再按部署说明修改完配置文件,然后只需修改nginx配置文件:

server {
    listen 80;
    root   /var/www/django1.23;
    index  index.html index.htm;
    server_name lbforum.django.obmem.info;
    access_log  /var/log/nginx/django.access.log;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:9090;
        uwsgi_param UWSGI_PYHOME /var/www/django1.23/vtenv;
        uwsgi_param UWSGI_CHDIR /var/www/django1.23/LBForum/sites/default;
        uwsgi_param UWSGI_SCRIPT lbforum_wsgi;
    }
}

于是 http://lbforum.django.obmem.info 就是论坛程序了。

posted @ 2013-05-04 14:46  catmelo  阅读(519)  评论(0编辑  收藏  举报