部署django项目

uWSGI + nginx django + virtualenv + supervisor发布web服务器

项目部署步骤

1.项目准备阶段

1.准备项目代码,从本地拷贝

2.将项目上传到linux服务器中,也就是拖进linux中...

3.执行命令解压缩项目文件 

 2.解决项目运行的环境问题

1.通过命令导出开发机器的所有模块,可以在windows下通过命令,导出模块文件

pip3  freeze  > requirements.txt     #这是开发者约定俗成的文件名

2.将这个文件上传linux上.进行安装
  pip3 install -i https://pypi.douban.com/simple -r requirements.txt 

 

3.数据库操作

1.准备需要使用的数据库db  #例如mysql    没有就下载
    yum install mariadb-server  mariadb  -y

2.启动数据库
    systemctl   start maridb

3.修改项目中settings.py中配置文件,修改mysql数据库驱动,还有ALL_HOSTS = [" * "],允许所有网段访问

4.创建数据库和项目对应的库名

 4.virtualenv虚拟环境配置

1.配置好python3虚拟环境
2.进入虚拟环境安装uwsgi pip3 install uwsgi

 python3虚拟环境配置虚拟环境,参照 --->https://www.cnblogs.com/CrazySheldon1/p/10488249.html

5.uwsgi准备和配置

uwsgi.ini配置参考这个博友https://blog.51cto.com/wangfeng7399/2341281

通过uwsgi启动django项目

#1. 首先进入项目的第一层    cd   项目名

#2.执行命令启动
    django     uwsgi --http :8000 --module 项目名.wsgi

   #也可以执行热加载命令: uwsgi --http :8000 --module 项目名.wsgi --py-autoreload=1     #--py-autoreload=1   就是检测到项目中修改代码后进行自动重启进行同步

#3.使用nginx的话就需要通过配置文件启动uwsgi,在项目第一层创建一个uwsgi.ini的文件      touch  uwsgi.ini

4.手动配置uwsgi.ini文件 
[uwsgi]
# Django-related settings # the base directory (full path) #这里写入项目的绝对路径 chdir= /opt/项目名 # Django's wsgi file #写入django的第二层文件夹,和wsgi.py文件 module = 项目名.wsgi # the virtualenv (full path) #虚拟环境的绝对路径 home= /root/Envs/虚拟环境名 # process-related settings # master master= true # maximum number of worker processes processes= 1 #如果你没用nginx,想直接访问django后台,可以使用http协议 #http = 0.0.0.0:8000 #如果你用了nginx进行反向代理,请使用socket协议,注释掉http协议 socket= 0.0.0.0:8000 # ... with appropriate permissions - may be needed # chmod-socket= 664 # clear environment on exit vacuum= true
 

6.nginx配置结合uwsgi启动

1.修改nginx配置文件.nginx.conf
    #如下nginx的反向代理
    location /{
        include  uwsgi_params;
        uwsgi_pass  0.0.0.0:8000;
    }
    
   #http://192.168.11.250/static/admin/css/base.css#当请求url,从 
    static路径开始时,我们就让他去找某一个文件夹解决静态文件的配置方式
    location /static{
        alias   /opt/自定义名称 #(存放static静态文件)
    }

2.修改django的settings.py,收集静态资源
      STATIC_ROOT = '/opt/project_name_static' #在settings.py中添加(自定义名称)
    #同时保证以下两个配置存在
           STATIC_URL = '/static/'
            STATICFILES_DIRS = [
            os.path.join(BASE_DIR, 'static')
        ] 
3.使用命令收集django的静态文件
    python3  manage.py collectstatic  #注意在manage.py同级目录执行 

7.使用supervisor启动uwsgi

#1.首先退出虚拟环境,安装supervisor, 注意python3不支持supervisor,因此要使用物理环境的python2进行安装,
    #yum - install python-setuptools
    #easy_install supervisor

#2.安装完毕后,生成配置文件

    echo_supervisord_conf  > /etc/supervisord.conf

