zoukankan      html  css  js  c++  java
  • 【Django】Django web项目部署(Nginx+uwsgi)

    一、安装uwsgi

     通过pip安装uwsgi。

    pip install uwsgi

    测试uwsgi,创建test.py文件:

    def application(env, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return [b"Hello World"]

    通过uwsgi运行该文件。

    uwsgi --http :8001 --wsgi-file test.py

    常用选项:

    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号。

    vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)

    二、安装nginx

    sudo apt-get install nginx  #安装

      启动Nginx:

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

    三、Django部署

    在我们用python manager.py startproject myproject创建项目时,会自动为我们生成wsgi文件,所以,我们现在之需要在项目目录下创建uwsgi的配置文件即可,我们采用ini格式:

    # myweb_uwsgi.ini file
    [uwsgi]
     
    # Django-related settings
     
    socket = :8000
     
    # the base directory (full path)
    chdir           = /mnt/myproject
     
    # Django s wsgi file
    module          = myproject.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
     
    daemonize       = /mnt/myproject/uwsgi_log.log
     
    pidfile = /mnt/myproject/uwsgi_pid.log

    再接下来要做的就是修改nginx.conf配置文件。打开/etc/nginx/nginx.conf文件,http中添加如下内容。

    server {
        listen         8099;
        server_name    127.0.0.1
        charset UTF-8;
        access_log      /var/log/nginx/myweb_access.log;
        error_log       /var/log/nginx/myweb_error.log;
     
        client_max_body_size 75M;
     
        location / {
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:8000;
            uwsgi_read_timeout 2;
        }  
        location /static {
            expires 30d;
            autoindex on;
            add_header Cache-Control private;
            alias /mnt/myproject/static/;
         }
     }
    listen 指定的是nginx 对外的端口号。

    server_name  设置为域名或指定的到本机ip。

    nginx通过下面两行配置uwsgi产生关联:

    include uwsgi_params;  
    uwsgi_pass 127.0.0.1:8000; //必须与uwsgi中配置的端口一致

    最后我们在项目目录下执行下面的命令来启动关闭我们的项目:

    1 #启动
    2 uwsgi --ini uwsgi.ini 
    3 /etc/init.d/nginx start  
    4 
    5 #停止
    6 uwsgi --stop uwsgi_pid.log
    7 /etc/init.d/nginx stop

    好了 ,现在我们可以访问127.0.0.1:8099即可看到我们自己的项目了

  • 相关阅读:
    小数的进制转换
    水题 O
    水题 J
    水题T,二进制转16进制
    水题B
    水题C
    HDU 2042
    HDU 2041
    Index For SQL Server
    Learning Note For Angular
  • 原文地址:https://www.cnblogs.com/perfe/p/6196854.html
Copyright © 2011-2022 走看看