部署Django项目 uwsgi+Nginx

部署Django项目 uwsgi+Nginx

一、准备虚拟机 Ubuntu server 17.10

  • 更新系统

    sudo apt-get update #获取更新
    sudo apt-get upgrade #更新
    

  • 查看系统的服务

    service --status-all #查看所有服务
    
  • 安装openssh-server

    apt-get install openssh-server
    
  • 安装net-tools,用ifconfig查看IP

    apt-get install net-tools
    
  • 用xshell连接虚拟机

  • 上传项目

二、 python3环境准备

  • 安装pip3

    apt-get install python3-pip
    
  • 检查包

  • 安装依赖包

  • 运行Django项目测试能否跑通,检查有没有漏包

三、uwsgi配置

  • 安装uwsgi

    pip3 install uwsgi
    
  • 测试uwsgi能不能跑通

    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 #运行命令
    
  • 配置uwsig启动Django

    uwsgi mysite-uwsgi.ini # 测试命令
    配置文件内容
    [uwsgi]
    http = :9000
    #the local unix socket file than commnuincate to Nginx
    socket = 127.0.0.1:8001
    # the base directory (full path)
    chdir = /home/alex/CrazyEye
    # Django's wsgi file
    wsgi-file = CrazyEye/wsgi.py
    # maximum number of worker processes
    processes = 4
    #thread numbers startched in each worker process
    threads = 2
     
    #monitor uwsgi status
    stats = 127.0.0.1:9191
    # clear environment on exit
    vacuum          = true
    

四、Nginx配置

  • 安装 Nginx

    apt-get install nginx
    
  • 拷贝uwsgi_params到Django项目

    cp /etc/nginx/uwsgi_params /home/new/myproject/ 
    
  • 配置Nginx

    • 删除/etc/nginx/site-enabled/default

    • 创建自己的配置文件

      # the upstream component nginx needs to connect to
      upstream django {
          # server unix:///path/to/your/mysite/mysite.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 .example.com; # 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/your/mysite/media;  # your Django project's media files - amend as required
          }
       
          location /static {
              alias /path/to/your/mysite/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/your/mysite/uwsgi_params; # the uwsgi_params file you installed
          }
      }
      
  • 重启Nginx

    service nginx restart
    
  • 启动uwsgi项目

  • 可以访问了

你可能感兴趣的:(部署Django项目 uwsgi+Nginx)