Ubuntu Django2 + uwsgi + nginx (前后端分离配置)

  • 修改后端项目文件下,setttings.py
    DEBUG = False
    
    ALLOWED_HOSTS = ['*',]
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'database_name',
            'USER': 'username',
            'PASSWORD': 'passwd',
            'HOST': '127.0.0.1',
            'PORT': '3306',
        },
    }
  • 安装mysql
    sudo apt-get install mysql-server -y
    sudo apt-get install mysql-client
    sudo apt-get install libmysqlclient-dev -y

    (开启远程访问)
    新建mysql用户

    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

    进入该用户建立xxx数据库

  • 安装Python3
    sudo apt-get install python3.5
  • 将后端项目文件传送(xftp, git)到某路径下 /path/to/project, 前端文件也传到某一路径下 /path/to/www
    sudo apt-get install python-software-properties -y
    sudo apt-get install python3.5-dev python3.5-venv -y
    sudo apt install python-pip -y
  • 在某路径下创建虚拟环境/path/to/env
    python3.5 -m venv env
    source /path/to/env/bin/activate
  • 进入/path/to/project
    pip3 install -r requirments.txt
    pip3 install django-rest-captcha==0.1.0(这个老是不下载不懂为啥子)
  • 下载uwsgi
    pip3 install uwsgi
  • 安装nginx
    sudo apt install nginx -y
    cp /etc/nginx/uwsgi_params /path/to/project
  • 配置uwsgi.ini
    找个存放的位置,/path/to/uwsgi.ini
    [uwsgi]
    chdir=/path/to/project
    module=project.wsgi:application
    home=/path/to/env
    master=true
    socket=:8001
    chmod-socket = 666
    vacuum=true
  • 项目目录下新建nginx配置文件
    vim gdpro_ng.conf
    upstream django {
        # server unix:/root/xueyiwang/xueyiwang.sock; # for a file socket
        server 127.0.0.1:8001; # for a web port socket (we'll use this first)
    }
     
    # configuration of the server
    server {
        # the port your site will be served on
        listen      8000;
        # the domain name it will serve for
        server_name 127.0.0.1; # substitute your machine's IP address or FQDN
        charset     utf-8;
     
        # max upload size
        client_max_body_size 75M;   # adjust to taste
     
        # Django media
        location /media  {
            alias /path/to/project/media;  # your Django project's media files - amend as required
        }
     
        location /static {
            alias /path/to/project/static; # your Django project's static files - amend as required
        }
     
        # Finally, send all non-media requests to the Django server.
        location / {
            uwsgi_pass  django;
            include   /path/to/project/uwsgi_params; # the uwsgi_params file you installed
        }
    }
  • 前端文件 /path/to/www 添加nginx配置文件 dsa_ng.conf
    server {
        listen      80;
        server_name 外网ip;
        charset     utf-8;
     
        client_max_body_size 75M;
     
        location / {
            root  /path/to/www;
            index index.html;
        }
    }
  • 添加软连接
     sudo ln -s /path/to/project/gdpro_ng.conf /etc/nginx/sites-enabled/ 
     sudo ln -s /path/to/www/dsa_ng.conf /etc/nginx/sites-enabled/
  • 重启nginx
    sudo service nginx restart
    # 每次重启都要使用下面指令开启uwsgi
    # 百度搞个自启脚本吧
    uwsgi -ini /path/to/uwsgi.ini
posted @ 2019-09-17 21:21  xxx_fna  阅读(128)  评论(0)    收藏  举报