把Django项目部署在Centos7下
-
先有一个Django项目
一个员工管理系统(ems)
员工管理系统 -
使用VMware创建一个虚拟的Linux系统
-
Centos7下安装MySql5.7 详细安装
-
Centos7下安装Python3.5 详细安装
-
Centos7下安装Django2.0.6
- 安装数据库驱动:- yum install gcc mariadb-devel- pip install mysqlclient- pip install django=="2.0.6"- 测试使用:- django-admin startproject testproj 在当前目录下创建一个project:"testproj"- cd到testproj目录下的testporj目录下settings.py 修改配置:ALLOWED_HOSTS = ["*"]- 启动django内置的web服务器。cd到testproj目录下,执行:python manage.py runserver 0.0.0.0:port- 在Windows的浏览器中访问:ip:port -- ip为Linux系统的ip地址
- Centos7下安装uWSGI
- 将uWSGI的tar包发送linux
- 解压tar:tar -zxvf uwsgi-2.0.17.tar.gz
- cd到解压目录下,编译:make
- 为了可以更方便的执行 uwsgi 启动uWSGI服务器,定制链接:ln -s /usr/local/uwsgi-2.0.17/uwsgi /usr/bin/uwsgi则可以在任意目录下执行 uwsgi 去启动uWSGI服务器
- 测试使用python的wsgi服务器-uWSGI- 在任意的一个目录中定义一个python脚本:hello.pydef application(env, start_response):start_response('200 OK', [('Content-Type','text/html;charset=utf-8')])return [bytes('你好啊!!','utf-8'),b'Mr_lee'] # 基于wsgi协议规范实现的代码- 启动uWSGI服务器,并部署hello.py程序uwsgi --http 192.168.248.128:8001 --wsgi-file hello.py #注意hilo.py可以写成绝对路径- 浏览器访问:192.168.248.128:8001
-
uWSGI部署Django项目
1.mysql数据库操作:
设置mysql的引擎默认为:innodb在/etc/my.cnf的[mysqld]中添加配置:default-storage-engine=InnoDB建议mysql设置为严格模式在/etc/my.cof的[mysqld]中添加配置:sql_mode=STRICT_TRANS_TABLES查看mysql的配置参数:mysql> show variables where variable_name like '%mode%';注:修改配置后,要重启Mysql服务在数据库中建好项目需要的database:“ems2_project”可使用Navicat创建,注意修改字符集为:utf8
2.在Django项目的settings.py中修改配置
DENUG = False # 去掉开发模式ALLOWED_HOSTS = ["*"] # 开放访问hostDATABASES = {'default' : 'django.db.backends.mysql','NAME' : 'ems2_project','USER' : 'root','HOST' : 'localhost' # 建议写同一个ip地址,在每个uWSGI中'PORT' : 3306,'PASSWORD' : '123456'}
3.发送项目到Linux并做移植
python manage.py makemigrationspython manage.py migrate
4.编写uWSGI的配置文件创建文件:config.ini # 建议放在项目的跟目录下
[uwsgi]http = 192.168.186.159:9000 # uWSGI服务器访问地址# uWSGI和Nginx通信的portsocket = 192.168.186.159:9001chdir = /usr/local/Django_project/ems2_project # 项目所在目录(建议存放在/usr/local/下,先创建Django_project目录)wsgi-file = ems2_project/wsgi.py # 基于项目目录的相对路径processes = 4threads = 2stats = 192.168.186.159:9002vacuum = truepidfile = /usr/local/Django_project/ems2_project/uwsgi.pid # 建议进程ID存放位置daemonize = /usr/local/Django_project/ems2_project/uwsgi.log # 建议日志文件存放位置
5.根据如上配置启动uWSGI服务器uwsgi --ini config.ini # 注意:config.ini是相对路径6.关闭服务器uwsgi -- stop uwsgi.pid # 通过进程id文件 注:如多次开启并未关闭,需要kill -9 进程id
- Centos7下安装、配置Nginx
安装:- 将tar包发送的linux- 解压 tar -zxvf nginx-1.11.1.tar.gz - 安装依赖 yum install gcc zlib-devel pcre-devel - cd到解压目录:./configure #配置检测- cd到解压目录:make && make install #编译并安装- 安装完成,安装路径为/usr/local/nginx ,日志路径为 /usr/local/nginx/logs ,可执行文件路径为 /usr/local/nginx/sbin ,配置文件路径为 /usr/local/nginx/conf欢迎页面路径为 /usr/local/nginx/html- ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx 制作连接,便于执行nginx指令- nginx #启动 nginx -s stop #关闭nginx -s reload #重启http://ip:80即可访问nginx欢迎页面
配置:到配置路径中的nginx.conf /usr/local/nginx/conf/nginx.conf
upstream ems{server 192.168.157.141:9001; # uWSGI's socket = 192.168.x.x:9001#可以在添加其他的uWSGI的服务器
}server {listen 80;server_name 192.168.157.142; # nginx服务器的ipcharset utf-8;location / {uwsgi_pass ems; #和上面的upstream转接 include /usr/local/nginx/conf/uwsgi_params; # the uwsgi_params file you installed} location /static { #http://ip:80/static/a/b/c/d.png ==> /usr/local/static/a/b/c/d.png alias /usr/local/static; # your Django project's static files - amend as required}
}
- 搭建uWSGI集群
搭建uWSGI集群,只需要多做几份uWSGI的配置文件,文件中设置不同的ip:port,指向相同的project,然后启动多个uWSGI即可。
*默认:轮询upstream django {server 192.168.0.103:8989; server 192.168.0.104:8989;}
*iphash:基于ip的负载均衡.upstream django {ip_hash;server 192.168.0.103:8989;server 192.168.0.104:8990;}
*权重轮询:upstream django {server 192.168.0.103:8989 weight=1; server 192.168.0.104:8990 weight=2; }
*最小连接数:upstream django {least_conn; server 192.168.0.103:8989;server 192.168.0.104:8990;}
至此,一个项目部署成功。