virtualenv+supervisord+tornado+nginx配置

好久没有写些东西了,年末了,都很忙
最近的事情也挺多,写一下开始搞python时作web用的一个配置
最初使用python就是用的python2.7的 而服务器用的centos系统,上面python仍是2.4的版本
多个版本的共存这时就很少说了,网上也有不少写这个的
下载python2.7的源码,编译安装好后
准备安装virtualenv html

pip install virtualenv

进入/data/www目录,建立一个python2.7的环境 前端

virtualenv app -p /usr/bin/python27 cd app
source bin/activate

你会发如今命令行前会多出(app)字样,这就是建立成功了,在当前环境下运行一下python,你将看到相似于以下的版本信息 python

Python 2.7.3 (default, Jun 7 2012, 21:39:50) [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

在此运行 linux

pip install supervisor
pip install tornado

假设你的tornado源码在/data/www/app/app目录下,那么在/data/www/app/etc目录下创建supervisord.conf
内容以下 nginx

[unix_http_server] file=/tmp/supervisor.sock ; path to your socket file   [supervisord] logfile=/tmp/supervisord.log ; supervisord log file logfile_maxbytes=50MB ; maximum size of logfile before rotation
logfile_backups=10 ; number of backed up logfiles
loglevel=warn ; info, debug, warn, trace
pidfile=/tmp/supervisord.pid ; pidfile location
nodaemon=false ; run supervisord as a daemon
minfds=1024 ; number of startup file descriptors
minprocs=200 ; number of process descriptors user=www ; default user childlogdir=/data/log
  [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
  [supervisorctl] serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket   [program:tor8000] command=python /data/www/app/app/app.py autostart=true
stderr_logfile = /data/log/tornado-8000-stderr.log stdout_logfile = /data/log/tornado-8000-stdout.log   [program:tor8001] command=python /data/www/app/app/app.py --port=8001 autostart=true
stderr_logfile = /data/log/tornado-8001-stderr.log stdout_logfile = /data/log/tornado-8001-stdout.log

这里使用supervisor维护两个进程,分别运行在8000和8001端口上
在/data/www/app目录下建立supervisord.sh,内容以下 web

#!/bin/bash
 
PID="/tmp/supervisord.pid"
CONF="etc/supervisord.conf"
 
stop() {
    if [ -f $PID ]; then
        kill `cat -- $PID`
        rm -f -- $PID
        echo "stopped"
    fi
}
 
start() {
    echo "starting"
    if [ ! -f $PID ]; then
        supervisord -c $CONF
        echo "started"
    fi
}
 
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
esac

给supervisord.sh添加执行权限chown +x supervisord.sh
运行tornado的进程 shell

./supervisord.sh start

配置成功,固然nginx来作下前端代理的配置就很简单了
nginx.conf内容大体以下: centos

upstream http {
    server 127.0.0.1:8000;
    server 127.0.0.1:8001;
}
 
server {
    listen 80;
    server_name app.com;
    client_max_body_size 4M;
 
    error_page 404 = /404.html;
    location /404.html {
        root /data/www/app/app/html;
        internal;
    }
    location ^~ /static/ {
        root /data/www/app/app;
        if ($query_string) {
            expires max;
        }
    }
    location = /favicon.ico {
        rewrite (.*) /static/favicon.ico;
    }
    location = /robots.txt {
        rewrite (.*) /static/robots.txt;
    }
    location ~ .*\.(html|txt|xml) {
        root /data/www/app/app/html;
    }
    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_pass http://http;
    }
}

大功告成,相信不少新手都碰到过这些配置问题,但愿个人总结能给你们带来些方便 bash

相关文章
相关标签/搜索