uwsgi+anaconda+nginx部署django项目(ubuntu下)

conda 环境不必多说: conda(或source)  activate  test 进入test虚拟环境

接下来安装uwsgi:

  pip install uwsgi 在conda环境下大概率安装不成功,可以使用一下命令代替:

    conda install -c conda-forge uwsgi 

  运行uwsgi 有可能碰到 libiconv.so 动态库找不到的问题,同样可以用conda安装  

    conda install -c conda-forge libiconv

uwsgi安装好后,需要在django项目根目录下建立一个名为uwsgi.ini的文件,在里面配置好uwsgi:

  

[uwsgi]
socket=127.0.0.1:8000                                  # 指定项目执行的端口号,用nginx的时候就要配socket
pythonpath=/home/admin/test/ # 指定项目所在位置,在使用conda虚拟环境时必须要有这一条
chdir=/home/admin/test/                  # 指定项目的目录
wsgi-file=test/wsgi.py # 项目上wsgi.py所在的位置,与settings目录相同
processes=1                          # 开启的进程数量
threads=2
master=True # master :允许主线程存在(true)
pidfile=uwsgi.pid
daemonize=uwsgi.log # 日志,uwsgi无法启动时来这查看错误日志

uwsgi配置好后,要启动uwsgi, 启动命令:

  uwsgi --ini uwsgi.ini          # 启动uwsgi, 一定要在django实际使用的conda环境下,否则会报错

  uwsgi --stop uwsgi.pid      # 停止uwsgi

  uwsgi --reload uwsgi.pid   #  重启uwsgi

uwsgi配置好后,要配置nginx:

  首先安装nginx, 运行命令      sudo apt install nginx

  nginx 命令:

    sudo service nginx start    启动

    sudo service nginx stop   停止

    sudo service nginx restart  重启nginx

    sudo nginx -s reload 重载配置文件

    sudo nginx -s quit 优雅的停止nginx

    sudo nginx -s term 停止nginx

    sudo nginx -s reopen 打开一份新的日志

  配置nginx:

    在/etc/nginx/site-available下新建文件test.conf:    server {

        listen 8888;                                        #nginx监听的端口
        charset utf-8;
        client_max_body_size 75M;
        location / {
          uwsgi_pass 127.0.0.1:8000;                  # 与uwsgi中配置的相一致
          include /etc/nginx/uwsgi_params;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

      location /static/ {   # 如果用到静态文件
        alias /home/test/test/static/;
      }

}

    然后要建立软连接:

      sudo ln -s /etc/nginx/site-available/test.conf /etc/nginx/site-enabled/test.conf

  最后用 sudo nginx -s reload 命令启动重载配置文件即可

最后, 如果需要配置负载均衡:

// http模块中配置upstream
upstream test {
   server 127.0.0.1:8002 weight=2;
   server 127.0.0.1:8003 weight=1;
}

// 将server模块中的location替换为如下
location / {
                proxy_pass http://test;
}
原文地址:https://www.cnblogs.com/jiaxiaoxin/p/10642263.html