Home > Backend Development > Python Tutorial > 在Linux系统上通过uWSGI配置Nginx+Python环境的教程

在Linux系统上通过uWSGI配置Nginx+Python环境的教程

WBOY
Release: 2016-06-10 15:06:53
Original
1230 people have browsed it

1.安装ubuntu有uwsgi的ppa:

add-apt-repository ppa:stevecrozz/ppa 
apt-get update 
apt-get install uwsgi 
Copy after login

2. 用uwsgi代替mod_wsgi

Nginx 的整体配置这里不说了,假设已经明白 Nginx的基本配置,那么uwsgi就类似这么配置:

  location / { 
   include uwsgi_params 
   uwsgi_pass 127.0.0.1:9090 
  } 
Copy after login

再比如django就是:

 ....... 
 from django.core.handlers.wsgi import WSGIHandler 
 application = WSGIHandler() 
Copy after login

然后运行uwsgi监听9090,其中-w后跟模块名,也就是刚才配置的myapp

 uwsgi -s :9090 -w myapp 
Copy after login

运行网站发现已经部署完成了。

3.uwsgi的参数

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

并发4个线程:

 uwsgi -s :9090 -w myapp -p 4 
Copy after login

主控制线程+4个线程:

 uwsgi -s :9090 -w myapp -M -p 4 
Copy after login

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

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

限制内存空间128M:

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

服务超过10000个req自动respawn:

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

后台运行等:

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

Copy after login

4.为uwsgi配置多个站点

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

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

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

 apt-get install Python-setuptools
 easy_install virtualenv 
Copy after login

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

 virtualenv /var/www/myenv 
Copy after login

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

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

最后配置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; 
     } 
   } 
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template