Django的部署能够有不少方式,采用nginx+uwsgi的方式是其中比较常见的一种方式。html
在这种方式中,咱们的一般作法是,将nginx做为服务器最前端,它将接收WEB的全部请求,统一管理请求。nginx把全部静态请求本身来处理(这是NGINX的强项)。而后,NGINX将全部非静态请求经过uwsgi传递给Django,由Django来进行处理,从而完成一次WEB请求。前端
可见,uwsgi的做用就相似一个桥接器。起到桥梁的做用。python
Linux的强项是用来作服务器,因此,下面的整个部署过程咱们选择在Ubuntu下完成。nginx
2.1 准备知识web
django数据库
一个基于python的开源web框架,请确保本身熟悉它的框架目录结构。
uWSGIdjango
一个基于自有的uwsgi协议、wsgi协议和http服务协议的web网关
nginx后端
经常使用高性能代理服务器
wsgi.pytomcat
django项目携带的一个wsgi接口文件 若是项目名叫destiny的话,此文件就位于[destiny/destiny/wsgi.py]
服务器上的nginx已经安装好了,在/etc/nginx 目录下,部署本身的django程序时,最好写本身的conf配置文件,而后按照本身的配置文件启动,配置文件写法下面会说。安全
首先要在本身的目录下安装anaconda环境,安装包在 /home/updatefiles 目录下,是一个sh文件,启动安装一路回车便可,而后为本身的项目建造虚拟环境,具体作法看这里,建造虚拟环境后,安装你的程序对应各类包,例如django-1.11.2等等,最后在虚拟环境中,使用pip安装uwsgi
上面的工做都完成了,最后在虚拟环境中,使用pip安装uwsgi
pip install uwsgi
测试uWSGI: 新建文件test.py,写入如下内容
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return "Hello World"
运行
uwsgi --http 0.0.0.0:8000 --wsgi-file test.py
若是端口占用,使用
lsof -i :8000
列出占用端口的程序的pid号,并使用如下命令杀掉全部占用端口的程序
sudo kill -9 pid
而后浏览 http://127.0.0.1:8000(或http://内网ip:8000、或http://外网ip:8000)查看效果,有”Hello World”输出即安装成功。
首先肯定你的django程序没什么问题了,再开始部署流程,每每会有但不只限于如下问题:
setting.py里debug没关
setting.py里数据库信息没有更换为服务器数据库信息
setting.py里没有设置allow_host
setting.py里没有设置template或static目录信息
首先,建议工程目录采起这样的结构
|--工程 | |--Django程序 |--setting
在本身的工程目录下,创建两个文件夹,一个是django程序,一个是setting,用来装部署配置文件。
在setting中创建如destiny.ini的配置文件
下面是个例子(以图书管理系统为例,把该改的地方改为你本身的)
# backend_uwsgi.ini file [uwsgi] # Django-related settings # the base directory (full path) chdir = /home/yangtz/booksManage # Django's wsgi file module = booksManage.wsgi # process-related settings # master master = true # maximum number of worker processes processes = 10 # the socket (use the full path to be safe),将socket写入sock文件,安全又便捷 socket = /home/yangtz/booksManage/booksManage/booksManage.sock # ... with appropriate permissions - may be needed chmod-socket = 666 # clear environment on exit vacuum = true daemonize = /home/yangtz/booksManage/log/uwsgi.log
在setting中创建如destiny.conf的配置文件,
下面是个例子(以图书管理系统为例,把该改的地方改为你本身的)
user www-data; worker_processes auto; pid /run/nginx.pid; # 若是是非root用户,user设为nobody events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## # backend_nginx.conf # the upstream component nginx needs to connect to upstream bookmanager { server unix:/home/yangtz/booksManage/booksManage/booksManage.sock; #读取uwsgi生成的socket文件中的socket信息 # server 127.0.0.1:8000; # sever信息,有上边sock文件,这个就能够注掉了 } # configuration of the server,最重要的部分 server { # 你的服务站点挂载的端口号 listen 4020; # the domain name it will serve for server_name 192.168.100.18; # 通常写你服务器的ip或你的域名 charset utf-8; # access_log /home/yangtz/webback/backend/log/nginx_access.log; # error_log /home/yangtz/webback/backend/log/nginx_error.log; # max upload size client_max_body_size 75M; # adjust to taste # Finally, 设置总目录,实际就是映射到uwsgi的socket location / { uwsgi_pass bookmanager; include /etc/nginx/uwsgi_params; # the uwsgi_params file you install } # 若是有media或者静态文件须要挂载就加上,先后端各自分开的,不加或者注掉就行 # Django media # location /media { # alias /path/to/your/mysite/media; # your Django project's media files - amend as required # } # location /static { # alias /home/yangtz/webback/backend/static; # your Django project's static files - amend as required # } } }
最后启动uwsgi和nginx
uwsgi --ini /×××/你的工程/setting/destiny.ini
nginx -c /×××/你的工程/setting/destiny.conf (若是权限问题,你是root的话,能够在前面加sudo)
若是是非root用户,不能listen 1024如下的端口。
那么只好,在你的conf文件中配置user nobody; listen <1024+>;
舒适提示:
使用 lsof -l 查看端口占用
kill 进程号 (杀掉进程,只能kill本身的进程)
前端代码部署只需将包含前端代码文件夹放到 tomcat/webapps下,而后到tomcat/bin里启动 ./startup.sh便可