• HOME
  • DOCS
  • WTF
  • TECH
  • LIFE
  • PAGES
    • ARCHIVE
    • TAGS
    • ABOUT
    • FRIENDS
    • RSS
  • TOOLS
    • GEO
    • RANDOM()
    • GOO.GL
    • CSS HEART
Aj's Blog

记录时间溜走的瞬间和折腾过的那些事

nginx(lnmp)+uwsgi emperor模式多站配置

2016-01-19  TECH  emperor  lnmp  Nginx  python  uwsgi  

最近练手学python开发、顺便学学微信api操作,阿里云的debian6服务器上已经装好了lnmp,不想影响现有vhost配置。于是找了点uwsgi emperor的资料,总结如下。从这篇文章开始,打算以后都给文章配图。

一、uwsgi安装中遇到的坑
阿里云的debian6自带的python2.6,升级python2.7
编译安装uwsgi太多库版本不匹配。用apititude 基本上都解决了
建议还是用debian7、8,各种版本问题太麻烦
uwsgi编译后得到一个可执行文件,本文中默认cp到 /usr/bin/uwsgi

二、uwsgi配置
uwsgi的emperor模式跟nginx的vhost管理模式思路完全一样
一个uwsgi主进配置(/etc/uwsgi.ini),指定emperor子站配置目录后,uwsgi会自动扫描加载(稍有延迟)

[uwsgi]
emperor = /etc/uwsgi.d/ #uwsgi子站配置目录

gid = www
uid = www
chmod = 777

master = true
workers = 4
max-requests = 1000

pidfile = /var/run/uwsgi.pid
daemonize = /var/log/uwsgi.log

假设子站域名 test.com, 在/etc/uwsgi.d/新建 test.com.ini做成模版方便以后使用,
我因为需要比较小,只是学习和测试,没有启用uwsgi的plugins和virtulenv

[uwsgi]
socket = /tmp/%n.sock
chdir = /home/wwwroot/%n/
logto = /home/wwwroot/%n/web.log
wsgi-file = index.py #根据自己情况确定
module = index:app   #根据自己情况确定

我使用的falcon框架,简单的index.py

# -*- coding: utf-8 -*-
import falcon

#--------------------------------------------------
# Default for Debug
#--------------------------------------------------
class Default(object):
    def on_get(self, req, resp):
        html = "API: default"
        resp.status = falcon.HTTP_200
        resp.body   = (html)

#--------------------------------------------------
# uWSGI App Entry & URL Dispatch
#--------------------------------------------------
app = falcon.API()
app.add_route( '/', Default() )

三、nginx对应vhost配置

server {
    listen 80;
    server_name test.com;
    root /home/wwwroot/test.com;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/test.com.sock; #和前面子站.ini配置文件匹配
    }
    access_log /home/wwwlogs/test.com.log access;
}

lnmp reload声效即可

四、uwsgi管理脚本
从lnmp自带的 /etc/init.d/nginx 修改而来

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for uwsgi webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f uwsgi defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add uwsgi'

### BEGIN INIT INFO
# Provides:          uwsgi
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the uwsgi web server
# Description:       starts uwsgi using start-stop-daemon
### END INIT INFO

# Author:   licess
# website:  http://lnmp.org

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="uwsgi emperor mode"
NAME=uwsgi
DAEMON=/usr/bin/uwsgi
CONFIGFILE=/etc/$NAME.ini
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

set -e
[ -x "$DAEMON" ] || exit 0

case "$1" in
    start)
        echo -n "Starting $NAME:\t\t\t\t"
        if [ -f $PIDFILE ] ; then
            PID=$(cat $PIDFILE)
            echo "pid=$PID, already running.\n"
            exit 1
        fi
        $DAEMON $CONFIGFILE
        if [ "$?" != 0 ] ; then
            echo " faild.\n"
            exit 1
        fi
    ;;
    stop)
        echo -n "Stopping $NAME:\t\t\t\t"
        if [ ! -f $PIDFILE ] ; then
            echo "not running.\n"
            exit 1
        fi
        $DAEMON --stop $PIDFILE
        if [ "$?" != 0 ] ; then
            echo " faild.\n"
            exit 1
        else
            PID=$(cat $PIDFILE)
            echo "pid=$PID, stoped.\n"
            rm -f $PIDFILE
        fi
    ;;
    status)
        echo -n "Status $NAME: \n"
        ps aux|grep $DAEMON
    ;;
    reload)
        echo -n "Reloading $NAME: \t\t\t\t"
        if [ ! -f $PIDFILE ] ; then
            echo "not running.\n"
            exit 1
        fi
        $DAEMON --reload $PIDFILE
        if [ "$?" != 0 ] ; then
            echo " faild.\n"
            exit 1
        else
            PID=$(cat $PIDFILE)
            echo "pid=$PID, reloaded.\n"
        fi
    ;;
    *)
        echo "Usage: $SCRIPTNAME {start|status|stop|reload}\n" >&2
        exit 3
    ;;
esac
exit 0
下一篇:   nginx(lnmp)+node+express web开发小结
上一篇:   给博客加个HTTPS吧 (lnmp nginx+letsencrypt)
暂无评论

Cancel reply