Django的部署能够有不少方式,采用nginx+uwsgi的方式是其中比较常见的一种方式。
在这种方式中,咱们的一般作法是,将nginx做为服务器最前端,它将接收WEB的全部请求,统一管理请求。nginx把全部静态请求本身来处理(这是NGINX的强项)。而后,NGINX将全部非静态请求经过uwsgi传递给Django,由Django来进行处理,从而完成一次WEB请求。
可见,uwsgi的做用就相似一个桥接器。起到桥梁的做用。html
Linux的强项是用来作服务器,因此,下面的整个部署过程咱们选择在Ubuntu下完成。前端
Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。linux
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特色是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。nginx
打开ubuntu控制台(ctrl+alt+t)利用Ubuntu的仓库安装。web
linux@ubuntu:~$ sudo apt-get install nginx #安
启动Nginx:django
linux@ubuntu:~$ /etc/init.d/nginx start #启动 linux@ubuntu:~$ /etc/init.d/nginx stop #关闭 linux@ubuntu:~$ /etc/init.d/nginx restart #重启
修改Nginx默认端口号,打开/etc/nginx/nginx.conf 文件,修改端口号。ubuntu
server { listen 8000; server_name 127.0.0.1 access_log /var/log/nginx/myblog_access.log; error_log /var/log/nginx/myblog_error.log; charset utf-8; client_max_body_size 75M; root /home/linux/Desktop/MyBlog; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:9001; uwsgi_read_timeout 2; } location /static/ { expires 30d; autoindex on; add_header Cache-Control private; alias /home/linux/Desktop/MyBlog/static/; } }
经过上面命令重启nginx。服务器
linux@ubuntu:~$ sudo pip install uwsgi
测试uwsgi 写一个test.py文件并发
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return “HelloWorld”
linux@ubuntu:~$ uwsgi --http :8001 --wsgi-file test.py
运行上面命令,app
在咱们经过Django建立myweb项目时,在子目录myweb下已经帮咱们生成的 wsgi.py文件。因此,咱们只须要再建立myblog_uwsgi.ini配置文件便可,固然,uwsgi支持多种类型的配置文件,如xml,ini等。此处,使用ini类型的配置。
[uwsgi] # Django-related settings
#socket 指定项目执行的端口号。 socket = 127.0.0.1:9001 # 项目绝对路径 chdir = /home/linux/Desktop/MyBlog # Django的wsgi文件相对路径 wsgi-file = MyBlog/wsgi.py # process-related settings # master master = True # 最大进程数 processes = 4 # 线程数 threads = 2 #设置此参数,有一个主进程 master=True #守护进程的方式运行,log日志存在此log文件里 deamonize=/var/log/uwsgi/djangoProject.log #主进程id写入文件里 pidfile= /var/log/nginx/uwsgi.pid # ... with appropriate permissions - may be needed # chmod-socket = 664 #退出时,清理环境 vacuum = True reload-mercy = 10 max-requests = 5000 limit-as = 512 buffer-size = 30000
接下来,切换到myweb项目目录下,经过uwsgi命令读取myblog_uwsgi.ini文件启动项目。
.......................... Python main interpreter initialized at 0x8b52dc0 your server socket listen backlog is limited to 100 connections your mercy for graceful operations on workers is 60 seconds mapped 319920 bytes (312 KB) for 4 cores *** Operational MODE: preforking *** WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x8b52dc0 pid: 7158 (default app) *** uWSGI is running in multiple interpreter mode *** spawned uWSGI master process (pid: 7158) spawned uWSGI worker 1 (pid: 7160, cores: 1) spawned uWSGI worker 2 (pid: 7161, cores: 1) spawned uWSGI worker 3 (pid: 7162, cores: 1) spawned uWSGI worker 4 (pid: 7163, cores: 1)
注意查看uwsgi的启动信息,若是有错,就要检查配置文件的参数是否设置有误。
listen 指定的是nginx代理uwsgi对外的端口号。
include uwsgi_params;
uwsgi_pass 127.0.0.1:9001;
include 必须指定为uwsgi_params;而uwsgi_pass指的本机IP的端口与myweb_uwsgi.ini配置文件中的必须一致。
而后访问主机ip 加上设置的端口8000 127.0.0.1:8000 就能够了