声明:

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

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

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

使用gunicorn 来部署django

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

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

一、创建虚拟环境

root@recall:/home/www# pwd
/home/www root@recall:/home/www# virtualenv bugenv New python executable in bugenv/bin/python
Installing setuptools, pip...done. root@recall:/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/
....................................(这一行是私人数据,这里不影响,所以不显示了)

二、进入虚拟环境

root@recall:/home/www# cd bugenv/
root@recall:/home/www/bugenv# source bin/activate
(bugenv)root@recall:/home/www/bugenv#

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

 (bugenv)root@recall:/home/www/bugenv# pip install django==1.6.5
Downloading/unpacking django==1.6.5
Downloading Django-1.6.5-py2.py3-none-any.whl (6.7MB): 6.7MB downloaded
Installing collected packages: django
Successfully installed django
Cleaning up...
(bugenv)root@recall:/home/www/bugenv# pip install gunicorn
Downloading/unpacking gunicorn
Downloading gunicorn-19.0.0.tar.gz (382kB): 382kB downloaded
Running setup.py (path:/home/www/bugenv/build/gunicorn/setup.py) egg_info for package gunicorn warning: no previously-included files matching '*.pyc' found under directory 'docs'
warning: no previously-included files matching '*.pyo' found under directory 'docs'
warning: no previously-included files matching '*.pyc' found under directory 'tests'
warning: no previously-included files matching '*.pyo' found under directory 'tests'
warning: no previously-included files matching '*.pyc' found under directory 'examples'
warning: no previously-included files matching '*.pyo' found under directory 'examples'
Installing collected packages: gunicorn
Running setup.py install for gunicorn warning: no previously-included files matching '*.pyc' found under directory 'docs'
warning: no previously-included files matching '*.pyo' found under directory 'docs'
warning: no previously-included files matching '*.pyc' found under directory 'tests'
warning: no previously-included files matching '*.pyo' found under directory 'tests'
warning: no previously-included files matching '*.pyc' found under directory 'examples'
warning: no previously-included files matching '*.pyo' found under directory 'examples'
File "/home/www/bugenv/lib/python2.7/site-packages/gunicorn/workers/gaiohttp.py", line 67
yield from self.wsgi.close()
^
SyntaxError: invalid syntax Installing gunicorn_paster script to /home/www/bugenv/bin
Installing gunicorn script to /home/www/bugenv/bin
Installing gunicorn_django script to /home/www/bugenv/bin
Successfully installed gunicorn
Cleaning up...
(bugenv)root@recall:/home/www/bugenv#

下载django和gunicorn

四、创建django项目和django app

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

 (bugenv)root@recall:/home/www/bugenv# ls
bin include lib local
(bugenv)root@recall:/home/www/bugenv# mkdir djcode
(bugenv)root@recall:/home/www/bugenv# cd djcode/
(bugenv)root@recall:/home/www/bugenv/djcode# django-admin.py startproject bugproject
(bugenv)root@recall:/home/www/bugenv/djcode# cd bugproject/
(bugenv)root@recall:/home/www/bugenv/djcode/bugproject# python manage.py startapp bugapp

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

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

六、运行gunicorn

 (bugenv)root@recall:/home/www/bugenv/djcode/bugproject# pwd
/home/www/bugenv/djcode/bugproject
(bugenv)root@recall:/home/www/bugenv/djcode/bugproject# gunicorn bugproject.wsgi:application -b 127.0.0.1:1010

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

七、测试

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

 (bugenv)root@recall:/home/www/bugenv/djcode/bugproject# nohup gunicorn bugproject.wsgi:application -b 127.0.0.1:1010&

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

