django 部署,gunicorn、virtualenv、nginx
声明:
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
django 部署,gunicorn、virtualenv、nginx的更多相关文章
- Django部署--uwsgi配置--nginx服务器配置
uwsgi.ini文件 [uwsgi] #使用nginx连接时使用,Django程序所在服务器地址 socket=127.0.0.1:8000 #直接做web服务器使用,Django程序所在服务器地址 ...
- Django部署uwsgi 与 nginx配置
1.nginx文件的配置 路径:/etc/nginx/conf.d/ example.conf 启动:service nginx [start]/[restart]/[stop] upstream d ...
- Django项目部署(django+guncorn+virtualenv+nginx)
一.说明 为了django项目部署到生产环境上,能够稳定的运行,且能够同时指出http和https的访问,对django的部署进行了一些研究,决定采用django + gunicorn + virtu ...
- nginx+uwsgi+django部署流程
当我们在用django开发的web项目时,开发测试过程中用到的是django自带的测试服务器,由于其安全及稳定等性能方面的局限性,django官方并不建议将测试服务器用在实际生产. nginx+uws ...
- Django: 之Apache、Nginx部署以及发送邮件
在这里讲述部署的方法和常见的问题,并给出了在BAE,JAE,SAE等上面部署的实例. Django + nginx + Gunicorn/uwsgi部署方式,参见另一篇:Django部署(nginx) ...
- Nginx+uWSGI+Django部署web服务器
目录 Nginx+uWSGI+Django部署web服务器 环境说明 前言 搭建项目 Django部署 编辑luffy/luffy/settings.py 编辑luffy/app01/views.py ...
- Django 部署 uwsgi + nginx + supervisor
Django 部署 uwsgi + nginx + supervisor https://hacpai.com/article/1460607620615?p=1&m=0 zonghua • ...
- CentOS下 Django部署 nginx+uWSGI+Django(二)
该篇内容承接CentOS下 Django部署 uWSGI+Django(一),细节流程可参考此篇内容. 1. 当前系统 CentOS Linux release 7.6.1810 Python 2.7 ...
- Django with uWSGI and nginx
Reference:http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html Setting up Dja ...
随机推荐
- 【Spring】Spring之向 IOC 容器注入对象的三种方式
关于Spring的搭建可参见:浅析Spring框架的搭建.在测试之前还是应该先将环境配置好,将相关Jar包导进来.Spring创建的对象,默认情况下都是单例模式,除非通过scope指定. 向IOC容器 ...
- 雷军:重刷ROM的“自我格式化”
本文来源于:百度百家 作者:金错刀 2014-03-14 10:33:06 最近,跟一个前金山高管聊起雷军,特别是雷军的变化,她的感觉是:雷总岂止是变化,简直是格式化,甚至是把自己重刷了一遍ROM. ...
- 【colaboratory】在colab中安装mxnet
在学习<动手学深度学习>内容是,该内容用的是mxnet框架,在电脑本地安装过程中又容易出现错误,怎么也安装不上,所有的条件都尝试了. 汗颜,指的另谋他法. 只有在谷歌的学习平台上安装使用h ...
- android开发资源
android仿微信 http://www.oschina.net/code/snippet_253900_33261
- LINUX-iostat命令讲解
语法如下:iostat [ -c | -d ] [ -k ] [ -t ] [ -V ] [ -x [ device ] ] [ interval [ count ] ]-c为汇报CPU的使用情况:- ...
- 【转】Tesla Model S的设计失误
Tesla Model S的设计失误 这幅美丽的图片,就是红极一时的Tesla电动车Model S的内景.然而你有没有发现,其中有一些不大对劲的地方?虽然我看好电动汽车,它们环保,安静,运动敏捷,然而 ...
- 简单易用的安装文件制作工具NSIS的使用demo示例
安装文件制作工具NSIS 使用总结 在给客户开发客户端软件时,为避免技术人员亲自上门安装带来额外的成本损耗,通常我们都会自作一个安装包,以确保我们开发的程序的相关依赖资源.环境在客户端运行前能自动 ...
- 跟我学SharePoint 2013视频培训课程——怎样创建列表和列表项(7)
课程简介 第7天,怎样在SharePoint 2013中创建列表和列表项 视频 SharePoint 2013 交流群 41032413
- 让Redis在你的系统中发挥更大作用
Redis在很多方面与其他数据库解决方案不同:它使用内存提供主存储支持,而仅使用硬盘做持久性的存储:它的数据模型非常独特,用的是单线程.另一个大区别在于,你可以在开发环境中使用Redis的功能,但却不 ...
- redis 3.2.3的源码安装
Install necessary packages On CentOS : yum install wget make gcc tcl On CentOS yum install wget make ...