centos6 python3 django-uwsgi-nginx 环境搭建02-之uWSGI

1.安装 uwsgi

pip3 install uwsgi

2.第一个wsgi程序,首先创建个foobar.py文件

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

3.在8000端口上面部署(前一章已经把8000端口对外开放了)

在foobar.py所在目录运行下面的语句

uwsgi --http :8000 --wsgi-file foobar.py

然后通过浏览器访问服务器的8000端口:

4.多进程,线程配置

可以通过--processes 和 --threads 来配置多个进程和线程

uwsgi --http :9090 --wsgi-file foobar.py --master --processes 4 --threads 2

这个会创建4个进程,每个进程有2个线程,还有一个主进程(在其他进程挂掉后,会重新启动挂掉的进程)

一个http的router

5.镜像--暂时没用到

One important task is monitoring. Understanding what is going on is vital in production deployment. The stats subsystem allows you to export uWsgi’s internal statistics as JSON:

uwsgi --http :9090 --wsgi-file foobar.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191

Make some request to your app and then telnet to the port 9191,you’ll get lots of fun information. You may want to use “uwsgitop” (justpipinstallit),which is a top-like tool for monitoring instances.

6.部署在Django上面

django的应用,前一章已经创建过了,在那个应用的基础上来进行部署

manger.py所在目录

uwsgi --http ***.***.***.***:8000 --chdir /home/aslan/myblog/ --wsgi-file myblog/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:8080

通过浏览器访问8000端口可以正常访问:

uwsgi --socket ***.***.***.***:8000 --chdir /home/aslan/myblog/ --wsgi-file myblog/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:8080

--socket和 --http的区别是:

???

命令行太长了,官方也给出了一个简单的方式就是使用配置文件

创建一个user.ini文件

[uwsgi]
http = ***.***.***.***:8000
chdir = /home/aslan/myblog/
wsgi-file = myblog/wsgi.py  #这个文件是django创建应用时自动创建的
processes = 4
threads = 2
stats = 127.0.0.1:9001

然后通过下面的命令运行:

uwsgi user.ini

可以实现和上面命令行一样的效果

/home/aslan/myblog/myblog/wsgi.py是Django创建应用时创建的,如果你的应用里面没有,说明你的Django的版本比较低了 < 1.4。现在我使用的是django 1.10

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native