一, 背景 前台Vue项目构建部署到nginx服务器上,接下来部署后台,不再使用python managy.py runserver 0.0.0.0:10082测试方式,而是使用 uwsgi 启动 django 二, 部署流程 [在虚拟环境中] 0. . /root/venv/bin/activate 1. settings.py STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATICFILES_DIRS = ( os.path.join(BASE_DIR, 't3static'), ) 2. python manage.py collectstatic 3. pip3.6 install uwsgi 4. 与manage.py 同级创建 uwsgi.ini 文件 [uwsgi] # 用户自定义变量,方便后面引用 project = orange username = root base = /opt/gits # 表示需要操作的目录,也就是项目的目录 chdir = %(base)/%(project) wsgi-file = webhook/wsgi.py # python 虚拟环境 home = /root/venv/ module = %(project).wsgi:application master = true processes = 20 uid = %(username) gid = %(username) socket = /var/run/%(project).sock pidfile = /var/run/%(project).pid chown-socket = %(username):%(username) chmod-socket = 666 vacuum = true # 记录日志,不要忘记创建目录及权限,注意这会导致进程为后台进程,即不可使用supervisor管理 daemonize = %(base)/log/%(project).log #当python源文件变化时会自动加载,一般开发环境使用 py-autoreload =1 harakiri = 600 5. 启动 uwsgi uwsgi --ini uwsgi.ini [uWSGI] getting INI configuration from uwsgi.ini [uwsgi-static] added mapping for /static => /opt/git/orange/static 6. 配置nginx , 使用nginx做uwsgi代理 /usr/local/nginx/conf/nginx.conf include /usr/local/nginx/conf/conf.d/*.cnf; /usr/local/nginx/conf/conf.d/uwsgi_orange.cnf server { listen 10082 default_server; server_name _; client_max_body_size 50m; client_header_buffer_size 10m; large_client_header_buffers 4 10m; location / { root /opt/gits/orange; #index index.html; uwsgi_send_timeout 1800s; # 指定向uWSGI传送请求的超时时间,完成握手后向uWSGI传送请求的超时时间。 uwsgi_connect_timeout 1800s; # 指定连接到后端uWSGI的超时时间。 uwsgi_read_timeout 1800s; # 指定接收uWSGI应答的超时时间,完成握手后接收uWSGI应答的超时时间。 #uwsgi_pass 127.0.0.1:8000; uwsgi_pass unix:///var/run/orange.sock; include /usr/local/nginx/conf/uwsgi_params; } location /static { alias /opt/gits/orange/static; } #通过这个参数,定义错误页面的文件 ,当状态码是 404 400 401 时,返回40x.html页面 error_page 404 401 400 403 /40x.html; } /usr/local/nginx/sbin/nginx -s reload 三,测试验证后台应用 ps aux | grep uwsgi # 有进程存在 打开浏览器 http://192.168.89.133:10082 网页存在,登录没有问题,则成功 四,封装启动脚本 /usr/local/bin/restart_orange.sh #!/bin/bash prj_base='/opt/gits/orange/' PY_VENV=/root/venv uwsgi_ini=/opt/gits/orange/uwsgi.ini #判断用户是否为root if [ `whoami` != "root" ]; then echo "only root user can run !!!" echo "运行uwsgi的用户必须为root" exit 1 fi ps aux |egrep ${uwsgi_ini} |grep -v 'grep' if [ $? -eq 0 ];then ps aux |egrep ${uwsgi_ini} |grep -v 'grep' |awk '{print $2}' |xargs kill -9 if [ $? -eq 0 ];then echo "杀死uwsgi成功" else echo "杀死uwsgi失败" exit 6 fi else echo "没有uwsgi在运行" fi source ${PY_VENV}/bin/activate cd ${prj_base} chmod 777 ${prj_base}manage.py #${prj_base}manage.py collectstatic &>/dev/null #if [ $? -eq 0 ];then # echo "静态文件收集成功" #else # echo "静态文件收集失败,请手工收集" #fi uwsgi --uid root --gid root --ini ${uwsgi_ini} if [ $? -eq 0 ];then echo "uwsgi启动成功" fi chmod +x restart_orange.sh restart_orange.sh 杀死uwsgi成功 [uWSGI] getting INI configuration from /opt/gits/orange/uwsgi.ini uwsgi启动成功 ps aux | grep /opt/gits/orange/uwsgi.ini root 94529 0.7 1.0 357048 41548 ? S 20:41 0:00 uwsgi --uid root --gid root --ini /opt/gits/orange/uwsgi.ini root 94534 0.1 0.9 357304 34996 ? Sl 20:41 0:00 uwsgi --uid root --gid root --ini /opt/gits/orange/uwsgi.ini root 94535 0.1 0.9 357304 34988 ? Sl 20:41 0:00 uwsgi --uid root --gid root --ini /opt/gits/orange/uwsgi.ini ... ... 五,Vue前端项目使用nginx代理的uwsgi提供的Django后端服务 前端Vue因为要访问后端nginx代理的uwsgi接口,所以需要修改 nginx 配置,打通前后端接口连通性。 vim /usr/local/nginx/conf/conf.d/orange_v1.cnf server { listen 80; server_name www.orange.com; location / { try_files $uri $uri/ /index.html; root /usr/local/nginx/html/orange_v1; index index.html index.htm; } location /api { proxy_pass http://192.168.89.133:10082; } } /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful /usr/local/nginx/sbin/nginx -s reload 六,全局性测试 因为已经做了前端域名解析服务,所以,浏览器打开直接访问服务对应的域名即可,http://www.orange.com 输入错误用户名和密码,有错误提示,输入正确的,登录后没有问题,则验证通过。
云计算之VUE-Django开发
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.