Django部署:Django+gunicorn+Nginx环境的搭建

本人的服务器环境为Ubuntu14.04,使用的是Python3.4版本,而且安装有pip(Ubuntu中Python3配合的是pip3),而且以管理员身份运行,若是是普通用户,请切换管理员权限(sudo)。 html

一.gunicorn和nginx的简介

gunicorn须要搭配nginx使用,那么二者的做用究竟是什么。 python

1.gunicorn简介:gunicorn是一个Python WSGI UNIX服务器。WSGI(Web Server Gateway Interface)是Web服务网关接口,位于WEB应用层和WEB服务器层之间。在这里WEB应用固然是指Python解释器及Django编写的程序,而WEB服务器指的是Nginx,因此Gunicorn位于二者之间。 linux

2.Nginx简介:Nginx是反向代理服务器,接收外部Internet网络请求,并将请求转发给内部网络的WSGI,并将响应信息反馈给外部Internet用户。因此gunicorn就是起到沟通做用,将Django和Nginx联系起来,也就是咱们说的网关做用。 nginx

二.Django,Gunicorn,Nginx的安装

在这里咱们使用的是Django最新版本1.8.4,固然,你也能够选择其余版本。 web

#pip3 install Django==1.8.4 shell

#pip3 install gunicorn django

#apt-get install python-dev nginx 服务器

三.环境搭建与测试

1.建立Django项目 网络

建立mysite项目 session

#django-admin.py startproject mysite

#cd mysite/

建立home应用

# python3 manage.py startapp home

2.设置setting.py文件

添加app,须要添加gunicorn和home(你本身设置的应用名)两项。

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # add app
    'bugapp',
    'gunicorn',
 )

3.运行gunicorn

#nohup gunicorn mysite.wsgi:application -b 127.0.0.1:1010&

nohup是后台运行指令,详细请查看:《nohup-真正的shell后台运行

固然,你也能够参考《鸟哥的linux私房菜(第三版)》,第17章第2节《脱机管理问题》

运行后可tail查看nohup.out文件,gunicorn的运行状况会输出到这个文件中。

4.查看端口

#netstat -plnt

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      5414/nginx
tcp        0      0 127.0.0.1:1010          0.0.0.0:*               LISTEN      5131/python3
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      30237/sshd

很明显,gunicorn已经运行起来了,使用curl测试一下:

#curl 127.0.0.1:1010

这时候若是返回一个HTML页面,说明gunicorn运行成功。接下来就是将gunicorn的WEB数据发送到发向代理服务器Nginx了,并由它发布到Internet上接收访问。

4.配置Nginx

打开Ngnix配置文件/etc/nginx/site-available/default文件,建议提早将原文件作个备份。将该文件修改为一下内容:

  1 server{
  2     listen 80;
  3
  4     server_name www.edse.cn;
  5     location / {
  6         proxy_pass http://127.0.0.1:1010;
  7         proxy_set_header Host $host;
  8         proxy_set_header X-Real-IP $remote_addr;
  9         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 10     }
 11
 12     location /static/{
 13         root /data/testweb;
 14     }
 15     location /media/{
 16         root /data/testweb;
 17     }
 18 }

保存后测试一下配置文件:


# nginx -t
nginx: [warn] conflicting server name "www.edse.cn" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

OK了!

5.最后重启服务器

#service nginx restart

四.Gunload在代码变动后的reload问题

修改过Django代码后,须要从新创建gunicorn链接,不然页面仍是维持代码变动前的状态。固然咱们能够采起两种方法:

1.你能够KILL掉gunicorn进程,而后从新创建。可是费时费力,太麻烦,若是你在测试代码频繁变动,那无疑是巨大的工做量。

2.其实在gunicorn的19.0版本后已经加入了--reload选项,只要在运行gunicorn添加进去就能够了。以下:

#nohup gunicorn mysite.wsgi:application -b 127.0.0.1:1010 --reload&

加入--reload字段,就能够实时显示变动代码了。


参考文献:

《django 部署,gunicorn、virtualenv、nginx》

《Django 部署(nginx)

《gunicorn autoreload on source change》

相关文章
相关标签/搜索