• Nginx+uWSGI或fastcgi部署Django项目


    nginx+uWSGI

    ubuntu下先安装下C编译器和Python环境:

    sudo apt-get install build-essential python-dev
    

    使用pip安装uWSGI:

    pip install uwsgi
    

    nginx配置:

    可以单独为站点设置一个配置文件:

    sudo vim /etc/nginx/sites-enabled/mysite
    

    或者直接在nginx.conf中设置:

    sudo vim /etc/nginx/nginx.conf
    

    设置:

    server {
        listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6
     
        server_name 站点域名;
        location / {
                uwsgi_pass 127.0.0.1:8080;
                include uwsgi_params;
        }
        #设置该网站应用中所需要的静态文件的根目录,需要将admin和用到的第三方库像restframework的静态文件都放到此目录中
         location ~/static/ {
                root   /home/user/mysite/;  #项目地址
               # root  html;
               # index  index.html index.htm;
                break;
            }
            #设置媒体文件的根目录
           location ~/media/ {
                root   /home/user/mysite/;
               # root  html;
               # index  index.html index.htm;
                break;
            }
    }
    

    自己电脑上搭建localhost服务器时,注意别被/etc/nginx/sites-enabled/default中的配置覆盖掉了,最好将其注释掉。

    然后设置uWSGI,建立文件myfile.ini:

    [uwsgi]
    socket = 127.0.0.1:8080   #与nginx配置中的uwsgi_pass相同
    chdir = /home/user/mysite/    #项目地址
    wsgi-file = mysite/wsgi.py
    processes = 4
    threads = 2
    pidfile=/tmp/project-master.pid
    stats = 127.0.0.1:9191
    virtualenv = <path to env> #virtualenv

    其中wsgi-file是django(1.4版本以上)项目自动建立的文件,里面内容为:

    import os
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "weixian.settings")
    
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    

    如果django版本过低无此文件的话,可以自己建立,或者在myfile.ini中设置env,module,pythonpath:

    [uwsgi]
    socket = 127.0.0.1:8080
    chdir = /home/user/mysite/
    pythonpath = ..
    env = DJANGO_SETTINGS_MODULE=mysite.settings
    module = django.core.handlers.wsgi:WSGIHandler()
    processes = 4
    threads = 2
    stats = 127.0.0.1:9191
    virtualenv <path to env>

    按配置重启nginx:

    /usr/sbin/nginx -s reload
    

    或者:

    killall -9 nginx
    nginx -c /etc/nginx/nginx.conf
    

    启动uWSGI:

    uwsgi myfile.ini

    重启uWSGI:

    # using kill to send the signal
    kill -HUP `cat /tmp/project-master.pid`
    # or the convenience option --reload
    uwsgi --reload /tmp/project-master.pid
    # or if uwsgi was started with touch-reload=/tmp/somefile
    touch /tmp/somefile

    ini文件也可以用xml文件来设置。

    添加xml支持:

    sudo apt-get install libxml2-dev
    

    配置myfile.xml

    <uwsgi>
        <socket>127.0.0.1:8080</socket>
        <chdir>/home/user/mysite/</chdir>
        <module>mysite/wsgi</module>
    </uwsgi>
    

    启动:

    uwsgi -x myfile.xml

    nginx+fastcgi

    fastcgi需要安装flup:

    wget http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gz
    tar zxvf flup-1.0.2.tar.gz
    cd flup-1.0.2
    python setup.py install
    

    或者:

    sudo apt-get install  python-flup
    

    nginx配置:

    server {
            listen       80;
            server_name  localhost;
    
            #设置该网站应用中所需要的静态文件的根目录
            location ~/static/ {
                root   /home/user/mysite/;
               # root  html;
               # index  index.html index.htm;
                break;
            }
            #设置媒体的根目录
            location ~/media/ {
                root   /home/user/mysite/;
               # root  html;
               # index  index.html index.htm;
                break;
            }
            #host and port to fastcgi server 
            location / {
            fastcgi_pass 127.0.0.1:8080;
            fastcgi_param PATH_INFO $fastcgi_script_name;
            fastcgi_param REQUEST_METHOD $request_method;
            fastcgi_param QUERY_STRING $query_string;
            fastcgi_param CONTENT_TYPE $content_type;
            fastcgi_param CONTENT_LENGTH $content_length;
            fastcgi_param pass_header Authorization;
            fastcgi_intercept_errors off; 
            }
            #设置浏览器缓存这些图片格式文件浏览器缓存时间是30天,css/js缓存时间1小时
            location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
            {
                expires 30d;
            }
    
            location ~ .*.(js|css)?$
            {
                expires 1h;
            }
    }
    

    重启nginx,然后启动fcgi:

    python manage.py runfcgi host=127.0.0.1 port=8080 method=prefork --settings=mysite.settings  #采用静态进程池的话性能会比动态线程生成高2倍左右
    

    ok。

    要更新django项目的话,

    ps -ef |grep fcgi 
    

    找出主进程号kill掉,再重启fcgi即可。

    也可以写个重启脚本。使用pidfile选项将fcgi的pid存储到文件中,重启时读取kill掉。

    #!/bin/bash
    
    PROJDIR="/home/user/myproject"
    PIDFILE="$PROJDIR/mysite.pid"
    
    cd $PROJDIR
    if [ -f $PIDFILE ]; then
        kill `cat -- $PIDFILE`
        rm -f -- $PIDFILE
    fi
    
    exec python manage.py runfcgi host=127.0.0.1 port=8080 method=prefork pidfile=$PIDFILE --settings=mysite.settings

      

  • 相关阅读:
    laravel 图片上传与前端显示问题
    laravel elquent distinct 用法
    JQuery跳出each循环的方法(包含数组遍历)
    Laravel分页以及样式——从未如此简单
    Laravel 的 Blade 模板引擎,当数据存在时输出变量
    laravel 图片上传与前端显示问题
    使用Laravel Eloquent ORM 时如何查询表中指定的字段
    Laravel中路由绑定 Controller 包含子目录写法
    php artisan 命令列表
    如何安装 Composer
  • 原文地址:https://www.cnblogs.com/linxiyue/p/3973989.html
Copyright © 2020-2023  润新知