zoukankan      html  css  js  c++  java
  • nginx+uwsgi+django的搭建笔记

    之前搭建过 Apache下的Django项目,存在的问题是admin读写数据库无权限,一直没解决?有了解的指导一下!!

    今天测试了一下nginx下的django安装

    总的说来是比Apache下的配置简单,是真简单(Apache下的我忙活了一个星期,但是还是有个尾巴没搞定。这个一个下午,几乎参考了虫师的就能配置起来)

    (我的用的阿里云,直接root安装的,所以没有sudo)

    首先 安装nginx:

    apt-get install nginx

    ..................

    遇到个问题,是没有安装成功,源找不到,于是 apt-get update后即可

    修改Nginx默认端口号,打开/etc/nginx/nginx.conf 文件(此为主配置文件),修改/etc/nginx/sites-enabled/default 里的端口号(原来默认80,如果没有被占用也没关系)

    而且 default -> /etc/nginx/sites-available/default (是个链接,所以如果想要自己创建个配置文件,就链接一下)

    以下是我修改的default内容,把80改为8080

    server {
       
     listen 8080 default_server;
    listen [::]:8080 default_server;

    }

    然后启动nginx后访问  ip:8080,就能访问nginx欢迎页面了。

    第二步 安装uwsgi

     通过pip安装uwsgi

    root@ubuntu:/etc# python -m pip install uwsgi   #如果是py3,用 python3 。。。。。。。。。

    编写个测试脚本

    # cat test.py 
    def application(environ, start_response):  
        status = '200 OK'   
        output = 'Hello World! powerde by wsgi'  
        response_headers = [('Content-type', 'text/plain'),
    ('Content-Length', str(len(output)))]  
        start_response(status, response_headers)
        return [output]

    运行

    uwsgi --http :8001 --wsgi-file test.py
    然后浏览器访问 http://127.0.0.1:8001
    就可以访问这个测试页 显示 Hello World! powerde by wsgi

    测试访问你的项目wsgi.py
     uwsgi --http :8001 --chdir /app/mysit/ --wsgi-file mysit/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191

    此处虫师的写法有问题,chdir 为project目录    --wsgi-file 对应project下的同名目录,再下面是wsgi.py,否则系统无法import,如下

    *** Operational MODE: single process ***
    failed to open python file mysit/wsgi.py
    unable to load app 0 (mountpoint='') (callable not found or import error)
    *** no app loaded. going in full dynamic mode ***
    *** uWSGI is running in multiple interpreter mode ***

    常用选项:(照搬的)

    常用选项:
    
    http : 协议类型和端口号
    
    processes : 开启的进程数量
    
    workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
    
    chdir : 指定运行目录(chdir to specified directory before apps loading)
    
    wsgi-file : 载入wsgi-file(load .wsgi file)
    
    stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
    
    threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
    
    master : 允许主进程存在(enable master process)
    
    daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。(肯定要启用,要不刷屏!!)
    
    pidfile : 指定pid文件的位置,记录主进程的pid号。   (生成pid文件,以便stop uwsgi)
    
    vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)


    第三步 配置Django

    在django项目project目录下(manage.py下)创建配置文件分两种 ini 与xml
    ini格式
    # myweb_uwsgi.ini file
    [uwsgi]
    # Django-related settings
    socket = :8081
    
    # the
    base directory (project full path) chdir = /app/mysit # Django s wsgi file module = mysit.wsgi # process-related settings # master master = true # maximum number of worker processes processes = 4 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true # pidfile for record run pid pidfile =pid.uwsgi # run process background and save log to daemonize daemonize = UWSGI.log

    使用命令  uwsgi --ini myweb  myweb_uwsgi.ini     ###在目录下会生成pid及log文件

    xml格式(搬的)(再加入pid)

    #mytest_scoket.xml
    <uwsgi> <socket>127.0.0.1:8099</socket> <chdir>/app/test_project</chdir> <module>test_project.wsgi</module> <processes>4</processes> <daemonize>uwsgi.log</daemonize> </uwsgi>

    执行:uwsgi -x mytest_scoket.xml

    以上格式2选1。

    配置nginx配置文件,我还是修改的default文件!!! 如下

    server {
        listen 8080;
        server_name www.wenxi.xin     #这个是什么都无所谓,只是一个名称标记,虫师说他的要写成ip,这个应该不用,因为这个就相当于server ID,写入log
        charset UTF-8;
        client_max_body_size 75M;
        location / {
    
            include uwsgi_params;      #加载uwsgi
            uwsgi_pass 127.0.0.1:8081;     #是uwsgi 启动的socket端口, 即 myweb_uwsgi.ini 中的socket,应该也可以生成个socket文件,然后访问这个文件!
            uwsgi_read_timeout 5;
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            #try_files $uri $uri/ =404;        #这个要屏蔽,要不会报502错误,此uri是什么,还没找到
    
        }
        location /static {          #指定静态文件的别名,这个和Apache差不多
    
            expires 30d;
            autoindex on; 
            add_header Cache-Control private;
            alias /app/mysit/static/;
    
            }
    #如果有media,再加一项别名就洗
    
        # location /media/ {
          #       alias  /home/yixiang/Practice/Python/nginx_django/test_project/media/;
          #    }
    
    }

    配置完成后重启服务,步骤包括

    进入 myweb_uwsgi.ini 目录

    执行 uwsgi --ini myweb_uwsgi.ini

    然后 service nginx restart 或者 start

    然后访问你的nginx对外服务的ip:8080 就到了你看到了你的django项目了!!!

    原理,uwsgi 提供django 服务, nginx访问uwsgi提供的服务,并将静态文件路径转换,提供给客户端,

    当然了,这也是很浅显的东西,uwsgi官方文档很全面,如果精学需要看看

    参考文档:

    官方文档 http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html#

    参考了虫师http://www.cnblogs.com/fnng/p/5268633.html

    http://www.linuxidc.com/Linux/2016-07/133490.htm

    欢迎访问我的django(nginx)项目:http://60.205.221.253:8080/

    ----django(Apache)项目:http://60.205.221.253

  • 相关阅读:
    2016改变与突破的一年
    web安全测试&渗透测试之sql注入~~
    后台服务端接口验证项目实例~~
    Web自动化框架之五一套完整demo的点点滴滴(excel功能案例参数化+业务功能分层设计+mysql数据存储封装+截图+日志+测试报告+对接缺陷管理系统+自动编译部署环境+自动验证false、error案例)
    web自动化框架之四测试报告的搭建
    web自动化框架之三获取数据库值与界面值比较~~
    Web自动化框架搭建之二基于数据驱动应用简单实例~~
    web自动化框架之一介绍与环境搭建(Selenium+Eclipse+Python)
    事件响应
    威胁分析
  • 原文地址:https://www.cnblogs.com/if-then/p/7259865.html
Copyright © 2011-2022 走看看