tornado + supervisor + nginx 的一点记录

时间:2023-01-13 09:24:52

看了比较多的blog基本都是这个架构:

supervisor ------------ app1

|-------app2

|-------....

|-------appn

|-------nginx

|-------redis

统一都交给supervisor来管理。总觉得哪里不对:

1) nginx作为supervisor的子进程,会有问题,它貌似会不断的去执行启动(导致大量的错误日志:端口已经被占用)

2)   nginx 和 redis 的启动与配置与app之间应该是没有耦合关系的,和supervisor也是没有耦合关系的。

===================

自己整理微调如下:

-----------------------

supervisor -----|------- app1

|------- appn

nginx   ---------|---------proxy1

|---------proxy2

redis

这个部署架构意味着要做多个脚本,并加入到开机启动项里面去。supervisor/nginx/redis各需要一个。

nginx 脚本如下:

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL

  

supervisor 由于根具体的应用对应,因此脚本也应该根据具体应用命名,比如有一个应用叫做 didibus

#!/bin/sh
#
# /etc/rc.d/init.d/supervisord
#
# Supervisor is a client/server system that
# allows its users to monitor and control a
# number of processes on UNIX-like operating
# systems.
#
# chkconfig: - 64 36
# description: Supervisor Server
# processname: supervisord # Source init functions
. /etc/rc.d/init.d/functions prog="didibus" prefix="/usr"
exec_prefix="${prefix}"
prog_bin="${exec_prefix}/bin/supervisord"
PIDFILE="/var/run/$prog.pid" config_file="/path/to/didibus/supervisord.conf" start()
{
echo -n $"Starting $prog: "
daemon $prog_bin -c $config_file --pidfile $PIDFILE
[ -f $PIDFILE ] && success $"$prog startup" || failure $"$prog startup"
echo
} stop()
{
echo -n $"Shutting down $prog: "
echo
for i in {1..4}
do supervisorctl -c $config_file stop cool_talk_server$i
done
[ -f $PIDFILE ] && killproc $prog || success $"$prog shutdown"
echo
} restart_childs()
{
echo -n $"restarting childs of $prog: "
echo
for i in {1..4}
do supervisorctl -c $config_file restart didibus$i
done
} case "$1" in start)
start
;; stop)
stop
;; status)
status $prog
;; restart)
stop
start
;; restart_childs)
restart_childs
;; *)
echo "Usage: $0 {start|stop|restart|restart_childs|status}"
;; esac

 

然后将脚本放在/etc/init.d/目录下,并加入启动项,对于各种类型的linux,命令有所差异,centos如下:

chkconfig --add service_script
chkconfig service_script on