Django的部署能够有不少方式,采用nginx+uwsgi的方式是其中比较常见的一种方式。今天在Ubuntu上使用Nginx部署Django服务,虽然不是第一次搞这个了,可是发现仍是跳进了好多坑,google了很久才搞定。想一想仍是把这个过程记录下来,省得下次再来踩一样的坑。html
apt-get install nginx
ubantu安装完Nginx后,文件结构大体为:
全部的配置文件都在 /etc/nginx下;
启动程序文件在 /usr/sbin/nginx下;
日志文件在 /var/log/nginx/下,分别是access.log和error.log;
而且在 /etc/init.d下建立了启动脚本nginx。前端
sudo /etc/init.d/nginx start # 启动 sudo /etc/init.d/nginx stop # 中止 sudo /etc/init.d/nginx restart # 重启
apt-get install python-dev pip install uwsgi
至于为何要使用uwsgi,能够参见这边博客:快速部署Python应用:Nginx+uWSGI配置详解(1)。
这样大致的流程是:nginx做为服务器最前端,负责接收client的全部请求,统一管理。静态请求由Nginx本身处理。非静态请求经过uwsgi传递给Django,由Django来进行处理,从而完成一次WEB请求。
通讯原理是:
the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django
python
在Django项目下新建test.py文件,nginx
# test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return ["Hello World"] # python2 #return [b"Hello World"] # python3
而后执行shell命令:web
uwsgi --http :8001 --plugin python --wsgi-file test.py
加上--plugin python是告诉uWSGI在使用python插件,否则颇有可能会出现相似这样的错误:shell
uwsgi: unrecognized option '--wsgi-file' getopt_long() error
执行成功在浏览器中打开:http://localhost:8001显示Hello World说明uwsgi正常运行。django
首先得保证Django项目没有问题ubuntu
python manage.py runserver 0.0.0.0:8001
访问http://localhost:8001,项目运行正常。
而后连接Django和uwsgi,实现简单的web服务器,到Django项目目录下执行shell:segmentfault
uwsgi --http :8001 --plugin python --module blog.wsgi
blog为你的项目名。访问http://localhost:8001,项目正常。注意这时项目的静态文件是不会被加载的,须要用nginx作静态文件代理。浏览器
uwsgi支持经过配置文件的方式启动,能够接受更多的参数,高度可定制。咱们在Django项目目录下新建uwsgi.ini
# Django-related settings socket = :8001 # the base directory (full path) chdir = /home/ubuntu/blog # Django s wsgi file module = blog.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
在shell中执行:
sudo uwsgi --ini uwsgi.ini
ps:若是实在不想配置nginx的话,单uwsgi就已经能完成部署了(把socket换成http),你能够把Django中的静态文件放到云平台中如七牛等等,这样你的Web也能被正常访问。
nginx默认会读取/etc/nginx/sites-enabled/default
文件中的配置,修改其配置以下:
server { # the port your site will be served on listen 80; # the domain name it will serve for server_name 127.0.0.1; # 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 /home/ubuntu/blog/media; # your Django project's media files - amend as required } location /static { alias /home/ubuntu/blog/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { include uwsgi_params; # the uwsgi_params file you installed uwsgi_pass 127.0.0.1:8001; } }
把Django自带的静态文件收集到同一个static中,否则访问Django的admin页面会找不到静态文件。在django的setting文件中,添加下面一行内容:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
而后到项目目录下执行:
python manage.py collectstatic
修改配置文件
DEBUG = False ALLOWED_HOSTS = ['*']
一切配置好后直接重启nginx便可。更加详细的说明请参见官方文档
若是监听80端口,部署后访问localhost自动跳转到nginx默认的欢迎界面
uwsgi: option ‘--http‘ is ambiguous