安裝:sudo apt-get install python-tornadohtml
sudo apt-get install nginxpython
sudo apt-get install supervisornginx
1. 配置nginx。我安裝的nginx的目錄爲2個配置文件。/etc/nginx/nginx.conf 和 /etc/nginx/conf.d/foo.conf.web
其實是nginx.conf包含了文件foo.confshell
因此若是沒有特殊需求,就不須要更改nginx.conf了,在conf.d文件夾下新建你的conf文件。socket
foo.conf的內容;tornado
upstream foo { #ip_hash; #consistent_hash $args;這裏就是你須要nginx幫你代理的端口,下面的意思是將訪問80端口的請求分配到6900,6901,6902上去。其餘的無需更改。若是你不用supervisor,那就須要手工運行你的web程序,監聽6900,6901,6902 server 127.0.0.1:6900 weight=1 max_fails=2 fail_timeout=30s; server 127.0.0.1:6901 weight=1 max_fails=2 fail_timeout=30s; server 127.0.0.1:6902 weight=1 max_fails=2 fail_timeout=30s; } server { listen 80; server_tag off; index index.html; charset utf-8; location / { proxy_read_timeout 100; proxy_pass_header User-Agent; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Scheme $scheme; proxy_set_header Accept-Encoding 'gzip'; proxy_pass http://foo; proxy_redirect off; proxy_store off; } location /stat { stub_status on; access_log off; } proxy_buffering off; }
3. 運行nginx:sudo /usr/sbin/nginxui
若是發生這個錯誤:this
緣由是80已經被佔用了,頗有多是被nginx佔用了,url
找到進程結束掉或者結束掉nginx進程。/usr/sbin/nginx -s stop
4. /etc/supervisor/supervisord.conf內容:()
[unix_http_server]
file=/var/run//supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run//supervisor.sock ; use a unix:// URL for a unix socket
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
[include]
files = /etc/supervisor/conf.d/*.conf##這裏就是include了具體的配置文件,你須要在/etc/supervisor/conf.d/文件夾下新建conf文件。
好比你在conf.d文件夾下新建的tornado.conf的文件
[program:tornado-80] command=python /var/www/starter.py 6901 #這裏就是運行web,若是跟nginx結合,還須要監聽6902,6900端口 directory=/var/www/ user=www-data autorestart=true redirect_stderr=true stdout_logfile=/var/log/tornado.log loglevel=info
啓動supervisor:
Usage: /etc/init.d/supervisord {start|stop|restart|force-reload|status|force-stop}
進入supervisor命令行:
sudo supervisorctl
5. 守護supervisor進程:
#!/bin/sh pywebn=`ps aux | grep supervisord | grep -v "grep" | wc -l`; #wc -l 的意思是輸出文件的行數,若是不等於1,則說明沒有運行了,須要再啓動下 if [ "$pywebn" != "1" ]; then sleep 1; /usr/bin/python /usr/local/bin/supervisord -c /etc/supervisord.conf; else break; fi
6. 排錯
INFO gave up: tornado entered FATAL state, too many start retries too quickly
這個網上有不少個狀況,基本上是由於你的程序執行了就退出了,而後又執行,又退出,致使的
要看看你的程序是否有問題,個人狀況是python web.py 80 的時候就有問題,換成python web.py 8888 就沒問題了。
直接在shell中執行python web.py 80 就沒有問題
若是supervisor有問題,除了須要看/var/log/supervisor/supervisord.log還要看你配置的log,如/var/log/tornado.log