nginx和uwsgi部署Django項目

1、防火牆和端口設置

  • 開啓防火牆: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 --permanenthtml

    須要注意的是:開放端口以後須要重啓防火牆才能生效

2、項目環境安裝

  • 依賴安裝java

    yum install python3-devel zlib-devel mysql-devel libffi-devel bzip2-devel openssl-devel java wget gcc
  • 安裝nginxnode

    yum install nginx
  • 安裝redis和mysqlpython

    yum install mysql-server
    yum install redis
  • 安裝python進程管理工具supervisormysql

    yum install python3-devel zlib-devel mysql-devel libffi-devel bzip2-devel openssl-devel java wget gcc
  • 設置開機啓動nginx

    systemctl enable redis mysqld nginx supervisord

    注意:web

    1. mysql安裝以後默認不會啓動,須要使用systemctl start mysqld.service來啓動,或者mysqld.service替換爲mysqld
    2. 設置開機自啓動時是mysqld和supervisord,不是mysql和supervisor
  • 安裝python3.7redis

    • 下載源碼包: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上:sql

      • 軟鏈接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,而後重試

3、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自己沒有該文件

4、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

    6、小坑

  • 問題:部署上線後靜態文件加載不了
  • 解決辦法:

    • 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路徑配置是否正確
相關文章
相關標籤/搜索