一、安装uwsgi

通过pip安装uwsgi。

pip install uwsgi

测试uwsgi,创建test.py文件:

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

通过uwsgi运行该文件。

uwsgi --http :8001 --wsgi-file test.py

常用选项:

http : 协议类型和端口号

processes : 开启的进程数量

workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)

chdir : 指定运行目录(chdir to specified directory before apps loading)

wsgi-file : 载入wsgi-file(load .wsgi file)

stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)

threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)

master : 允许主进程存在(enable master process)

daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。

pidfile : 指定pid文件的位置,记录主进程的pid号。

vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)

二、安装nginx

sudo apt-get install nginx  #安装

  启动Nginx:

/etc/init.d/nginx start  #启动
/etc/init.d/nginx stop  #关闭
/etc/init.d/nginx restart  #重启

三、Django部署

在我们用python manager.py startproject myproject创建项目时,会自动为我们生成wsgi文件,所以,我们现在之需要在项目目录下创建uwsgi的配置文件即可,我们采用ini格式:

# myweb_uwsgi.ini file
[uwsgi]
 
# Django-related settings
 
socket = :8000
 
# the base directory (full path)
chdir           = /mnt/myproject
 
# Django s wsgi file
module          = myproject.wsgi
 
# process-related settings
# master
master          = true
 
# maximum number of worker processes
processes       = 4
 
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true
 
daemonize       = /mnt/myproject/uwsgi_log.log
 
pidfile = /mnt/myproject/uwsgi_pid.log

再接下来要做的就是修改nginx.conf配置文件。打开/etc/nginx/nginx.conf文件,http中添加如下内容。

server {
    listen         8099;
    server_name    127.0.0.1
    charset UTF-8;
    access_log      /var/log/nginx/myweb_access.log;
    error_log       /var/log/nginx/myweb_error.log;
 
    client_max_body_size 75M;
 
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8000;
        uwsgi_read_timeout 2;
    }  
    location /static {
        expires 30d;
        autoindex on;
        add_header Cache-Control private;
        alias /mnt/myproject/static/;
     }
 }

listen 指定的是nginx 对外的端口号。

server_name  设置为域名或指定的到本机ip。

nginx通过下面两行配置uwsgi产生关联:

include uwsgi_params;  
uwsgi_pass 127.0.0.1:8000; //必须与uwsgi中配置的端口一致

最后我们在项目目录下执行下面的命令来启动关闭我们的项目:

 #启动
uwsgi --ini uwsgi.ini
/etc/init.d/nginx start #停止
uwsgi --stop uwsgi_pid.log
/etc/init.d/nginx stop

好了 ,现在我们可以访问127.0.0.1:8099即可看到我们自己的项目了

最新文章

  1. 对于有了ACM以后的生活
  2. fir.im Weekly - 聊聊让人向往的远程开发工作
  3. 利用IdentityServer3在ASP.NET 5和Angular中实现OAuth2 Implicit Flow
  4. IIS假死状态处理
  5. [IIS] eset/Restart/Recycling/Refresh 的区别
  6. 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)
  7. C# 与 C++ 数据类型对照
  8. HotSpot Builder Utility安装指南
  9. BeanFactory和FactoryBean
  10. [React Testing] Reusing test boilerplate
  11. vs2010中看不见类视图和资源视图的解决方法
  12. .NET截断字符串
  13. zTree模糊查询人员姓名:getNodesByParamFuzzy
  14. Microsoft Visual Studio 2012正式版官方下载
  15. python selenium2 中的显示等待WebDriverWait与条件判断expected_conditions举例
  16. python MRO:C3算法
  17. C++对象模型复习
  18. JavaScript的内置对象(Global对象)
  19. Baidu 人脸识别FireFly 与PC连接调试
  20. 路由网关---zuul

热门文章

  1. Access restriction: The type TaskTopicResolver is not accessible due to restrict
  2. Html5-canvas
  3. list内容按长度等分
  4. IOS导航栏颜色渐变与常用属性
  5. Spark MLlib Data Type
  6. http 请求详解大全
  7. Mac下使用firefoxdriver
  8. 1.6jdk + eclipse + pydev搭建Python开发环境
  9. Pycharm中使用GitHub
  10. 开源BTS产品中存在多处漏洞,攻击者或可劫持手机通讯基站