nginx uwsgi 配置

环境
  • centos 6.5
  • anaconda 3
  • nginx 1.8
  • python 3.6
  • uWSGI 2.0.18
uwsgi安装
$ 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!\n"

//# 测试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!\n"

//# 启动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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值