django部署(gunicorn版本)

django部署

采用 nginx + gunicorn + django部署,系统是centos7

安装环境

  • nginx 安装

    1. 官网下载安装http://nginx.org/en/docs/

    2. 配置文件

      user  root;
      worker_processes  1;
      events {
          worker_connections  1024;
      }
      http {
          include       mime.types;
          default_type  application/octet-stream;
          sendfile        on;
          server {
              listen 80;
              server_name  youtubemp4.us;
              charset utf-8;
              client_max_body_size 10m;
              access_log  /var/log/nginx/build-access.log;
              error_log  /var/log/nginx/build-err.log;
              location /static/ {
                  #静态文件如js,css的存放目录
                  alias /root/en7/static/;
              }
              location / {
                  proxy_pass http://0.0.0.0:8000; # 这里要配合启动文件使用
                  proxy_set_header   Host                 $http_host;
                  proxy_set_header   X-Real-IP            $remote_addr;
                  proxy_set_header   X-Forwarded-For      $proxy_add_x_forwarded_for;
                  proxy_set_header   X-Forwarded-Proto    $scheme;
              }
          }
      }
      
    3. 软连接

      ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

    4. 命令

      nginx -c xxx.conf
      nginx -s reload
      
  • gunicorn

    1. 安装

    pip install gunicorn

    1. 配置文件

      # /usr/bin/python3
      # encoding: utf-8
      import multiprocessing
      bind = '0.0.0.0:8000'      #绑定ip和端口号
      backlog = 512                #监听队列
      chdir = '/root/en7'  #gunicorn要切换到的目的工作目录
      timeout = 30      #超时
      
      workers = multiprocessing.cpu_count() * 2 + 1    #进程数
      threads = 2 #指定每个进程开启的线程数
      # 设置守护进程
      daemon = True
      
    2. 命令

      gunicorn 模块名.wsgi:application -c xxx.conf
      kill -9 pid
      
  • django

    1. 修改settings文件

      DEBUG = False
      ALLOWED_HOSTS = ["149.28.39.105","youtubemp4.us"]
      STATIC_ROOT = os.path.join(BASE_DIR, 'static')
      
      # STATICFILES_DIRS = [
      #     os.path.join(BASE_DIR,"static")
      # ]
      
    2. 执行收集静态文件

      python manager.py collectstatic
      

部署步骤

拉取django项目

gunicorn启动django项目

启动nginx服务器

posted @ 2020-07-23 22:19  huameixiao  阅读(396)  评论(0编辑  收藏  举报