您的位置:首页 > 运维架构 > Nginx

python+Tornado + Supervisor + nginx部署

2014-05-03 21:25 951 查看
转载声明:http://blog.163.com/wangkangming2008@126/blog/static/78277928201252092612850/
1.安装python 2.6.6

wget http://www.python.org/ftp/python/2.6.6/Python-2.6.6.tgz
tar -xvzf Python-2.6.6.tgz

cd Python-2.6.6

./configure

make

make install

2.tornado 安装
http://www.tornadoweb.org/
下载 tornado-2.3.tar.gz

tar xvf tornado-2.3.tar.gz

cd tornado-2.3

python setup.py install

3.setuptools安装
http://pypi.python.org/pypi/setuptools#files
系统要安装python,并安装与之对应的setuptools,下载地址在此

安装:

# sh setuptoolsxxxx.egg

4.supervisor安装

supervisor安装非常方便,

easy_install supervisor就可以

或者下载地址在此,解压缩后#python setup.py install

# echo_supervisord_conf > /etc/supervisord.conf

修改/etc/supervisord.conf文件 添加

[program:tornado_poll]

command=python /home/wangwang/helloworld/helloworld.py –port=80%(process_num)02d ; 要执行的命令,这里的“%(process_num)02d”会用2位精度的进程号替换,例如,第一个进程是8001,第二个进程是8002,以此类推,下同。

process_name=%(program_name)s-80%(process_num)02d ; process_name expr (default %(program_name)s) ;启动的进程的名字,这里的名字只是supervisor内部是别用,与你所启动程序的进程名无关

numprocs=2 ; 启动几个tornado进程

directory=/home/wangwang/helloworld ; 运行前cd到此目录

autostart=true ; supervisord守护程序启动时自动启动tornado

autorestart=true ; supervisord守护程序重启时自动重启tornado

user=www-data ; 运行程序前su到此用户

redirect_stderr=true ; 将stderr重定向到stdout

stdout_logfile=/home/wangwang/helloworld/python_log ; 记录控制台输出的日志位置

5.tornado 入口

############### helloworld.py ###############

#coding=utf-8

import sys

import tornado.httpserver

import tornado.ioloop

import tornado.options

import tornado.web

from tornado.options import define, options

#define("port", default=8888, help="run on the given port", type=int)

port = int(sys.argv[1].split('=')[1])

if port == 0:

exit(1)

class MainHandler(tornado.web.RequestHandler):

def get(self):

content
= "hello ,world from port: %s" % port

self.write(content)

def main():

application = tornado.web.Application([

(r"/", MainHandler),

])

http_server = tornado.httpserver.HTTPServer(application)

http_server.listen(port)

tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":

main()

supervisor进程管理

#开启 supervisor:

#supervisord

#管理进程

#supervisorctl

>status/stop/start/restart

stop all #停止所有进程

stop tornado_poll:tornado_poll-8000 #停止运行在8000端口上的Tornado守护进程

stop tornado_poll:* #停止所有

#nginx 做反向代理实现负载均衡
http://ftp.exim.llorien.org/pcre/
tar xvf pcre-6.5.tar.gz

cd pcre-6.5

./configure

make

make install
http://nginx.org/en/download.html
tar zxvf nginx-1.2.1.tar.gz

cd nginx-1.2.1

./configure --with-http_stub_status_module --prefix=/usr/local/webserver/nginx

make && make install

修改 nginx/conf/nginx.conf

worker_processes 1;

error_log logs/error.log;

pid logs/nginx.pid;

events {

use epoll;

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

access_log logs/access.log;

sendfile on;

keepalive_timeout 65;

upstream frontends {

server 127.0.0.1:8000;

server 127.0.0.1:8001;

}

server {

listen 80;

client_max_body_size 50M;

location ^~ /static/ {

root /home/wangwang/helloworld;

if ($query_string) {

expires max;

}

}

location = /favicon.ico {

rewrite (.*) /static/favicon.ico;

}

location = /robots.txt {

rewrite (.*) /static/robots.txt;

}

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://frontends;
}

}

}

nginx/sbin/nginx -t -c nginx/conf/nginx.conf #检测配置文件是否正常

nginx/sbin/nginx -c nginx/conf/nginx.conf #开启nginx

访问:http://127.0.0.1/

hello ,world from port: 8000 / hello ,world from port: 8001
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: