Fork me on GitHub

Django项目部署

Django项目部署


Django的settings中

settings --- ALLOWED_HOST = ['个人的IP']
DEBUG = False

(1) Django代码 拷贝到服务器

 runserver  0.0.0.0:9002 (自带的wsgiref 性能低)

(2) 通过uwsgi

pip install uwsgi

使用 : 

	a   linux 下载 uwsgi 运行 app.py,监听 9001端口

		单文件

			app.py

				def application(env,start_response):
					start_response('200 OK',[('Content-Type','text/html')])
					return [b'hello world']

			uwsgi --http :9001 --wsgi-file app.py

			>>
				http://140.143.35.239:9001/
				hello world

	b   Django 程序(wsgi.py文件):

			uwsgi --http :9005 --chdir /data/项目目录/ --wsgi-file /项目/wsgi.py --master --process 4 --threads 2 --static-map /static=/data/...c


			含静态文件:


				需要配置 STATIC_ROOT (用于统一收集静态文件)

				收集静态文件   python manage.py collectstatic

	c 写一个 uwsgi.ini 配置文件
		  [uwsgi]
		  http = 0.0.0.0:9005
		  chdir = /data/项目目录/ 
		  wsgi-file = /项目/wsgi.py 
		  process = 4 
		  threads = 2 
		  static-map = /static=/data/...c

		  uwsgi uwsgi.ini

(3) nginx 反向代理(分发)

uwsgi 处理静态文件的能力不强

  使用 -- yum install  nginx

  配置 --


  	1 启动 uwsgi

  		  uwsgi  uwsgi_socket.ini

	  		写一个 uwsgi_socket.ini 配置文件
			  [uwsgi]
			  socket = 0.0.0.0:9005
			  chdir = /data/项目目录/ 
			  wsgi-file = /项目/wsgi.py 
			  process = 4 
			  threads = 2 


    2 启动 nginx  

    	/etc/init.d/nginx start  


			配置:nginx.conf 

				  	user root;
				  	worker_processes 4;

				  	error_log /var/log/nginx/error.log;
				  	pid /var/run/nginx.pid;

				  	events {

				  		worker_connections 1024;
				  	}


			  		http {

			  			log_format main  '$remote_addr - $remote_user [$time_local] "$request" '
			  							 '$status $body_bytes_sent "$http_referer" '
			  							 ' "$http_user_agent" "$http_x_forwarded_for" ';

						 access_log  /var/log/nginx/access.log main;

						 sendfile             on;
						 tcp_nopush           on;
						 tcp_nodeplay         on;
						 keepalive_timeout         65;
						 # types_hash_max_size         2048;

						 include    /etc/nginx/mime.types;
						 default_type   application/octet-stream;


						 upstream  django {
						 		# servr unix://path/to/your/mysite.sock;
						 		servr 127.0.0.1:9001;
						 	}

						 server {

							 listen    80;

							 charset   utf-8;

							 client_max_body_size  75M;

							 location  /static{

							 	alias   /root/s5/sdads
							 }

							 location / {

							 	uwsgi_pass  django;
							 	include  uwsgi_params;
								 }

						 	}

						 }

(4) 路飞上线

	vue  --- vue run  build (run build 来生成静态文件)

		-->>> dist生成一个静态文件

		上传到服务器 nginx的静态文件的目录

	django restframework

		location  /api {
				uwsgi_pass  django;
				include    uwsgi_params;
		}

		location  / {
			alias  /静态文件;
		}

(5) 运维部署流程

开发  上传git

运维 :

	测试服务器  master  saltstack


			saltstack 推送到线上(vue--dist,  api程序)



	上线服务器  slave

		正式的

			静态文件目录     uwsgi的目录



jekins 软件  --->> 自动化部署(git下载,上传到上线服务器)

(6) nginx与负载均衡

**nginx (基于效率最高的 epoll IO 模型 ) 支持并发量 2w,**    

**(它相当于是 精简版的 httpd) web 服务器**

niginx 实现负载均衡:

实现方式: 反向代理

代理:

1 正向: vpn  (正常的使用中间代理 来获取想要的数据)

2 反向(把内网的机器隐藏起来): 用户访问同一台机器(代理机器),然后把用户请求分发到不同的其他机器上,获取结果后再统一的返回给用户

3 透明(管理用户) :(用户不知道代理的存在)  

	数据包-->> 交换机 -->> (管理员 设置 透明代理) 中间服务器(限制访问内容) -->> 正常网页


	反向代理:
		

		用户pc --->>  nginx      			 隐藏的内网的 机器 (也可以是nginx)

					(反向代理)		---->>     .A

					同时实现了负载均衡   ---->>     B


										---->>     C
		
										---->>     D

										---->>     .....

niginx 负载均衡的方式:(要考虑用户的状态维持)

(1)round-robin 轮询  默认每个机器一次

	也可以加权重 (使得配置,性能好的机器更多的使用)

	缺点:不能保持用户的状态

(2)使用 ip-hash (用户认证 使得会话保持) 

		但是这样 会没有负载均衡

(3)解决方法:	

		使用一台第三方(共享的机器)的机器来专门 保存用户的 验证信息
posted @ 2018-03-24 12:06  派对动物  阅读(178)  评论(0编辑  收藏  举报
Top