Centos6.3 下安装 Ngixn +tornado + supervisor

tornado先天对异步(no-bolocking)处理能力,非常适合作为Web服务。tornado在linux平台使用epoll来实现异步事件的处理,性能非常好。但是python做为一个脚步语言,单进程执行,无法利用多CPU,对当今的多核CPU是一个很大的浪费。为提高性能,提高CPU利用率,一般会将tornado程序允许cup*n个。
怎样才能放便启动多个tornado程序呢,我们可以用supervisor来管理多个tornado应用。supervisor安装非常方便

easy_install supervisord

安装Nginx

groupadd www
useradd -s /sbin/nologin -g www www
mkdir -p /data/wwwroot
chmod +w /data/wwwroot
mkdir -p /data/wwwroot/logs
chmod 755 /data/wwwroot/logs
chown -R www:www /data/wwwroot

wget http://www.nginx.org/download/nginx-1.3.11.tar.gz -P /data/soft/src/
tar zxvf /data/soft/src/nginx-1.3.11.tar.gz -C /data/soft/install
cd /data/soft/install/nginx-1.3.11

yum install zlib pcre pcre-devel openssl

./configure \
    --user=www\
    --group=www\
    --prefix=/etc/nginx\
    --sbin-path=/etc/nginx/sbin/nginx\
    --conf-path=/etc/nginx/conf/nginx.conf\
    --with-http_stub_status_module\
    --with-http_ssl_module\
    --with-pcre\    
make &&  make install

vim /etc/init.d/nginx

加入以下内容

#!/bin/bash
#
# chkconfig: - 85 15
# description: Nginx is a World Wide Web server.
# processname: nginx

nginx=/etc/nginx/sbin/nginx
conf=/etc/nginx/conf/nginx.conf

case $1 in
       start)
              echo -n "Starting Nginx"
              $nginx -c $conf
              echo " done"
       ;;

       stop)
              echo -n "Stopping Nginx"
              killall -9 nginx
              echo " done"
       ;;

       test)
              $nginx -t -c $conf
       ;;

reload)
              echo -n "Reloading Nginx"
              ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP
              echo " done"
       ;;

restart)
$0 stop
$0 start
       ;;

       show)
              ps -aux|grep nginx
       ;;

       *)
              echo -n "Usage: $0 {start|restart|reload|stop|test|show}"
       ;;
esac

chmod +x /etc/init.d/nginx

chkconfig --add nginx
chkconfig nginx on

service nginx start
service nginx test

mv /etc/nginx/conf/nginx.conf /etc/nginx/conf/nginx.conf_bak
cp /data/soft/src/nginx.conf /etc/nginx/conf/nginx.conf
vim /etc/nginx/conf/nginx.conf

安装tornado

yum install python-setuptools
easy_install tornado

安装 supervisor

http://supervisord.org/configuration.html?highlight=program#program-x-section-settings

easy_install supervisor

然后创建配置文件

echo_supervisord_conf > /etc/supervisord.conf

vim /etc/supervisord.conf

最后面加上:

[program:tornado_poll]
command=python /home/wwwroot/app/app.py 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=4                                                                      ; 启动几个tornado进程
directory=/home/wwwroot/app                                     ; 运行前cd到此目录
autostart=true                ; supervisord守护程序启动时自动启动tornado
autorestart=true              ; supervisord守护程序重启时自动重启tornado
user=www-data                   ; 运行程序前su到此用户
redirect_stderr=true          ; 将stderr重定向到stdout
stdout_logfile=/home/wwwroot/logs/tornado-80%(process_num)02d.log
stdout_logfile_maxbytes=500MB
stdout_logfile_backups=50
stdout_capture_maxbytes=1MB
stdout_events_enabled=false
loglevel=warn

运行:

supervisord

root@vps:/tmp# supervisorctl
tornado_poll:tornado_poll-8000   RUNNING    pid 11352, uptime 0:29:01
tornado_poll:tornado_poll-8001   RUNNING    pid 11347, uptime 0:29:02
supervisor>#这里可以输入控制命令

我们可以看到,上面显示了现在正在运行的守护进程的信息,下面介绍几个常用的控制命令

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



supervisord
supervisorctl reload

问题:

 * Starting Supervisor daemon manager...
Error: Another program is already listening on a port that one of our HTTP servers is configured to use.  Shut this program down first before starting supervisord.
For help, use /usr/bin/supervisord -h
   ...fail!

解决办法:

unlink /tmp/supervisor.sock

http://www.idndx.com/posts/ways-to-deploy-tornado-under-production-environment-using-supervisor.html

http://supervisord.org/
http://fendou.org/2011/09/23/supervisor-nginx-tornado/

你可能感兴趣的:(Centos6.3 下安装 Ngixn +tornado + supervisor)