阿里ecs centos7 中 Nginx + uwsgi 部署 Django REST framework + vue 前后端分离项目的实践

阿里centos7 中 Nginx + uwsgi 部署 Django REST framework + vue 前后端分离项目的实践

1, 项目环境

```
centos 7.4

python3.6.8

node 11

git
```

具体的安装步骤…。

2, 代码上传到服务器

  • 首先保证两个项目在本地运行是没有问题的
  • 安装虚拟环境,

pip3 install virtualenv

virtualenv --no-site-packages venv

source venv/bin/activate

  • scp 命令上传代码, 或者直接git clone 远程的git 仓库的代码(你已经有远程的git仓库)
  • pip install -r requirements.txt 将项目所需的包安装好
  • 直接 python manage.py runserver 保证项目的启动是没有问题的(方便后面出错的排查)

关于 Uwsgi

uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。

要注意 WSGI / uwsgi / uWSGI 这三个概念的区分。

  1. WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask框架写的程序)通信的一种规范。
  2. uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。
  3. 而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。
  4. uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。
    2.1, uwsgi的安装

pip install uwsgi
测试安装的结果?
创建一个test.py文件

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3

运行: uwsgi --http :8000 --wsgi-file test.py
在浏览器中访问http://ip:8000 出现 Hello World 表示uwsgi已经安装成功

2.2 使用uwsgi 启动Django项目

uwsgi --http :8000 --module mysite.wsgi

在浏览器中访问http://ip:8000
编写配置文件:

[uwsgi]
# 项目目录
chdir=/root/www/TestRunner/
# 指定项目的application
module=FasterRunner.wsgi:application
wsgi-file=FasterRunner.wsgi.py
# 指定sock的文件路径
#socket=/root/www/script/uwsgi.sock
socket=127.0.0.1:8000      # 注意这里只有在与nginx相互使用时才使用socket, 本地测试使用http,
# 进程个数
workers=5
pidfile=/root/www/script/uwsgi.pid
# 指定IP端口
#http=:8000
# 状态检测
stats=127.0.0.1:8081
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/root/www/script/uwsgi.log
# env
pythonpath=/root/www/venv/lib/python3.6/site-packages

配置文件可以参考uwsgi官网
通过配置文件启动项目

uwsgi -- ini  uwsgi.ini 

在浏览器中访问http://ip:8000 ip你的公网ip

3, nginx 安装,配置

3.1 安装: sudo apt-get install nginx
安装成功后,启动: sudo /etc/init.d/nginx start

nginx官网

查看下nginx的默认配置: nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
# 运行用户,默认是nginx

worker_processes auto;
# nginx进程数,一般设置为和cpu核数一样

error_log /var/log/nginx/error.log;
# 全局错误日志路径

pid /run/nginx.pid;
# 进程pid路径

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
# 负载动态模块
include /usr/share/nginx/modules/*.conf;

events {
# 工作模式与连接数上限
    worker_connections 1024;
   # 单个进程的最大连接数
}

http {
# 设置http服务器
    log_format  main  '$http_host $server_addr $remote_addr [$time_local] "$request" $status  $request_body  $body_bytes_sent "$http_referer" "$http_user_agent" $request_time $upstream_response_time';
    # 设置日志的格式

    access_log  /var/log/nginx/access.log  main;
    # 访问日志的路径

    sendfile            on;
    # 开启高效传输模式
    tcp_nopush          on;
    # 激活tcp_nopush参数可以允许把http response header和文件的开始放在一个文件里发布,作用是减少网络报文段的数量
    tcp_nodelay         on;
    # 激活tcp_nodelay,内核会等待将更多的字节组成一个数据包,从而提高I/O性能
    keepalive_timeout   65;
    # 长连接超时时间,单位是秒
    types_hash_max_size 2048;
    # 为了快速处理静态数据集,例如服务器名称, 映射指令的值,MIME类型,请求头字符串的名称,nginx使用哈希表

    include             /etc/nginx/mime.types;
    # 文件扩展名与类型映射表
    default_type        application/octet-stream;
    # 默认文件类型

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # 加载模块化配置文件
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
 
    server {
    # 基于域名的虚拟主机
        listen       80 default_server;
        # 监听端口
        listen       [::]:80 default_server;
        server_name  _;
        # 域名
        root         /usr/share/nginx/html;
        # 站点根目录,即网站程序存放目录

        # Load configuration files for the default server block.
        # 默认服务器块的加载配置文件
        include /etc/nginx/default.d/*.conf;

        location / {
        # 对“/”启用反向代理
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

这里注意下: include /etc/nginx/conf.d/*.conf;
为了使配置文件的结构清晰, 排错方便, 这里对前后端项目分别的配置
backend.conf

server {
    access_log  /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log ;
    uwsgi_cache_valid 1m;
    uwsgi_temp_file_write_size 64k;
    uwsgi_busy_buffers_size 64k;
    uwsgi_buffers 8 64k;
    uwsgi_buffer_size 64k;
    uwsgi_read_timeout 300;
    uwsgi_send_timeout 300;
    uwsgi_connect_timeout 300;
    listen       8001;      
    server_name  localhost;
    location /api/ {
        include  uwsgi_params;
        root     /root/www/TestRunner;
        uwsgi_pass  127.0.0.1:8000;              
        uwsgi_param UWSGI_SCRIPT FasterRunner.wsgi;
        uwsgi_param UWSGI_CHDIR  /root/www/TestRunner;
        index  index.html index.htm;
        client_max_body_size 35m;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

front.conf

# nginx 的反向代理
upstream django {
server 39.107.76.94:8001;
}
server {
    listen 9090;  # 我要监听那个端口
    server_name localhost;  # 你访问的路径前面的url名称
    error_page  404           /404.html;  # 错误页面
    error_page   500 502 503 504  /50x.html;  # 错误页面

    # 指定项目路径uwsgi
    location / {
            index index.html index.htm;
            root /root/www/TestWeb/dist;
    }
    location /static {
            root /root/www/TestWeb/dist;
    }
    # 与开头处相对应
   location /api {
    proxy_pass http://django;
    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值