nginx+uwsgi+django

Posted open-yang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+uwsgi+django相关的知识,希望对你有一定的参考价值。

以配置文件形式运行项目:

  nginx默认端口80,反向代理(uwsgi配置)端口8000

  1.配置pyhton环境

    (1)导入django项目,创建虚拟环境,加载项目需要的模块

    (2)虚拟环境运行----------workon 虚拟环境名称

    (3)pip 导入uwsgi模块-----pip3 install -i https://pypi.douban.com/simple uwsgi  

  2.配置uwsgi

    (1)在项目下手动创建一个uwsgi.ini配置文件:

# uwsgi的配置文件 

[uwsgi] 

# Django-related settings

# the base directory (full path)

#项目的绝对路径,定位到nbcrm的第一层

chdir = /opt/djangoprojects/crm01 

# Django‘s wsgi file

# 找到项目第二层的wsgi文件

module = crm01.wsgi

 

# the virtualenv (full path)

# 找到虚拟环境的绝对路径

home = /root/Envs/crm_env3

 

# process-related settings

# master

# 主进程

master = true

 

# maximum number of worker processes

# 开启uwsgi的多进程数,根据cpu核数来定义

processes = 2

 

# the socket (use the full path to be safe

# 基于socket链接运行crm,只有与nginx结合的时候,才使用socket形式,指定django项目的服务器和端口

socket = 0.0.0.0:8000

# 当你没用nginx,调试项目的时候,使用http形式

#http = 0.0.0.0:8000

 

# ... with appropriate permissions - may be needed

# chmod-socket = 664

# clear environment on exit

vacuum = true

 

#指定一个参数,日志放在哪

 

#如果你使用了supervisor,请注释掉这个参数

#守护进程在后台运行,且将日志信息,输出到uwsgi.log日志中,或者参数设置yes,直接后端运行

daemonize = uwsgi.log

    (2)启动配置文件(可以使用绝对路径)----------------uwsgi --ini uwsgi.ini   

    (3)uwsgi默认不支持静态文件解析,使用nginx去解析静态文件 

  3.配置nginx

    (1)修改nginx.conf配置文件

      nginx结合uwsgi进行请求转发

server

  listen 80;

  server_name localhost;

  location /

    include uwsgi_params;

    uwsgi_pass 0.0.0.0:8000;

  

如果设计了负载均衡,uwsgi_pass同样采用upsetream地址池

    (2)nginx处理静态文件:

       ①django项目的settings.py静态文件配置          

          STATIC_ROOT=‘/opt/djangoprojects/crm01_statics‘

          STATIC_URL = ‘/static/‘

          STATICFILES_DIRS = [os.path.join(BASE_DIR,‘statics‘),]

       ②执行命令,收集crm的静态文件到配置的文件夹

          python3 /opt/djangoprojects/crm01/manage.py collectstatic

       ③配置nginx的server中location路径匹配          

          location /static

            alias /opt/djangoprojects/crm01_statics/;

          

    (3)启动nginx(nginx -s reload重新载入)

  4.访问网站,可设置域名解析进行访问

 

以上是关于nginx+uwsgi+django的主要内容,如果未能解决你的问题,请参考以下文章

Django部署——uwsgi+Nginx(超详细)

通过 Nginx 的 uwsgi + django - uwsgi 设置/生成?

nginx 和uwsgi的区别与作用

nginx +uwsgi + django配置

Nginx与uWSGI交互

nginx + uwsgi: -- 请求的不可用修饰符:0 --