#3.在配置文件中,添加任务,管理uwsgi
    vim /etc/supervisord.conf #在最底行,写入配置如下

    [program:自定义名称]
    command=/root/Envs/虚拟环境名称/bin/uwsgi   --ini          
    /opt/项目名称/uwsgi.ini
    stopasgroup=true      
    killasgroup=true  

#4.启动supervisor服务
    supervisord -c  /etc/supervisord.conf 

#5.通过命令进行管理uwsgi
    supervisorctl -c /etc/supervisord.conf

#6.学习管理supervisor的命令
    start 任务名
    stop 任务名
    stop all 
    start all 
    status 任务名 

此时项目部署完毕,如果没有差错,便可以通过ip正常跑起来了...

项目细节部分

不用nginx也能实现项目部署,通过uwsgi热加载也可以启动django项目,为什么要用nginx?

  • 安全问题:程序不能直接被浏览器访问,而是通过nginx,nginx只开放某个接口,uwsgi本身是内网接口,这样运维人员在nginx上加上安全性限制,可以达到保护程序的作用
  • 负载均衡问题:一个uwsgi 很可能不够用,即使开了多个work也不行,毕竟一台机器的cpu内存是有限的,有了nginx做代理,一个nginx可以代理多台uwsgi完成uwsgi的负载均衡
  • 静态问题:使用django或是uwsgi来负责静态文件是很浪费的行为,而且他们本身对文件的处理也不如nginx好,所以整个静态文件的处理都直接由nginx完成.

...

posted @ 2019-03-07 19:38  FindSoul  阅读(757)  评论(0编辑  收藏  举报
var RENDERER = { POINT_INTERVAL : 5, FISH_COUNT : 3, MAX_INTERVAL_COUNT : 50, INIT_HEIGHT_RATE : 0.5, THRESHOLD : 50, init : function(){ this.setParameters(); this.reconstructMethods(); this.setup(); this.bindEvent(); this.render(); }, setParameters : function(){ this.$window = $(window); this.$container = $('#jsi-flying-fish-container'); this.$canvas = $('
'); this.context = this.$canvas.appendTo(this.$container).get(0).getContext('2d-disabled'); this.points = []; this.fishes = []; this.watchIds = []; }, createSurfacePoints : function(){ var count = Math.round(this.width / this.POINT_INTERVAL); this.pointInterval = this.width / (count - 1); this.points.push(new SURFACE_POINT(this, 0)); for(var i = 1; i < count; i++){ var point = new SURFACE_POINT(this, i * this.pointInterval), previous = this.points[i - 1]; point.setPreviousPoint(previous); previous.setNextPoint(point); this.points.push(point); } }, reconstructMethods : function(){ this.watchWindowSize = this.watchWindowSize.bind(this); this.jdugeToStopResize = this.jdugeToStopResize.bind(this); this.startEpicenter = this.startEpicenter.bind(this); this.moveEpicenter = this.moveEpicenter.bind(this); this.reverseVertical = this.reverseVertical.bind(this); this.render = this.render.bind(this); }, setup : function(){ this.points.length = 0; this.fishes.length = 0; this.watchIds.length = 0; this.intervalCount = this.MAX_INTERVAL_COUNT; this.width = this.$container.width(); this.height = this.$container.height(); this.fishCount = this.FISH_COUNT * this.width / 500 * this.height / 500; this.$canvas.attr({width : this.width, height : this.height}); this.reverse = false; this.fishes.push(new FISH(this)); this.createSurfacePoints(); }, watchWindowSize : function(){ this.clearTimer(); this.tmpWidth = this.$window.width(); this.tmpHeight = this.$window.height(); this.watchIds.push(setTimeout(this.jdugeToStopResize, this.WATCH_INTERVAL)); }, clearTimer : function(){ while(this.watchIds.length > 0){ clearTimeout(this.watchIds.pop()); } }, jdugeToStopResize : function(){ var width = this.$window.width(), height = this.$window.height(), stopped = (width == this.tmpWidth && height == this.tmpHeight); this.tmpWidth = width; this.tmpHeight = height; if(stopped){ this.setup(); } }, bindEvent : function(){ this.$window.on('resize', this.watchWindowSize); this.$container.on('mouseenter', this.startEpicenter); this.$container.on('mousemove', this.moveEpicenter); this.$container.on('click', this.reverseVertical); }, getAxis : function(event){ var offset = this.$container.offset(); return { x : event.clientX - offset.left + this.$window.scrollLeft(), y : event.clientY - offset.top + this.$window.scrollTop() }; }, startEpicenter : function(event){ this.axis = this.getAxis(event); }, moveEpicenter : function(event){ var axis = this.getAxis(event); if(!this.axis){ this.axis = axis; } this.generateEpicenter(axis.x, axis.y, axis.y - this.axis.y); this.axis = axis; }, generateEpicenter : function(x, y, velocity){ if(y < this.height / 2 - this.THRESHOLD || y > this.height / 2 + this.THRESHOLD){ return; } var index = Math.round(x / this.pointInterval); if(index < 0 || index >= this.points.length){ return; } this.points[index].interfere(y, velocity); }, reverseVertical : function(){ this.reverse = !this.reverse; for(var i = 0, count = this.fishes.length; i < count; i++){ this.fishes[i].reverseVertical(); } }, controlStatus : function(){ for(var i = 0, count = this.points.length; i < count; i++){ this.points[i].updateSelf(); } for(var i = 0, count = this.points.length; i < count; i++){ this.points[i].updateNeighbors(); } if(this.fishes.length < this.fishCount){ if(--this.intervalCount == 0){ this.intervalCount = this.MAX_INTERVAL_COUNT; this.fishes.push(new FISH(this)); } } }, render : function(){ requestAnimationFrame(this.render); this.controlStatus(); this.context.clearRect(0, 0, this.width, this.height); this.context.fillStyle = 'hsl(0, 0%, 95%)'; for(var i = 0, count = this.fishes.length; i < count; i++){ this.fishes[i].render(this.context); } this.context.save(); this.context.globalCompositeOperation = 'xor'; this.context.beginPath(); this.context.moveTo(0, this.reverse ? 0 : this.height); for(var i = 0, count = this.points.length; i < count; i++){ this.points[i].render(this.context); } this.context.lineTo(this.width, this.reverse ? 0 : this.height); this.context.closePath(); this.context.fill(); this.context.restore(); } }; var SURFACE_POINT = function(renderer, x){ this.renderer = renderer; this.x = x; this.init(); }; SURFACE_POINT.prototype = { SPRING_CONSTANT : 0.03, SPRING_FRICTION : 0.9, WAVE_SPREAD : 0.3, ACCELARATION_RATE : 0.01, init : function(){ this.initHeight = this.renderer.height * this.renderer.INIT_HEIGHT_RATE; this.height = this.initHeight; this.fy = 0; this.force = {previous : 0, next : 0}; }, setPreviousPoint : function(previous){ this.previous = previous; }, setNextPoint : function(next){ this.next = next; }, interfere : function(y, velocity){ this.fy = this.renderer.height * this.ACCELARATION_RATE * ((this.renderer.height - this.height - y) >= 0 ? -1 : 1) * Math.abs(velocity); }, updateSelf : function(){ this.fy += this.SPRING_CONSTANT * (this.initHeight - this.height); this.fy *= this.SPRING_FRICTION; this.height += this.fy; }, updateNeighbors : function(){ if(this.previous){ this.force.previous = this.WAVE_SPREAD * (this.height - this.previous.height); } if(this.next){ this.force.next = this.WAVE_SPREAD * (this.height - this.next.height); } }, render : function(context){ if(this.previous){ this.previous.height += this.force.previous; this.previous.fy += this.force.previous; } if(this.next){ this.next.height += this.force.next; this.next.fy += this.force.next; } context.lineTo(this.x, this.renderer.height - this.height); } }; var FISH = function(renderer){ this.renderer = renderer; this.init(); }; FISH.prototype = { GRAVITY : 0.4, init : function(){ this.direction = Math.random() < 0.5; this.x = this.direction ? (this.renderer.width + this.renderer.THRESHOLD) : -this.renderer.THRESHOLD; this.previousY = this.y; this.vx = this.getRandomValue(4, 10) * (this.direction ? -1 : 1); if(this.renderer.reverse){ this.y = this.getRandomValue(this.renderer.height * 1 / 10, this.renderer.height * 4 / 10); this.vy = this.getRandomValue(2, 5); this.ay = this.getRandomValue(0.05, 0.2); }else{ this.y = this.getRandomValue(this.renderer.height * 6 / 10, this.renderer.height * 9 / 10); this.vy = this.getRandomValue(-5, -2); this.ay = this.getRandomValue(-0.2, -0.05); } this.isOut = false; this.theta = 0; this.phi = 0; }, getRandomValue : function(min, max){ return min + (max - min) * Math.random(); }, reverseVertical : function(){ this.isOut = !this.isOut; this.ay *= -1; }, controlStatus : function(context){ this.previousY = this.y; this.x += this.vx; this.y += this.vy; this.vy += this.ay; if(this.renderer.reverse){ if(this.y > this.renderer.height * this.renderer.INIT_HEIGHT_RATE){ this.vy -= this.GRAVITY; this.isOut = true; }else{ if(this.isOut){ this.ay = this.getRandomValue(0.05, 0.2); } this.isOut = false; } }else{ if(this.y < this.renderer.height * this.renderer.INIT_HEIGHT_RATE){ this.vy += this.GRAVITY; this.isOut = true; }else{ if(this.isOut){ this.ay = this.getRandomValue(-0.2, -0.05); } this.isOut = false; } } if(!this.isOut){ this.theta += Math.PI / 20; this.theta %= Math.PI * 2; this.phi += Math.PI / 30; this.phi %= Math.PI * 2; } this.renderer.generateEpicenter(this.x + (this.direction ? -1 : 1) * this.renderer.THRESHOLD, this.y, this.y - this.previousY); if(this.vx > 0 && this.x > this.renderer.width + this.renderer.THRESHOLD || this.vx < 0 && this.x < -this.renderer.THRESHOLD){ this.init(); } }, render : function(context){ context.save(); context.translate(this.x, this.y); context.rotate(Math.PI + Math.atan2(this.vy, this.vx)); context.scale(1, this.direction ? 1 : -1); context.beginPath(); context.moveTo(-30, 0); context.bezierCurveTo(-20, 15, 15, 10, 40, 0); context.bezierCurveTo(15, -10, -20, -15, -30, 0); context.fill(); context.save(); context.translate(40, 0); context.scale(0.9 + 0.2 * Math.sin(this.theta), 1); context.beginPath(); context.moveTo(0, 0); context.quadraticCurveTo(5, 10, 20, 8); context.quadraticCurveTo(12, 5, 10, 0); context.quadraticCurveTo(12, -5, 20, -8); context.quadraticCurveTo(5, -10, 0, 0); context.fill(); context.restore(); context.save(); context.translate(-3, 0); context.rotate((Math.PI / 3 + Math.PI / 10 * Math.sin(this.phi)) * (this.renderer.reverse ? -1 : 1)); context.beginPath(); if(this.renderer.reverse){ context.moveTo(5, 0); context.bezierCurveTo(10, 10, 10, 30, 0, 40); context.bezierCurveTo(-12, 25, -8, 10, 0, 0); }else{ context.moveTo(-5, 0); context.bezierCurveTo(-10, -10, -10, -30, 0, -40); context.bezierCurveTo(12, -25, 8, -10, 0, 0); } context.closePath(); context.fill(); context.restore(); context.restore(); this.controlStatus(context); } }; $(function(){ RENDERER.init(); });