一般项目会部署在虚拟环境,虚拟环境的使用能够参考这里,点击前往html
固然你也能够直接部署,这里很少说。python
pip install uwsgi
这里只说明了一种安装方式,其余安装方式能够参考官网,点击跳转。nginx
# 建立一个名为test.py 的文件git
# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3版本
#return ["Hello World"] # python2版本
# 在HTTP端口9090上进行部署github
uwsgi --http:8000 --wsgi-file test.py
选项说明:web
如今再去访问,我这里的地址为http://192.168.10.165:8000/就能够看到Hello World 信息django
若是是这样,工做流程为 网络客户端 <--> uWSGI <--> Pythonvim
每个处于监听(Listen)状态的端口,都有本身的监听队列.监听队列的长度,与以下两方面有关:浏览器
somaxconn参数和使用该端口的程序中listen()函数服务器
能够经过echo 1000 >/proc/sys/net/core/somaxconn 命令来修改系统的监听队列长度。而且在/etc/sysctl.conf文件中添加net.core.somaxconn = 1024 并sysctl -p生效。
经过 添加--listen num参数增长队列长度。
默认状况下,uWSGI以单进场和单线程开始的,你可使用--processes添加更多的进程,--threads添加更多的线程
uwsgi --http :9090 --wsgi-file test.py --master --processes 4 --threads 2
查看uwsgi状态:
统计子系统容许将uWSGI的内部统计信息导出为JSON
uwsgi --http :9090 --wsgi-file test.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
请求您的应用程序,而后Telnet到端口9191,你会获得不少关系uwsgi的相关信息。
使用“uwsgitop”工具能够来监控实例
须要使用pip install uwsgitop 来安装uwsgitop
注意:将统计套接字绑定到私人地址(除非你知道你在作什么),不然每一个人均可以访问它!
uwsgi的使用还有其余不少的选项,查看更多点击这里。
如今咱们但愿uWSGI作一样的事情,可是运行Django站点而不是test.py 模块。
首先咱们来建立一个django 站点,并启动测试该站点是否能够正常访问:
source bin/activate # 进去python虚拟环境
pip install django # 安装django
django-admin startproject mysite # 新建站点
cd mysite/
vim mysite/settings.py # 配置ALLOWED_HOSTS为‘["*"]’,方便其余主机访问站点,也能够不修改
python manage.py runserver 0.0.0.0:8000 # 启动并测试可用性
在测试站点能够正常运行后,关闭站点,下面来使用uwsgi来运行django项目:
uwsgi --http :8000 --module mysite.wsgi
经过浏览器访问页面,若是页面出现,意味着uWSGI可以从你的virtualenv服务于你的django项目,而且正常运行,此时的工做流程为:
网络客户端 <--> uWSGI <--> Django
注意:
此时若是你的django须要访问静态文件,是访问不到的,须要利用uWSGI进行静态文件的配置,这里不做说明,由于通常也不须要uWSGI来处理站点的静态文件,
通常使用网络服务器来处理这些静态文件会更好,更高效。
一般不会让浏览器直接与uWSIG会话,这个是网络服务器的工做(这里为Nginx),uWSGI将做为一个中间件。
1.首先搭建Nginx环境
yum install nginx
/etc/init.d/nginx start # start nginx
nginx的配置问题这里不过多说明,避免与默认配置冲突,我选择新建vhost文件夹,在vhost下添加配置,而后导入到主配置文件中,这里使用8000端口。
新建一个Nginx的配置(mysite_nginx.conf)放入vhost中:(记得在Nginx主配置文件中导入该配置)
# mysite_nginx.conf # the upstream component nginx needs to connect to upstream django { # server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0.0.1:8001; # for a web port socket (we'll use this first) } # configuration of the server server { # the port your site will be served on listen 8000; # the domain name it will serve for server_name .example.com; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media location /media { alias /path/to/your/mysite/media; # your Django project's media files - amend as required } location /static { alias /path/to/your/mysite/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed } }
# 这里的配置须要进行修改,路径为你项目路径
这个conf文件告诉nginx从文件系统提供媒体和静态文件,以及处理须要Django干预的请求。对于大型部署来讲,让一台服务器处理静态/媒体文件以及另外一个处理Django应用程序的
作法被认为是很好的作法,可是如今这样作会很好。
为了方便对文件的查看和修改,建议把配置文件创建一个软链接到项目目录下。
sudo ln -s /etc/nginx/sites-enabled/mysite_nginx.conf /path/to/your/mysite/
2.部署静态文件
在运行Nginx以前,必须在静态文件夹中收集全部的django静态文件,须要编辑mysite/settings.py 添加:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
# and then run
python manage.py collectstatic
这里须要注意一点,配置的静态文件夹和django的静态文件夹不能为一个。
3.检查静态文件配置
在启动Nginx是会报错,那是由于配置文件在的 include /path/to/your/mysite/uwsgi_params;
须要导入这个文件, 这个文件通常在Nginx uwsgi发行版的目录中,或从这里获取https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
启动后检查是否能够获取静态文件。
1.使用uWSGI启动一个socket ,使nginx能够转发到uWSGI
uwsgi --socket:8001 --wsgi-file test.py
nginx同时被配置为与该WSGI在该端口上通讯,并与外界在8000端口进行通讯
这是当前的堆栈:
web客户端 <--> web服务器 <--> 套接字 <--> uWSGI <--> pyhton(test.py)
2.使用Unix套接字实现
编辑mysite_nginx.conf,使用套接字来实现:
server unix:///path/to/your/mysite/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
这个socket
选项告诉uWSGI使用哪一个文件进行套接字链接。
若是不行:
检查Nginx错误日志 nginx/error.log 若是你看到
connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permissiondenied)
那么可能你须要管理套接字的权限,以便运行nginx使用它。
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 # (verypermissive)
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664 # (moresensible)
你可能还须要将用户添加到nginx的组里,以便nginx能够正确的读取和写入套接字
来运行咱们的Django应用程序:
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664 # 或者是666
如今uWSGI和nginx不只仅是一个“Hello World ” ,而是个Django项目。
经过ini配置文件运行uWSGI
uwsgi支持经过各类的配置文件来启动服务,这里说一个ini配置文件
建立一个名为mysite_uwsgi.ini:
# mysite_uwsgi.ini file [uwsgi] # Django-related settings # the base directory (full path) chdir = /path/to/your/project # Django's wsgi file module = project.wsgi # the virtualenv (full path) home = /path/to/virtualenv # process-related settings # master master = true # maximum number of worker processes processes = 10 # the socket (use the full path to be safe socket = /path/to/your/project/mysite.sock # ... with appropriate permissions - may be needed # chmod-socket = 664 # 或者666 # clear environment on exit vacuum = true
并运行uwsgi:
uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file
测试站点是否正常,成功。。
退出Python虚拟环境使用:
deactivate
uWSGI能够运行在'皇帝'模式。在这种模式下,它会关注uWSGI配置文件的目录,并为每一个配置文件生成一个实例('vassals')。
每当修改配置文件时,皇帝将自动从新启动附庸。
# create a directory for the vassals sudo mkdir /etc/uwsgi sudo mkdir /etc/uwsgi/vassals # symlink from the default config directory to your config file sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/ # run the emperor uwsgi --emperor /etc/uwsgi/vassals --uid nginx --gid nginx
emperor
:在哪里寻找附庸(配置文件)uid
:进程一旦启动的用户IDgid
:进程一旦启动的组ID