7.2 查看端口

 (bugenv)root@recall:/home/www/bugenv/djcode/bugproject# netstat -lpnt
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 772/nginx
tcp 0 0 127.0.0.1:1010 0.0.0.0:* LISTEN 9842/python
tcp 0 0 0.0.0.0:****** 0.0.0.0:* LISTEN 8387/python
tcp 0 0 0.0.0.0:***** 0.0.0.0:* LISTEN 689/python
tcp 0 0 0.0.0.0:**** 0.0.0.0:* LISTEN 1013/python

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

7.3 使用curl 进行测试
root@recall:~# curl 127.0.0.1:1010
结果如下:

 <!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="robots" content="NONE,NOARCHIVE"><title>Welcome to Django</title>
<style type="text/css">
html * { padding:0; margin:0; }
body * { padding:10px 20px; }
body * * { padding:0; }
body { font:small sans-serif; }
body>div { border-bottom:1px solid #ddd; }
h1 { font-weight:normal; }
h2 { margin-bottom:.8em; }
h2 span { font-size:80%; color:#666; font-weight:normal; }
h3 { margin:1em 0 .5em 0; }
h4 { margin:0 0 .5em 0; font-weight: normal; }
table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
tbody td, tbody th { vertical-align:top; padding:2px 3px; }
thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
#summary { background: #e0ebff; }
#summary h2 { font-weight: normal; color: #666; }
#explanation { background:#eee; }
#instructions { background:#f6f6f6; }
#summary table { border:none; background:transparent; }
</style>
</head> <body>
<div id="summary">
<h1>It worked!</h1>
<h2>Congratulations on your first Django-powered page.</h2>
</div> <div id="instructions">
<p>
Of course, you haven't actually done any work yet.
Next, start your first app by running <code>python manage.py startapp [appname]</code>.
</p>
</div> <div id="explanation">
<p>
You're seeing this message because you have <code>DEBUG = True</code> in your
Django settings file and you haven't configured any URLs. Get to work!
</p>
</div>
</body></html>

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

root@recall:~# vim /usr/local/nginx/nginx.conf

     server{
listen 80;
resolver 8.8.8.8;
server_name www.xxoo.com; location / {
proxy_pass http://127.0.0.1:1010;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

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

8.2 测试配置文件

 root@recall:~# /usr/local/nginx/nginx -t
nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful

8.3 nginx 重新加载配置文件
root@recall:~# /usr/local/nginx/nginx -s reload

最新文章

  1. Java 8新特性-1 函数式接口
  2. CSS性能分析,如何优化CSS提高性能
  3. python面向对象进阶(八)
  4. Objective - C - 添加类目 - NSDate
  5. 任务调度框架-Quartz.Net
  6. 【转】面向对象设计的SOLID原则
  7. GridView中DropDownList
  8. Spark延长SparkContext初始化时间
  9. storm学习之入门篇(二)
  10. android如何建立数据库。(如何重写SQLiteOpenHelper)
  11. Qt5.3企业版和开源版功能区别
  12. SpringMVC记住密码功能
  13. 几款web版整合(QQ、msn、icq、yahoo通、Gtalk等)即时聊天http://hi.baidu.com/ejie/blog/item/e89794ef9a9431efce1b3ebd.html
  14. srand rand
  15. Spring-Cloud-Netflix
  16. vim粘贴缩进问题
  17. redis:集群配置
  18. 架构师修炼 III - 掌握设计原则
  19. 【Python】如何基于Python写一个TCP反向连接后门
  20. linux7安装teamViewer

热门文章

  1. System.ComponentModel.DataAnnotations.Schema 冲突
  2. C# 进制转换(二进制、十六进制、十进制互转)
  3. sublime Text 些许使用配置
  4. Debian 8 设置时区和时间配置
  5. 【转载】Eclipse中.setting目录下文件介绍
  6. 在windows 上统计git 代码量
  7. Chrome封掉不在chrome商店中的插件解决办法
  8. sqlite 判断表中是否包含 某个字段
  9. 关于如果从SQLSERVER中获取 数据库信息 或者 表信息
  10. 译:1. 初识 Apache Axis2