supervisor部署tornado
- supervisord常见命令
supervisorctl shutdown 关闭命令 supervisord -c /etc/supervisord.conf 启动supervisord supervisorctl restart all // 重启所有 supervisorctl restart program-name // 重启某一进程,program-name为[program:xx]中的xx supervisorctl status //查看supervisord状态
- 生成配置文件
echo_supervisord_conf > /etc/supervisord.conf
- 编辑配置文件
由于配置文件生成在etc目录下,我们要做进一步配置
带有;的行是被注释的,并不真正发挥作用 [program:olddream] command=python3 /dingshub/olddream/server.py --port=8989
; 注意上面一行配置,由于我在linux为python建立的软链接名为python3,各程序员实践的时候需要按照情况作调整 ;process_name=%(program_name)s ; process_name expr (default %(program_name)s) ;numprocs=1 ; number of processes copies to start (def 1) directory=/dingshub/olddream/ ; 网站文件夹实际路径,这一路经下放着tornado的起始配置文件server.py ;umask=022 ; umask for process (default None) ;priority=999 ; the relative start priority (default 999) autostart=true ; start at supervisord start (default: true) ;autorestart=unexpected ; whether/when to restart (default: unexpected) ;startsecs=1 ; number of secs prog must stay running (def. 1) ;startretries=3 ; max # of serial start failures (default 3) ;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) ;stopsignal=QUIT ; signal used to kill process (default TERM) ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) ;stopasgroup=false ; send stop signal to the UNIX process group (default false) ;killasgroup=false ; SIGKILL the UNIX process group (def false) ;user=chrism ; setuid to this UNIX account to run the program redirect_stderr=true ; redirect proc stderr to stdout (default false) stdout_logfile=/dingshub/olddream/dings.log ; 最好设置一个log日志文件 ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) ;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10) ;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) ;stdout_events_enabled=false ; emit events on stdout writes (default false) ;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO ;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) ;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10) ;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) ;stderr_events_enabled=false ; emit events on stderr writes (default false) ;environment=A="1",B="2" ; process environment additions (def no adds) ;serverurl=AUTO ; override serverurl computation (childutils) loglevel=info ; 这是一个中等级别的日志
- nginx配置(一个地址多个站点)
分站点配置---这是tornado网站的配置细节
我的nginx是通过编译源代码安装的,在nginx的安装目录下,新建一个vhosts文件夹(如果没有的话)
在vhosts文件夹下建立以 .conf为后缀的文件
例如:
upstream tornados{
server 127.0.0.1:8989; #可以发现这里8989端口与supervisord中的端口一致
server 127.0.0.1:8021;
server 127.0.0.1:8022;
}
proxy_next_upstream error;
server {
listen 8989;
server_name sniffcpcssocks.com;
root /dingshub/olddream;
index server.py;
# 静态文件直接由Nginx处理
location /static/{
alias /data/web/advance_python/tornado_asyn/img/;
expires 24h;
}
location /{
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
# 把请求方向代理传给tornado服务器,负载均衡
proxy_pass http://tornados;
}
}
- nginx conf文件夹下 nginx.conf主文件配置(我也并不了解nginx配置要诀---这种配置起码可以保证网站跑起来)
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; include /nginx/vhosts/*.conf; #这一句是一个ip地址,多个站点配置的关键 sendfile on; keepalive_timeout 65; server { listen 8999; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
- tornado网站主程序文件server.py(注意!这一程序文件里,不要出现任何注释符号,还有中文,因为supervisord执行时可能会出现异常)
import tornado.ioloop import tornado.web import tornado.httpserver import os .......... settings={ 'template_path':os.path.join(os.getcwd(),"templates"), 'static_path':os.getcwd()+"//templates//static" } def App(): return tornado.web.Application([ (r'/loadcolspage',loadcolspage), ......... (r'/xsfp/list', xsfp_v)],**settings) if __name__=="__main__": app = App() print(("current_path:"),os.path.join(os.getcwd()+"//templates",'static')) myserver = tornado.httpserver.HTTPServer(app) myserver.listen(8989) tornado.ioloop.IOLoop.current().start()