Django部署
1.方案一
client—>Nginx(openresty) —>gunicorn(通过wsgi启动托管) —>django(应用服务)
2.方案二
Nginx(openresty,upstream) —> supervisor(monitor) —> gunicorn —> django(应用服务)
3.上传项目到服务器
scp -r 文件夹 [email protected]:/x/(服务器存放地址)
4.创建运行环境
apt-get install virtualenv # 创建安装虚拟环境的工具
virtualenv name -p /usr/bin/python3.6 # 创建环境,选python版本
source name/bin/activate # 进入环境
deactivate # 退出环境
5.安装导出的依赖的包
pip install -r requirements.txt
6.安装nginx
非阻塞IO(高并发)
7.下载Openresty
(1)wget 下载工具
wget https://openresty.org/download/.....
(2)第三方库Openresty基于nginx与lua的高性能WEB平台,内部集成大量的lua库
(3)将用户与用户组改,不要为root
sudo chown user:user -Rf /opt/openresty
(4)安装包:sudo apt-get install libpcre3 libpcre3-dev openssl libssl-dev
(5)进入安装目录下执行安装
.configure --prefix=/opt/openresty
make install
测试:curl url 运行网址
8.启动
提示创建的软链接在哪 可以用此启动
9.配置nginx/conf
(1)例:location /hello{
default_type text/html;
content_by_lua 'ngx.say("<p>hello world!</p>")';
}查看是否成功
域名可以有多个,用空格隔开
(2)负载均衡配置(权重)
upstream xxx(www.xx.com){
server 127.0.0.1:8000 weight=3;
server 127.0.0.1:8001 weight=2;
server 127.0.0.1:8002 weight=1;
}
(3)location / {
proxy_pass http://www.xx.com(同上xxx)
}
(4)静态文件location /static{
alias(映射位置) /home/xx/static/ (你的项目的static)
expires 1d; (静态资源更新的时间)
}
注意:把/xadmin/static/xadmin文件移动到/static下
10.安装gunicorn(或uwsgi)
(1)只支持在Unix系统上运行,使用prefork master-worker模型,能够与wsgi
web框架协作
(2)pip install gunicorn
which nohup
(3)nohup gunicorn --chdir /home/用户名/www(地址)/project_name
project_name.wsgi:applocation --bind 0.0.0.0:8001 --workers=1 &
11.安装Supervisor与配置(监控gunicorn进程)
管理进程
(1)apt-get install supervisor
cd /etc/supervisor cd conf.d
vim 项目名.conf (创建文件) # 具体文件
/etc/supervisor/supervisord.conf 启动时默认加载的配置文件
(指定socket文件,log文件,pid文件)
(2)配置信息
[group:xsapp(随意起)]
programs=art1,art2
[program:art1]
nohup gunicorn --chdir /home/用户名/www/project_name
project_name.wsgi:applocation --bind 0.0.0.0:8001 --
workers=1
directory=/home/用户名/文件夹/项目(项目地址)
user=用户名
autorestart=true
redirect_stderr=true
stdout_logfile=log/名.log
loglevel=info
stopsignal=INT
有几个就复制几个
12.创建log文件夹,存放日志文件,在项目下创建
修改 /etc/supervisor/supervisord.conf 文件(有截图)
13.supervisord -n 默认启动
一定进到项目下启动,路径是相对的
supervisord : 启动
supervisor, -h 查看帮助
supervisorctl reload :修改完配置文件后重新启动
supervisor
supervisorctl status :查看
supervisor监管的进程状态
supervisorctl start 进程名 :启动XXX进程
supervisorctl stop 进程名 :停止XXX进程
supervisorctl stop all:停止全部进程,注:start、restart、stop都不会载
入最新的配置文件。
supervisorctl update:根据最新的配置文件,启动新配置或有改动的进程,配置
没有改动的进程不会受影响而重启