关于nginx:nginx和uwsgi部署Django项目

一、防火墙和端口设置

  • 开启防火墙:systemctl start firewalld
  • 敞开防火墙:systemctl stop firewalld
  • 查看防火墙状态:systemctl status firewalld
  • 重启防火墙:systemctl restart firewalld
  • 设置开机自启:systemctl enable nginx firewalld
  • 查看凋谢端口列表:firewall-cmd –list-ports
  • 凋谢指定的端口:firewall-cmd –zone=public –add-port=端口号/tcp –permanent

    须要留神的是:凋谢端口之后须要重启防火墙能力失效

二、我的项目环境装置

  • 依赖装置

    yum install python3-devel zlib-devel mysql-devel libffi-devel bzip2-devel openssl-devel java wget gcc
  • 装置nginx

    yum install nginx
  • 装置redis和mysql

    yum install mysql-server
    yum install redis
  • 装置python过程管理工具supervisor

    yum install python3-devel zlib-devel mysql-devel libffi-devel bzip2-devel openssl-devel java wget gcc
  • 设置开机启动

    systemctl enable redis mysqld nginx supervisord

    留神:

    1. mysql装置之后默认不会启动,须要应用systemctl start mysqld.service来启动,或者mysqld.service替换为mysqld
    2. 设置开机自启动时是mysqld和supervisord,不是mysql和supervisor
  • 装置python3.7

    • 下载源码包:wget https://www.python.org/ftp/py…
    • 解压:tar -xf Python-3.7.8rc1.tar.xz
    • 在/usr/local下新建python3目录,用于装置python3.7.8
    • 进入python3.7.8源码包装置:./configure –prefix=/usr/local/python3 –enable-optimizations
    • 执行命令:make && make install,吐过上一步增加了–enable-optimizations,这一步会比拟耗时间,这个参数能够不加
    • 把python3和pip3软连贯到/usr/bin上:

      • 软连贯python3:ln /usr/local/python3/bin/python3 /usr/bin/python3
      • 软连贯pip3:ln /usr/local/python3/bin/pip3 /usr/bin/pip3

      问题:应用pip3 install -r requirements.txt装置依赖时,报错:Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-build-ifh_bc24/cryptography/
      解决办法:命令行pip3 install –upgrade setuptools,而后重试

三、Django部署中nginx设置

  • nginx配置文件

    # For more information on configuration, see:
    #   * Official English Documentation: http://nginx.org/en/docs/
    #   * Official Russian Documentation: http://nginx.org/ru/docs/
    
    user root;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
    
    # Load dynamic modules. See /usr/share/nginx/README.dynamic.
    #include /usr/share/nginx/modules/*.conf;
    
    events {
      worker_connections 1024;
    }
    
    http {
      log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';
    
      access_log  /var/log/nginx/access.log  main;
    
      sendfile            on;
      tcp_nopush          on;
      tcp_nodelay         on;
      keepalive_timeout   65;
      types_hash_max_size 2048;
    
      include             /etc/nginx/mime.types;
      default_type        application/octet-stream;
    
      # Load modular configuration files from the /etc/nginx/conf.d directory.
      # See http://nginx.org/en/docs/ngx_core_module.html#include
      # for more information.
      include /etc/nginx/conf.d/*.conf;
    
      upstream uwsgi_backend {  # http申请转发配置
          server localhost:8888;
      }
    
      upstream channels-backend {  # websocket申请转发配置
          server localhost:8000;
      }
    
      server {
          listen       80 default_server;
          listen       [::]:80 default_server;
          server_name  www.ejpro.top; # 显示在浏览器上的名称
          charset utf-8;
          client_max_body_size 75M; #客户端申请体最大的限度
    
          # Load configuration files for the default server block.
          include /etc/nginx/default.d/*.conf;
    
    
          ##### 动态文件设置
          location /static/ {
              root  /home/tourism;  # static文件所在的目录门路
          }
    
          location /media/ {
              root  /home/tourism;  # media文件所在的目录门路
          }
    
    
          #### 动静转发申请
          location / {
              uwsgi_pass 127.0.0.1:8001;
              include /etc/nginx/uwsgi_params; # 指向的是nginx,Django自身没有该文件
          }
      }
    }
    
  • 注意事项:

    • 正文掉include /usr/share/nginx/modules/*.conf,如果不正文这行nginx会默认应用/etc/nginx/nginx.conf.default文件,正文掉后nginx就会应用nginx.conf的配置
    • 动态文件门路配置,static和media的配置门路都只能写到上一层,不能蕴含static和media这一层,这一点必须要留神,否则会呈现如下谬误:

      [error] 14635#0: *49 open() "/home/tourism/static/static/admin/simpleui-x/particles/particles.js"
      # 多写了一层static
    • 动静申请中须要留神的是:uwsgi_params门路在/etc/nginx下,Django自身没有该文件

四、uwsgi.ini配置

  • 配置文件

    [uwsgi]
    # Django manage.py 所在文件夹门路
    #Django我的项目源码目录
    chdir = /home/tourism
    # Django零碎的uwsgi,如果不记得能够从settings.py文件中WSGI_APPLICATION查看
    module = project.wsgi:application
    # 启用master过程治理
    master = true
    # 绑定的 UNIX socket
    socket = 127.0.0.1:8888
    # uwsgi的过程数
    processes = 4
    # 重启进行耗时超过30秒就敞开
    harakiri = 30
    # 最大申请解决数,之后从新生成过程
    max-requests = 5000
    # 退出时清理环境
    vacuum = true
    # socket
    socket = 127.0.0.1:8001
    # uid
    uid = 1000
    # gid
    gid = 2000
    
    
    # pidfile门路
    pidfile = /home/tourism/deploy/sub/master.pid
    # deamonize日志文件门路
    deamonize = /home/tourism/deploy/sub/tourism.log
    # 当服务器敞开或退出时是否革除pidfile和deamonize文件
    vacuum=True
    
    static-map = /static = /home/tourism/static
  • 如果部署上线后我的项目在虚拟环境中,还须要另外配置home参数,即python的虚拟环境。如果没有虚拟环境,不须要配置该项
  • 须要配置static-map = /static = /home/tourism/static

    六、小坑

  • 问题:部署上线后动态文件加载不了
  • 解决办法:

    • nginx中把第一行 user nginx改成user root,进步权限
    • nginx中的static和media配置门路,门路不能包含static和media
    • 配置Django我的项目权限:chmod 777 我的项目门路
    • 在uwsgi.ini中增加static-map = /static = static的绝对路径
    • 收集动态文件:python3 manage.py collectstatic
    • 留神查看settings.py文件中的static和media门路配置是否正确

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理