nginx uwsgi 설정

4297 단어
환경.
  • centos 6.5
  • anaconda 3
  • nginx 1.8
  • python 3.6
  • uWSGI 2.0.18

  • 설치
    $ source activate py3.6
    $ conda install -c anaconda icu
    $ pip install uwsgi
    $ uwsgi --version
    
    //#    libpcre.so.1: cannot open shared object file: No such file or directory
    //#        ,           
    $ find / -name "libpcre.so*" -type f
    $ ln -s /usr/lib64/libpcre.so /usr/lib64/libpcre.so.1
    //#      ,           
    
    //#     
    $ cat test.py
    def application(env, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return b"Hello world!
    " //# usgi $ uwsgi --http :8001 --wsgi-file test.py $ curl http://localhost:8001

    nginx 는 http 요청 을 받 아 uwsgi 에 전달 하기 때문에 보통 uwsgi 를 먼저 시작 합 니 다.
    uwsgi 설정, 동적 모드, nging 에 적용
    $ cat /etc/uwsgi.ini
    [uwsgi]
    socket = 127.0.0.1:9090
    uid = www
    gid = www
    master = true
    vhost = true
    no-site = true
    workers = 10
    reload-mercy = 10
    vacuum = true
    max-requests = 1000
    buffer-size = 30000
    pidfile = /var/run/uwsgi.pid
    daemonize = /var/log/nginx/uwsgi.log
    enable-threads = true
    thunder-lock = true
    

    다운로드 nginx
    $ yum install nginx
    $ vi /etc/nginx/conf.d/default.conf
    server {
        listen       80 default_server;
        server_name  _;
    
        access_log      /var/log/nginx/access.log;
        error_log       /var/log/nginx/error.log;
        location / {
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:9090;
            uwsgi_param UWSGI_SCRIPT wsgi;
            uwsgi_param UWSGI_CHDIR  /usr/share/nginx/html/test;
            client_max_body_size 35m;
        }
    }
    
    //# python  
    $ mkdir /usr/share/nginx/html/test
    
    //# uwsgi  wsgi.py    
    $ cat /usr/share/nginx/html/test/wsgi.py
    def application(env, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return b"Hello world!
    " //# uwsgi $ uwsgi --ini /etc/uwsgi.ini //# nginx $ service nginx start //# $ curl http://localhost:80

    uwsgi 시작 스 크 립 트
    $ vi /etc/init.d/uwsgi
    #!/bin/bash
    # uwsgi script
    # it is v.0.0.1 version.
    # chkconfig: - 89 19
    # description: uwsgi script
    # processname: uwsgi
    
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    
    ### modify by user
    export PYTHONHOME=/usr/local/anaconda3/envs/py3.6
    uwsgi_config=/etc/uwsgi.ini
    PIDFILE=`sed -n '/^pidfile/p' ${uwsgi_config} | awk -F "=" '{print $2}'|tr -d [:space:]`
    uwsgi=$PYTHONHOME/bin/uwsgi
    ### modify end
    
    # Source function library.
    .  /etc/rc.d/init.d/functions
    
    # Start uwsgi daemons functions.
    start() {
        # check if runing 
        if [ -f $PIDFILE ];then
            FILEPID=`cat $PIDFILE`
            isRun=`ps aux|grep $FILEPID| grep -v grep`
            if [ -n "$isRun" ];then
                echo "uwsgi (pid $FILEPID) already running."
                exit 0
            else
                rm -f $PIDFILE
            fi
    	    
        fi
    
        daemon $uwsgi --ini ${uwsgi_config}
        if [ -f $PIDFILE ];then
            FILEPID=`cat $PIDFILE`
            isRun=`ps aux|grep $FILEPID| grep -v grep`
            if [ -n "$isRun" ];then
                echo "uwsgi (pid $FILEPID) start sucess."
            else
                rm -f $PIDFILE
                echo "uwsgi start failed."
            fi
        else
            echo "uwsgi start failed."
        fi      
    
    }
    
    # Stop nginx daemons functions.
    stop() {
        if [ -f $PIDFILE ];then
            echo "stop form the pidfile"
            kill -2 `cat $PIDFILE`
            rm -f $PIDFILE
            echo "stop uwsgi done"
        else
    	    echo " uwsgi don't runing"
        fi
    }
    
    # reload the process
    reload() {
        if [ -f $PIDFILE ];then
            $uwsgi --reload $PIDFILE
            echo " uwsgi reload success"
        else
    	    echo " uwsgi don't runing"
        fi
    
    }
    
    # See how we were called.
    case "$1" in
    start)
            start
            ;;
    stop)
            stop
            ;;
    reload)
            reload
            ;;
    restart)
            stop
            sleep 1
            start
            ;;
    *)
            echo $"Usage: $prog {start|stop|restart|reload}"
            exit 1
    esac
    exit $RETVAL
    
    
    

    시스템 서비스 추가
    chmod +x /etc/init.d/uwsgi
    chkconfig --add uwsgi
    service uwsgi  restart
    

    좋은 웹페이지 즐겨찾기