django 部署,gunicorn、virtualenv、nginx

声明:

1、本篇文章是我边写命令边写的,请尊重我的劳动成果,转载请加上链接。

2、我既然公开写出来,是希望大家遇到问题的时候有个参考,所以,大家可以免费转载,使用该文章

3、但是,如果你要用这篇文章来赚钱,sorry,你至少得分我点吧。

使用gunicorn 来部署django

django写代码简单,但部署是个难题,今天终于发现gunicorn 这个好东西,与大家一起分享。

环境:ubuntu 14.04 64bit + django 1.6.5 + virtualenv + gunicorn

一、创建虚拟环境

[email protected]:/home/www# pwd
/home/www

[email protected]:/home/www# virtualenv bugenv

New python executable in bugenv/bin/python
Installing setuptools, pip...done.

[email protected]:/home/www# ll
total 16
drwxr-xr-x 4 root root 4096 Jul 21 15:04 ./
drwxr-xr-x 5 root root 4096 Jul 21 12:34 ../
drwxr-xr-x 6 root root 4096 Jul 21 15:05 bugenv/
....................................(这一行是私人数据,这里不影响,所以不显示了)

二、进入虚拟环境

[email protected]:/home/www# cd bugenv/
[email protected]:/home/www/bugenv# source bin/activate
(bugenv)[email protected]:/home/www/bugenv#

三、下载django和gunicorn
pip install django==1.6.5
pip install gunicorn

 1 (bugenv)[email protected]:/home/www/bugenv# pip install django==1.6.5
 2 Downloading/unpacking django==1.6.5
 3   Downloading Django-1.6.5-py2.py3-none-any.whl (6.7MB): 6.7MB downloaded
 4 Installing collected packages: django
 5 Successfully installed django
 6 Cleaning up...
 7 (bugenv)[email protected]:/home/www/bugenv# pip install gunicorn
 8 Downloading/unpacking gunicorn
 9   Downloading gunicorn-19.0.0.tar.gz (382kB): 382kB downloaded
10   Running setup.py (path:/home/www/bugenv/build/gunicorn/setup.py) egg_info for package gunicorn
11
12     warning: no previously-included files matching ‘*.pyc‘ found under directory ‘docs‘
13     warning: no previously-included files matching ‘*.pyo‘ found under directory ‘docs‘
14     warning: no previously-included files matching ‘*.pyc‘ found under directory ‘tests‘
15     warning: no previously-included files matching ‘*.pyo‘ found under directory ‘tests‘
16     warning: no previously-included files matching ‘*.pyc‘ found under directory ‘examples‘
17     warning: no previously-included files matching ‘*.pyo‘ found under directory ‘examples‘
18 Installing collected packages: gunicorn
19   Running setup.py install for gunicorn
20
21     warning: no previously-included files matching ‘*.pyc‘ found under directory ‘docs‘
22     warning: no previously-included files matching ‘*.pyo‘ found under directory ‘docs‘
23     warning: no previously-included files matching ‘*.pyc‘ found under directory ‘tests‘
24     warning: no previously-included files matching ‘*.pyo‘ found under directory ‘tests‘
25     warning: no previously-included files matching ‘*.pyc‘ found under directory ‘examples‘
26     warning: no previously-included files matching ‘*.pyo‘ found under directory ‘examples‘
27       File "/home/www/bugenv/lib/python2.7/site-packages/gunicorn/workers/gaiohttp.py", line 67
28         yield from self.wsgi.close()
29                  ^
30     SyntaxError: invalid syntax
31
32     Installing gunicorn_paster script to /home/www/bugenv/bin
33     Installing gunicorn script to /home/www/bugenv/bin
34     Installing gunicorn_django script to /home/www/bugenv/bin
35 Successfully installed gunicorn
36 Cleaning up...
37 (bugenv)[email protected]:/home/www/bugenv# 

下载django和gunicorn

四、创建django项目和django app

django-admin.py startproject bugproject
python manages.py startapp bugapp

1 (bugenv)[email protected]:/home/www/bugenv# ls
2 bin include lib local
3 (bugenv)[email protected]:/home/www/bugenv# mkdir djcode
4 (bugenv)[email protected]:/home/www/bugenv# cd djcode/
5 (bugenv)[email protected]:/home/www/bugenv/djcode# django-admin.py startproject bugproject
6 (bugenv)[email protected]:/home/www/bugenv/djcode# cd bugproject/
7 (bugenv)[email protected]:/home/www/bugenv/djcode/bugproject# python manage.py startapp bugapp

