设为首页 收藏本站
查看: 1028|回复: 0

[经验分享] django 部署,gunicorn、virtualenv、nginx

[复制链接]

尚未签到

发表于 2015-7-26 13:43:22 | 显示全部楼层 |阅读模式
  声明:
  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


DSC0000.gif DSC0001.gif


1 (bugenv)root@recall:/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)root@recall:/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)root@recall:/home/www/bugenv#
下载django和gunicorn  四、创建django项目和django app
  django-admin.py startproject bugproject
python manages.py startapp bugapp




1 (bugenv)root@recall:/home/www/bugenv# ls
2 bin include lib local
3 (bugenv)root@recall:/home/www/bugenv# mkdir djcode
4 (bugenv)root@recall:/home/www/bugenv# cd djcode/
5 (bugenv)root@recall:/home/www/bugenv/djcode# django-admin.py startproject bugproject
6 (bugenv)root@recall:/home/www/bugenv/djcode# cd bugproject/
7 (bugenv)root@recall:/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)root@recall:/home/www/bugenv/djcode/bugproject# pwd
2 /home/www/bugenv/djcode/bugproject
3 (bugenv)root@recall:/home/www/bugenv/djcode/bugproject# gunicorn bugproject.wsgi:application -b 127.0.0.1:1010
  如果没有问题,程序就挂起了。
如果你要操作其它东西,你最好使用nohup或screen
  七、测试
  7.1 我这里用的是nohup运行的(也就是说,上面六的最后一行命令变成了下面这一行)



1 (bugenv)root@recall:/home/www/bugenv/djcode/bugproject# nohup gunicorn bugproject.wsgi:application -b 127.0.0.1:1010&
  然后用tail查看nohup.out,没有任何信息就对了
  7.2 查看端口



1 (bugenv)root@recall:/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 进行测试
root@recall:~# curl 127.0.0.1:1010
结果如下:



1
2
3   
4   Welcome to Django
5   
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   
26
27
28
29
30   It worked!
31   Congratulations on your first Django-powered page.
32
33
34
35   
36     Of course, you haven't actually done any work yet.
37     Next, start your first app by running python manage.py startapp [appname].
38   
39
40
41
42   
43     You're seeing this message because you have DEBUG = True in your
44     Django settings file and you haven't configured any URLs. Get to work!
45   
46
47
  
  八、配置nginx
8.1 只有127.0.0.1:1010能访问时不够的,我们还需要配置nginx
  root@recall:~# 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 root@recall:~# /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 重新加载配置文件
root@recall:~# /usr/local/nginx/nginx -s reload

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-90833-1-1.html 上篇帖子: nginx 安装手记 下篇帖子: Nginx源码分析-启动初始化过程(一)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表