• python | Linux 搭建Nginx+uWSGI+Django环境


    1. 安装环境

      sudo apt-get install nginx
      sudo apt install python3
      sudo apt install python3-pip

      使用 sudo pip3 install 安装 下面环境

      pip3 install 
                      uwsgi
                      Django
                      django-cors-headers  # 跨域
                      mysqlclient    # 连接MySQL
                      wechatpy[cryptography]  #微信公众平台SDK
                      xlrd  # 表格插件

      然后在项目文件夹根目录执行下面命令,然后就可以在项目根目录下面找到后端的样式资源(static文件夹下)了

      python3 manage.py collectstatic
    2. 环境配置

      cd /etc/nginx/sites-enabled/ 

      创建一个配置文件,如:demo,里面的内容如下,当前示例配置文件是前后端分离(Vue和Django):

      server {
          listen 80;
          listen [::]:80;
      
          server_name demo.zhuchenglin.cn;
      
          # 后端Django的样式
          location /static/admin/ {
                       expires 30d;
                       autoindex on;
                       add_header Cache-Control private;
                       alias /var/www/html/demo/back/static/admin/;
          }
          # 前端样式
           location /static/ {
                     expires 30d;
                      autoindex on; 
                      add_header Cache-Control private;
                      alias /var/www/html/demo/front/dist/static/;
      
          }
          # 前端找到Vue的index.html
          location /front/ {
              alias /var/www/html/demo/front/dist/;
              index index.html index.htm;    
          }
          # 后端处理请求
          location /{
                 include  uwsgi_params;
                 uwsgi_pass  127.0.0.1:9000;
                    uwsgi_send_timeout 600; 
                  uwsgi_connect_timeout 600;
                  uwsgi_read_timeout 1000;   
              
           }
      }
    3. 服务启动和关闭

      1. 启动uWSGI,--processes 后面的数字可以更改,代表开启进程的个数。

        uwsgi --socket 127.0.0.1:9000 --chdir /var/www/html/demo/back  --module back.wsgi:application --processes 1 --enable-threads

        其他关于uwsgi的参数请见uwsgi官方文档

      2. 启动Nginx

        sudo /usr/sbin/nginx 
      3. 关闭uWSGI和Nginx

        1. 关闭uWSGI和Nginx都可以使用

          ps -ef | grep uwsgi
          ps -ef | grep nginx

          然后使用 kill -3 进程id

        2. nginx 命令

          sudo /usr/sbin/nginx -t   #检测配置文件是否有问题
          sudo /usr/sbin/nginx -s stop  # 快速关闭
          sudo /usr/sbin/nginx -s quit  # 从容关闭
          sudo /usr/sbin/nginx -s reload
          sudo /usr/sbin/nginx -s restart

    这样就可以通过域名来访问Django项目了。

  • 相关阅读:
    A. Two Semiknights Meet DFS
    Dummy Guy 几何
    Electrification Plan MST
    Parallel and Perpendicular 几何,数学
    A. Soroban 暴力+水题
    B. Fence 前缀和 水题
    Max Sum http://acm.hdu.edu.cn/showproblem.php?pid=1003
    亲和串 http://acm.hdu.edu.cn/showproblem.php?pid=2203
    N! http://acm.hdu.edu.cn/showproblem.php?pid=1042
    Let the Balloon Rise http://acm.hdu.edu.cn/showproblem.php?pid=1004
  • 原文地址:https://www.cnblogs.com/huangjiangyong/p/14081201.html
Copyright © 2020-2023  润新知