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.访问网站,可设置域名解析进行访问

原文地址:https://www.cnblogs.com/open-yang/p/11256133.html