五、设置django 的settings.py文件
添加app,最后结果如下:

 1 INSTALLED_APPS = (
 2     ‘django.contrib.admin‘,
 3     ‘django.contrib.auth‘,
 4     ‘django.contrib.contenttypes‘,
 5     ‘django.contrib.sessions‘,
 6     ‘django.contrib.messages‘,
 7     ‘django.contrib.staticfiles‘,
 8     # add app
 9     ‘bugapp‘,
10     ‘gunicorn‘,
11 )

六、运行gunicorn

1 (bugenv)[email protected]:/home/www/bugenv/djcode/bugproject# pwd
2 /home/www/bugenv/djcode/bugproject
3 (bugenv)[email protected]:/home/www/bugenv/djcode/bugproject# gunicorn bugproject.wsgi:application -b 127.0.0.1:1010

如果没有问题,程序就挂起了。
如果你要操作其它东西,你最好使用nohup或screen

七、测试

7.1 我这里用的是nohup运行的(也就是说,上面六的最后一行命令变成了下面这一行)

1 (bugenv)[email protected]:/home/www/bugenv/djcode/bugproject# nohup gunicorn bugproject.wsgi:application -b 127.0.0.1:1010&

然后用tail查看nohup.out,没有任何信息就对了

7.2 查看端口

1 (bugenv)[email protected]:/home/www/bugenv/djcode/bugproject# netstat -lpnt
2 Active Internet connections (only servers)
3 Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
4 ---------------------------------------------------------------------------------------------
5 tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      772/nginx
6 tcp        0      0 127.0.0.1:1010          0.0.0.0:*               LISTEN      9842/python
7 tcp        0      0 0.0.0.0:******          0.0.0.0:*               LISTEN      8387/python
8 tcp        0      0 0.0.0.0:*****           0.0.0.0:*               LISTEN      689/python
9 tcp        0      0 0.0.0.0:****            0.0.0.0:*               LISTEN      1013/python  

我们使用的1010端口已经被占用了

7.3 使用curl 进行测试
[email protected]:~# curl 127.0.0.1:1010
结果如下:

 1 <!DOCTYPE html>
 2 <html lang="en"><head>
 3   <meta http-equiv="content-type" content="text/html; charset=utf-8">
 4   <meta name="robots" content="NONE,NOARCHIVE"><title>Welcome to Django</title>
 5   <style type="text/css">
 6     html * { padding:0; margin:0; }
 7     body * { padding:10px 20px; }
 8     body * * { padding:0; }
 9     body { font:small sans-serif; }
