部署uWSGI+Nginx+Django项目
Django虽然可以通过runserver的方式启动,但是这样启动的是单进程的一个Django服务,所以显然单进程的manage.py是比不过web服务器uwsgi,同时再搭配上Nginx做静态文件转发和动态请求转发就无敌了。
配合uWSGI
安装uwsgi
apt-get install python-dev #不安装这个,下面的安装可能会失败
pip install uwsgi
配置uWSGI和Django
原理
the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django
基本测试
创建test.py文件,添加源码如下:
# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return ["Hello World"] # python2
#return [b"Hello World"] # python3
然后,Run uWSGI:
uwsgi --http :8000 --wsgi-file test.py
参数含义:
http :8000
: 使用http协议,8000端口wsgi-file
test.py: 加载指定文件 test.py
打开http://localhost:8000
,浏览器上应该显示hello world
。这说明一下环节是通的
the web client <-> uWSGI <-> Python
用UWSGI拉起Django
现在我们将在路径为/home/xx_user/path/to/project_name(以下统称projectpath)的项目中,用uwsgi来拉起这个项目服务。
- 在projectpath下建立wsgi.py文件(路径为/home/xx_user/path/to/project_name/wsgi.py),内容为
"""
WSGI config for datacenter project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/
"""
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
- 在命令行执行以下命令
uwsgi --http :8001 --chdir /home/xx_user/path/to/project_name --module wsgi
* --http 表示起在哪个端口上
* --chdir 表示在哪个路径下寻找wsgi配置文件
* --module 表示加载哪个模块
- 在浏览器访问
http://localhost:8001
可以看到服务已经起来了。
其实主要目的是通过 --chdir 告诉 uwsgi 去哪个地方找 wsgi.py 文件,同时,在 wsgi.py 中指明 settings.py 文件在哪里,这里的 settings.py 和 wsgi.py 文件在同一级目录里,所以 uwsgi 能找到对应的文件来加载设置,从而拉起服务。
配置Nginx
- 新建一个Nginx配置文件test.conf,内容如下。
upstream art {
server 127.0.0.1:8001;
}
server {
listen 80;
charset utf-8;
client_max_body_size 75M;
location / {
root /home/xx_user/path/to/project_nam;
uwsgi_pass art;
include /home/xx_user/path/to/project_nam/uwsgi_params;
}
location /media {
alias /home/xx_user/path/to/project_nam/media; # your Django project's media files - amend as required
}
location /static {
alias /home/xx_user/path/to/project_nam/static; # your Django project's static files - amend as required
}
uwsgi_params
可以从/etc/nginx/
下拷贝一份,也可以从这个页面下载。upstream art
表示有一个上游 art 用以处理动态请求,其中有一个server
起在127.0.0.1:8001
。server
核心内容是listen
表示Nginx监听哪个端口location /
表示动态请求通过uwsgi_pass
转发到上游art
。/media
表示从文件系统获取media文件,static同理,注意文件路径不要以为不需要加上media。
测试Nginx
sudo /etc/init.d/nginx restart
执行下面代码测试能否让nginx在页面上显示hello, world
uwsgi --socket :8001 --wsgi-file test.py
如果显示正确说明以下环节是通的
the web client <-> the web server <-> the socket <-> uWSGI <-> Python
用UNIX socket取代TCP port
对test.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)
重启Nginx并运行UWSGI
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666
打开页面查看是否正常。
如果上面一切都显示正常,则下面命令可以拉起django application
uwsgi --socket mysite.sock --module wsgi --chmod-socket=666
如果有问题,可以查看Nginx的log,位于/var/log/nginx/
用UWSGI和Nginx运行Django应用
为了不用每次都运行UWSGI命令拉起Django application,使用.ini文件能简化工作。
在项目根目录建立 mysite_uwsig.ini
,填入并修改以下内容。
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /home/xx_user/path/to/project_name
# Django's wsgi file
module = 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 = /home/xx_user/path/to/project_name/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 666
# clear environment on exit
现在,只要执行以下命令,就能够拉起 Django application。
uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file
编辑文件/etc/rc.local
, 添加下面内容到这行代码之前exit 0
/usr/local/bin/uwsgi --ini /path/to/mysite_uwsgi.ini --chmod-socket=666
可以每次机器重启时都重新启动服务。