Django项目开发完毕后,需要将代码放到服务器上,这样用户才能访问。接下来我们一步一步来进行一波部署操作。
Django项目部署步骤:
1、将需要部署的项目先压缩成.zip的压缩包
2、上传的服务器方法
mac与linux系统 scp 本地文件路径 root@服务器ip:存储位置 例:scp D:abc/lyb.zip root@192.168.0.0:/home/servers windows: ftp工具上传:filezilla 下载地址:https://filezilla-project.org/download.php?type=client xshell工具: 进入到服务器预定存储系统目录下,将.zip包在本地直接拖拽到服务器中
3、安装python解释器
4、Django框架
pip3 install django == 1.11.11
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
报错:no module _sqlite3 解决办法: 1、yum install sqlite-devel 2、再次重新编译安装Python3 ./configure make make install
5、解压.zip压缩包
yum install unzip # 安装解压工具
unzip 压缩包名字加后缀
6、修改settings.py文件
ALLOWED_HOSTS = ['*',] # 定义hosts地址 DEBUG = False # 关闭DEBUG模式 STATIC_ROOT = 'allstatic' # 配置项目静态文件存储路径
7、找到Django程序中每一个app和全局的静态文件,放置到某处
python manage.py collectstatic
8、安装Nginx来做反向代理
# 安装:
yum install nginx
9、Nginx的配置文件【默认配置文件位置:/etc/nginx/nginx.conf】
user root; # 当前用户 worker_processes 4; # 开启的进程数【一般和CPU核数一致】 error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; # 监听多少个socket变化 } 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_nodelay on; keepalive_timeout 65; include /etc/nginx/mime.types; default_type application/octet-stream; upstream django { server 127.0.0.1:8001; } server { listen 80; charset utf-8; # max upload size client_max_body_size 75M; location /static { alias /data/s9deploy/allstatic; # allstatic的路径 } location / { # 其他url处理 uwsgi_pass django; # 与上面upstream django关联 include uwsgi_params; } } }
10、启动Nginx
systemctl start nginx
11、安装uwsgo来替换wsgiref
pip3 install uwsgi
12、使用uwsgo代替wsgi运行Django项目
1》复杂运行方式:
代替wsgi运行django: 不处理静态文件: uwsgi --http 0.0.0.0:8001 --chdir /data/s9deploy/ --wsgi-file s9deploy/wsgi.py 处理静态文件: python manage.py collectstatic # /data/s9deploy/allstatic uwsgi --http 0.0.0.0:8001 --chdir /data/s9deploy/ --wsgi-file s9deploy/wsgi.py --static-map /static=/data/s9deploy/allstatic
2》简单运行方式
# 简单运行方式: vim uwsgi_s9deploy.ini # 添加一下内容 [uwsgi] http = 0.0.0.0:8001 # 所有人都可以访问 # scoket = 0.0.0.0:8001 # 仅能本机访问 chdir = /data/s9deploy/ wsgi-file = s9deploy/wsgi.py process = 4 # 开启进程数 static-map = /static=/data/s9deploy/allstatic # 启动 uwsgi --ini /data/s9deploy/uwsgi_s9deploy.ini
负载均衡或集群
1、配置uwsgi配置文件来进行负载均衡
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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_nodelay on; keepalive_timeout 65; include /etc/nginx/mime.types; default_type application/octet-stream; upstream django { server 127.0.0.1:8001; # 多开几个服务器,来分担压力 server 127.0.0.1:8002; } server { listen 80; charset utf-8; # max upload size client_max_body_size 75M; location /static { alias /data/s9deploy/allstatic; } location / { uwsgi_pass django; include uwsgi_params; } } }
2、复制上文多个uwsgi_s9deploy.ini文件修改文件的文件名
mv uwsgi_s9deploy.ini uwsgi_s9deploy_8001.ini # 修改名字 cp uwsgi_s9deploy_8001.ini uwsgi_s9deploy_8002.ini # 复制 # 修改 uwsgi_s9deploy_8002.ini的内容 # 将http修改为8002 [uwsgi] http = 0.0.0.0:8002 # 所有人都可以访问 # scoket = 0.0.0.0:8001 # 仅能本机访问 chdir = /data/s9deploy/ wsgi-file = s9deploy/wsgi.py process = 4 static-map = /static=/data/s9deploy/allstatic
3、运行Django项目【启用集群或负载均衡】
uwsgi --ini /data/s9deploy/uwsgi_s9deploy_8001.ini & # 添加&后台运行的意思 uwsgi --ini /data/s9deploy/uwsgi_s9deploy_8002.ini & # 启用两个Django程序进行负载均衡
升级版项目上传方法
第二阶段:管理工具,ansible/fabric/puppet/saltstack 将本地代码打包,通过管理工具,将代码上传到每一台服务器上。 第三阶段:基于git + 管理工具 运维人员: git clone -b v1.0 http://www.github.com/xxx/xxx/xx 编译: 再通过管理工具,将代码上传到每一台服务器上。 第四阶段:jekins web程序 基于git + 编译 + 管理工具
附加
安装supervisor,检测进程不要被关闭。
supervisor是一个对进程管理的软件,可以帮助我们启动uwsgi并维护(uwsgi进程关闭时,自动将其启动起来)。
a. 安装
yum install supervisor
b. 配置 vim /etc/supervisor.conf
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
[supervisord] http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server) ;http_port=127.0.0.1:9001 ; (alternately, ip_address:port specifies AF_INET) ;sockchmod=0700 ; AF_UNIX socketmode (AF_INET ignore, default 0700) ;sockchown=nobody.nogroup ; AF_UNIX socket uid.gid owner (AF_INET ignores) ;umask=022 ; (process file creation umask;default 022) logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) logfile_backups=10 ; (num of main logfile rotation backups;default 10) loglevel=info ; (logging level;default info; others: debug,warn) pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) nodaemon=false ; (start in foreground if true;default false) minfds=1024 ; (min. avail startup file descriptors;default 1024) minprocs=200 ; (min. avail process descriptors;default 200) ;nocleanup=true ; (don't clean up tempfiles at start;default false) ;http_username=user ; (default is no username (open system)) ;http_password=123 ; (default is no password (open system)) ;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP) ;user=chrism ; (default is current user, required if root) ;directory=/tmp ; (default is not to cd during start) ;environment=KEY=value ; (key value pairs to add to environment) [supervisorctl] serverurl=unix:///var/tmp/supervisor.sock ; use a unix:// URL for a unix socket ;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket ;username=chris ; should be same as http_username if set ;password=123 ; should be same as http_password if set ;prompt=mysupervisor ; cmd line prompt (default "supervisor") ; The below sample program section shows all possible program subsection values, ; create one or more 'real' program: sections to be able to control them under ; supervisor. ;[program:theprogramname] ;command=/bin/cat ; the program (relative uses PATH, can take args) ;priority=999 ; the relative start priority (default 999) ;autostart=true ; start at supervisord start (default: true) ;autorestart=true ; retstart at unexpected quit (default: true) ;startsecs=10 ; number of secs prog must stay running (def. 10) ;startretries=3 ; max # of serial start failures (default 3) ;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) ;stopsignal=QUIT ; signal used to kill process (default TERM) ;stopwaitsecs=10 ; max num secs to wait before SIGKILL (default 10) ;user=chrism ; setuid to this UNIX account to run the program ;log_stdout=true ; if true, log program stdout (default true) ;log_stderr=true ; if true, log program stderr (def false) ;logfile=/var/log/cat.log ; child log path, use NONE for none; default AUTO ;logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) ;logfile_backups=10 ; # of logfile backups (default 10) [program:oldboy] command=/usr/local/bin/uwsgi /data/oldboy/oldboy.ini ;命令 priority=999 ; 优先级(越小越优先) autostart=true ; supervisord启动时,该程序也启动 autorestart=true ; 异常退出时,自动启动 startsecs=10 ; 启动后持续10s后未发生异常,才表示启动成功 startretries=3 ; 异常后,自动重启次数 exitcodes=0,2 ; exit异常抛出的是0、2时才认为是异常 stopsignal=QUIT ; 杀进程的信号 stopwaitsecs=10 ; 向进程发出stopsignal后等待OS向supervisord返回SIGCHILD 的时间。若超时则supervisord将使用SIGKILL杀进程 user=chrism ; 设置启动该程序的用户 log_stdout=true ; 如果为True,则记录程序日志 log_stderr=false ; 如果为True,则记录程序错误日志 logfile=/var/log/cat.log ; 程序日志路径 logfile_maxbytes=1MB ; 日志文件最大大小 logfile_backups=10 ; 日志文件最大数量
c. 启动
supervisord /etc/supervisor.conf
或
/etc/init.d/supervisor start