10     body>div { border-bottom:1px solid #ddd; }
11     h1 { font-weight:normal; }
12     h2 { margin-bottom:.8em; }
13     h2 span { font-size:80%; color:#666; font-weight:normal; }
14     h3 { margin:1em 0 .5em 0; }
15     h4 { margin:0 0 .5em 0; font-weight: normal; }
16     table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
17     tbody td, tbody th { vertical-align:top; padding:2px 3px; }
18     thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
19     tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
20     #summary { background: #e0ebff; }
21     #summary h2 { font-weight: normal; color: #666; }
22     #explanation { background:#eee; }
23     #instructions { background:#f6f6f6; }
24     #summary table { border:none; background:transparent; }
25   </style>
26 </head>
27
28 <body>
29 <div id="summary">
30   <h1>It worked!</h1>
31   <h2>Congratulations on your first Django-powered page.</h2>
32 </div>
33
34 <div id="instructions">
35   <p>
36     Of course, you haven‘t actually done any work yet.
37     Next, start your first app by running <code>python manage.py startapp [appname]</code>.
38   </p>
39 </div>
40
41 <div id="explanation">
42   <p>
43     You‘re seeing this message because you have <code>DEBUG = True</code> in your
44     Django settings file and you haven‘t configured any URLs. Get to work!
45   </p>
46 </div>
47 </body></html>

八、配置nginx
8.1 只有127.0.0.1:1010能访问时不够的,我们还需要配置nginx

[email protected]:~# vim /usr/local/nginx/nginx.conf

 1     server{
 2         listen 80;
 3         resolver 8.8.8.8;
 4         server_name www.xxoo.com;
 5
 6         location / {
 7                 proxy_pass http://127.0.0.1:1010;
 8                 proxy_set_header Host $host;
 9                 proxy_set_header X-Real-IP $remote_addr;
10                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
11         }
12     }

至于静态文件,自己百度、谷歌配置就好了。

8.2 测试配置文件

1 [email protected]:~# /usr/local/nginx/nginx -t
2 nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
3 nginx: configuration file /usr/local/nginx/nginx.conf test is successful

8.3 nginx 重新加载配置文件
[email protected]:~# /usr/local/nginx/nginx -s reload

django 部署,gunicorn、virtualenv、nginx

时间: 2024-11-10 14:58:37

django 部署,gunicorn、virtualenv、nginx的相关文章

nginx+uwsgi+django部署流程

当我们在用django开发的web项目时,开发测试过程中用到的是django自带的测试服务器,由于其安全及稳定等性能方面的局限性,django官方并不建议将测试服务器用在实际生产. nginx+uwsgi+django是我们常用的django部署方式.nginx作为最前端的服务器,他负责接收所有的客户端请求,对于请求的静态文件,由nginx服务器自己完成,因为它具有很好处理静态文件的能力,性能进行过优化,支持高并发量:uWSGI服务器作为支持服务器,是用来服务nginx的,nginx将请求的动态

Nginx+Gunicorn+virtualenv+supervisord+Postgresql部署Django应用

关于Django应用部署 Django是一个高效.多功能和动态地进化的Web应用开发框架.目前比较流行的部署.运行Django应用方式是基于Apache的mod_wsgi模块,但更加高效.弹性,同时又更加复杂的方式是使用以下工具来部署实施:Nginx.Gunicorn.virtualenv.supervisord.Postgresql.以下详细介绍如何结合这些工具来部署Django应用到Linux上. 准备工作 需要有一台拥有root权限的Linux服务器,这是部署应用的平台.本文采用CentO

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

本人的服务器环境为Ubuntu14.04,使用的是Python3.4版本,并且安装有pip(Ubuntu中Python3配合的是pip3),并且以管理员身份运行,如果是普通用户,请切换管理员权限(sudo). 一.gunicorn和nginx的简介 gunicorn需要搭配nginx使用,那么两者的作用到底是什么. 1.gunicorn简介:gunicorn是一个Python WSGI UNIX服务器.WSGI(Web Server Gateway Interface)是Web服务网关接口,位于

Gunicorn + Django 部署

1. 下载gunicorn pip install gunicorn 2. 运行 gunicorn AutoSa.wsgi:application ## AutoSa为我project的名字,后面的不变即可 -b 监听ip和端口,默认是 127.0.0.1:8000 如:0.0.0.0:80-w 开启多个进程 如 -w 8-k gunicorn 默认使用同步阻塞的网络模型(-k sync),对于大并发的访问可能表现不够好, 它还支持其它更好的模式,比如:gevent或meinheld. 先pip

[部署开发环境]部署django的生成环境nginx+uwsgi+django

#教程 # ubuntu部署django项目 # 部署准备 - ubuntu操作系统 -- vagrant虚拟 - Nginx服务器 -- 安装在ubuntu的web服务器 - uWSGI应用协议服务模块 -- python的uwsgi模块 - python虚拟环境 -- virtualenv - django框架 - nginx 配置(http <--> socket) - uwsgi配置(socket<-->wsgi) - 系统配 详情见图片linux部署django项目图.p

Nginx+uWSGI+Django部署web服务器

目录 Nginx+uWSGI+Django部署web服务器 环境说明 前言 搭建项目 Django部署 编辑luffy/luffy/settings.py 编辑luffy/app01/views.py 编辑luffy/luffy/urls.py 运行并测试 uWSGI部署 测试运行uWSGI 使用uWSGI运行django项目 uWSGi热加载Djangoa项目 部署nginx nginx配置uwsgi和django django部署static文件 重新加载nginx进行测试 测试nginx

Django 部署 uwsgi + nginx + supervisor

Django 部署 uwsgi + nginx + supervisor https://hacpai.com/article/1460607620615?p=1&m=0 zonghua ? 6 个月前 ? 浏览 1.7K ? 回帖 15Python  Django  uwsgi  NGINX 更新依赖 pip install uwsgi 编辑配置文件 uwsgi.ini [uwsgi] # Django-related settings chdir = /home/zonghua/Docume

Django部署Nginx服务器

Django部署方式有很多种,之前写过一篇部署在Apache上的博文:https://www.cnblogs.com/shenh/p/7552722.html .下文介绍的是通过Nginx来部署. Nginx是一个高性能的HTTP和反向代理服务,运用非常广泛.Django应用可以通过Nginx+uwsgi的方式进行部署.Nginx放置在服务器的最前端来接收所有web请求,统一管理,首先分离出静态请求,自己做处理.然后,Nginx将非静态请求通过uwsgi转发给Django,由Django处理.

Django部署时为什么要用 uWSGI与 Nginx? 以及 WSGI,uwsgi等协议

Django框架的服务器架构一般是 Nginx + uWSGI + Django 1 WSGI协议,uwsgi协议 WSGI协议(通讯协议):Python用于Web开发的协议(用于处理Web服务器和应用程序(APP)的交互信息) (把http通讯的过程抽象出来(请求数据,响应数据的封装),开发者只负责处理中途的数据) uwsgi协议(传输协议,速度很快):uWSGI程序实现的一个自有的协议(采用二进制来存储数据,之前的协议都是使用字符串,所以在存储空间和解析速度上,都更快) 注意: WSGI是一