参考文章:

http://kb.cnblogs.com/page/86907/ (配置Nginx+uwsgi更方便地部署python应用)

http://projects.unbit.it/uwsgi/wiki/VirtualHosting (uWSGI VirtualHosting Mode)

http://uwsgi-docs.readthedocs.org/en/latest/Configuration.html (Configuring uWSGI)



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

uwsgi -M -p 2 -t 30 --limit-as 128 -R 10000 -x /usr/local/nginx/conf/uwsgi.xml -d /tmp/uwsgi.log --vhost

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

easy_install virtualenv

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

virtualenv /www/fabric-bolt/

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

source /www/fabric-bolt/
pip install django
pip install mako
...

最后配置nginx,注意每个站点必须单独占用一个server

    server {
        listen       80;
        server_name  app1.blogex.com;

        location / {
            root /www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt;
            include   uwsgi_params;
            uwsgi_pass  192.168.1.121:9001;
            uwsgi_param UWSGI_PYHOME /www/fabric-bolt;
            uwsgi_param UWSGI_CHDIR /www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt;
            uwsgi_param UWSGI_SCRIPT wsgi;
            access_log /usr/local/nginx/logs/access.log;
        }
        location /static/ {
            alias /www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt/work/myweb/static/;
            index index.html;
}

}


        server {
            listen       80;
            server_name  app2.blogex.com;

            location / {
                root /www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt/work;
                include   uwsgi_params;
                uwsgi_pass  192.168.1.121:9001;
                uwsgi_param UWSGI_PYHOME /www/fabric-bolt;
                uwsgi_param UWSGI_CHDIR /www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt/work;
                uwsgi_param UWSGI_SCRIPT django_wsgi;
                access_log /usr/local/nginx/logs/access.log;
        }
}


uwsgi.xml的配置


/usr/local/nginx/conf/uwsgi.pid
192.168.1.121:9001
4
/www/fabric-bolt/lib/python2.7/site-packages
/www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt
/www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt/work
/tmp/uwsgi.log


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


结语:

上篇文章配置好了dns服务器,这里分别访问nginx虚拟主机app1.blogex.com跟app2.blogex.com,可以看到django应用:

nginx+uwsgi部署多个python应用_第1张图片


nginx+uwsgi部署多个python应用_第2张图片