zoukankan      html  css  js  c++  java
  • django+wsgi+nginx 环境ubuntu

    本文采用uwsgi+nginx来部署Django,环境是ubuntu16.04

    这种方式是将nginx作为服务器前端,将接受web所有的请求,统一管理。Nginx把所有的静态请求自己处理(静态文件处理是ngInx强项),然后把所有非静态请求通过uwsgi传递给Django,由Django来处理,从而完成一次web请求。

    一、uWSGI

    1. 安装uWSGI
      pip install uwsgi
    2. 测试uWSGI是否安装成功
      在终端中输入以下命令查看uwsgi的版本号,如果输出正常,说明uswgi已安装成功
      $ uwsgi --version
      2.0.15
    3. 编写一个简单的wsgi应用测试uwsgi是否能正常使用
      首先创建一个test.py文件(命令:vim test.py)
      # test.py
      def application(env, start_response):
          start_response('200 OK', [('Content-Type','text/html')])
          return [b"Hello World"] # python3
          #return ["Hello World"] # python2
    4. 运行uwsgi
      uwsgi --http :8000 --wsgi-file test.py

      参数解释:

      http :8000表示使用http协议,端口号为8000,

      wigi-file则表示要运行的wsgi应用程序文件。

      uwsgi运行后打开浏览器,访问 ,或者是相应服务器地址的8000端口,就可以看到hello world 页面了

      如果想要运行项目来测试

      # uwsgi --http :8000 --chdir 项目路径 -w 项目.wsg --static-map=/static=static
      
      uwsgi --http :8000 --chdir /home/teacher/ -w teacher.wsgi --static-map=/static=static

      uWSGI常用命令:

      uwsgi --chdir=/path/to/your/project 
          --module=mysite.wsgi:application 
          --env DJANGO_SETTINGS_MODULE=mysite.settings 
          --master --pidfile=/tmp/project-master.pid 
          --socket=127.0.0.1:49152       # 可以ip地址,也可以是文件 
          --processes=5                  # 进程数量
          --uid=1000 --gid=2000          # 如果是root用户,uwsgi可以有删除权限
          --harakiri=20                  # 一个请求超时时间
          --max-requests=5000            # 一个工作进程最大请求数
          --vacuum                       # 退出时清楚环境
          --home=/path/to/virtual/env    # virtualenv的路径
          -- static                       # 做一个映射,指定静态文件
          --http                          # 这个就和runserver一样指定IP 端口
          --daemonize=/var/log/uwsgi/yourproject.log      # 日志 
    5.  创建uwsgi配置文件
      在项目统计目录下创建文件夹script,(比如项目目录路径/home/project_teacher/teacher,那么scrip文件存放在/home/project/teacher/script)
      配置信息如下:
      [uwsgi]
      # 项目目录
      chdir=/opt/project_teacher/teacher/
      # 指定项目的application
      module=teacher.wsgi:application
      # 进程个数
      workers=5
      pidfile=/opt/project_teacher/script/uwsgi.pid
      # 指定IP端口
      http=192.168.31.123:8080
      # 指定静态文件
      static-map=/static=/opt/test_project/teacher/static
      # 启动uwsgi的用户名和用户组
      uid=root
      gid=root
      # 启用主进程
      master=true
      # 自动移除unix Socket和pid文件当服务停止的时候
      vacuum=true
      # 序列化接受的内容,如果可能的话
      thunder-lock=true
      # 启用线程
      enable-threads=true
      # 设置自中断时间
      harakiri=30
      # 设置缓冲
      post-buffering=4096
      # 设置日志目录
      daemonize=/opt/project_teacher/script/uwsgi.log
      # 指定sock的文件路径
      socket=/opt/project_teacher/script/uwsgi.sock
    6. 启动配置
      $ uwsgi --ini uwsgi.ini   # 启动uwsgi配置
      [uwsgi-static] added mapping for /static => /home/trunk/static    # 启动成功
      
      $ uwsgi --stop uwsgi.pid  # 关闭uwsgi
      signal_pidfile()/kill(): Operation not permitted [core/uwsgi.c line 1659]

    二、Nginx

    1. 安装
      $ sudo apt-get install nginx  #安装
    2. 检查nginx是否安装成功
      $ /etc/init.d/nginx start
      
      [ ok ] Starting nginx (via systemctl): nginx.service.

      检查nginx是否启动成功

      $ ps -ef |grep -i nginx
      root       6961      1  0 03:56 ?        00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
      www-data   6962   6961  0 03:56 ?        00:00:00 nginx: worker process
      pala       6985   2090  0 03:57 pts/0    00:00:00 grep --color=auto -i nginx

      然后打开浏览器,访问ip地址,出现如下页面即代表nginx安装完成且可以正常启动。

      Nginx常用命令

      $ /etc/init.d/nginx start  #启动
      $ /etc/init.d/nginx stop  #关闭
      $ /etc/init.d/nginx restart  #重启

    三、Django + uWSGI +Nginx

    1. 创建一个xxx.conf配置文件(nginx的默认配置目录为/etc/nginx/conf.d)
      $ vim /etc/nginx/conf.d/xxx.conf
    2. 配置文件信息如下:
      server {   # 这个server标识我要配置了
              listen 80;  # 我要监听那个端口
              server_name 10.129.205.183 ;  # 你访问的路径前面的url名称
              access_log  /var/log/nginx/access.log  main;  # Nginx日志配置
              charset  utf-8; # Nginx编码
              gzip on;  # 启用压缩,这个的作用就是给用户一个网页,比如3M压缩后1M这样传输速度就会提高很多
              gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;  # 支持压缩的类型
      
              error_page  404           /404.html;  # 错误页面
              error_page   500 502 503 504  /50x.html;  # 错误页面
      
              # 指定项目路径uwsgi
              location / {        # 这个location就和咱们Django的url(r'^admin/', admin.site.urls),
                  include uwsgi_params;  # 导入一个Nginx模块他是用来和uWSGI进行通讯的
                  uwsgi_connect_timeout 30;  # 设置连接uWSGI超时时间
                  uwsgi_pass unix:/opt/project_teacher/script/uwsgi.sock;  # 指定uwsgi的sock文件所有动态请求就会直接丢给他
              }
      
              # 指定静态文件路径
              location /static/ {
                  alias  /opt/project_teacher/teacher/static/;
                  index  index.html index.htm;
              }

      uwsgi_params文件内容(放在项目根目录下)

      uwsgi_param  QUERY_STRING       $query_string;
      uwsgi_param  REQUEST_METHOD     $request_method;
      uwsgi_param  CONTENT_TYPE       $content_type;
      uwsgi_param  CONTENT_LENGTH     $content_length;
      
      uwsgi_param  REQUEST_URI        $request_uri;
      uwsgi_param  PATH_INFO          $document_uri;
      uwsgi_param  DOCUMENT_ROOT      $document_root;
      uwsgi_param  SERVER_PROTOCOL    $server_protocol;
      uwsgi_param  REQUEST_SCHEME     $scheme;
      uwsgi_param  HTTPS              $https if_not_empty;
      
      uwsgi_param  REMOTE_ADDR        $remote_addr;
      uwsgi_param  REMOTE_PORT        $remote_port;
      uwsgi_param  SERVER_PORT        $server_port;
      uwsgi_param  SERVER_NAME        $server_name;

      重启nginx

      $ /etc/init.d/nginx restart  #重启

    来源一位知乎小姐姐:https://zhuanlan.zhihu.com/p/29083368

    对了,插一嘴,在ubuntu中,普通用户对计算机中的文件如 opt/ 、etc/等文件夹中内容修改时,没有办法直接修改,除了命令行执行命令加上sudo之外,可以使用sudo nautilus来移动或删除文件。
    有问题可以直接将问题发送至1005819387@qq.com来讨论,或者在评论里直接说也可以

  • 相关阅读:
    NHibernate中多表(对象)间的查询
    将datagrid数据导到excel的一个问题
    win2003<IIS6>部署.net 4.0<asp.net 4>
    C# 单元测试
    office2010 word发布博客 博客园
    语义化的HTML首先要强调HTML结构
    SQL Server 2005 安装(各种错误)
    SWFUpload V2.2.0 说明文档
    SQL Server 复制, 集群
    高亮插件测试
  • 原文地址:https://www.cnblogs.com/linpei/p/10845286.html
Copyright © 2011-2022 走看看