参考链接:supervisor + Tornado + Nginx 使用详解, 用tornado ,Supervisord ,nginx架网站, tornado官方文档
https://blog.csdn.net/tengzhaorong/article/details/12833157
项目文档树:
1. 2├── chnservices 3│ └── channels.py 4├── etc 5│ ├── chnservices.conf 6│ ├── nginx 7│ │ └── nginx.conf 8│ ├── supervisord.conf 9│ └── supervisord.conf.original 10└── venv 11 ├── bin 12 │ ├── activate 13 │ ├── activate.csh 14 │ ├── activate.fish 15 │ ├── activate_this.py 16 │ ├── easy_install 17 │ ├── easy_install-2.7 18 │ ├── pip 19 │ ├── pip2 20 │ ├── pip2.7 21 │ ├── python 22 │ ├── python2 -> python 23 │ └── python2.7 -> python 24 ├── include 25 │ └── python2.7 -> /usr/include/python2.7 26 ├── lib 27 │ └── python2.7 28 └── local 29 ├── bin -> /a/path/venv/bin 30 ├── include -> /a/path/venv/include 31 └── lib -> /a/path/venv/lib
supervisor.conf(部分)
1[program:app-channels] 2process_name=%(program_name)s-%(process_num)s 3directory=/a/path/chnservices/ 4command=/a/path/venv/bin/python2.7 /a/path/chnservices/channels.py --port=%(process_num)s 5numprocs=2 6numprocs_start=8001 7;umask=022 8;priority=999 9autostart=true 10startsecs=2 11;startretries=3 12;autorestart=unexpected 13;exitcodes=0,2 14;stopsignal=QUIT 15;stopwaitsecs=10 16;stopasgroup=false 17;killasgroup=false 18user=www-data 19redirect_stderr=true 20stdout_logfile=/var/log/nginx/chn_stdout.log 21;stdout_logfile_maxbytes=1MB 22;stdout_logfile_backups=10 23;stdout_capture_maxbytes=1MB 24;stdout_events_enabled=false 25stderr_logfile=/var/log/nginx/chn_stderr.log 26;stderr_logfile_maxbytes=1MB 27;stderr_logfile_backups=10 28;stderr_capture_maxbytes=1MB 29;stderr_events_enabled=false 30;environment=A="1",B="2" 31;serverurl=AUTO
channels.py
1import tornado.ioloop 2import tornado.web 3from tornado.options import define, options 4 5define("port", default=8006, help="run on the given port", type=int) 6 7class MainHandler(tornado.web.RequestHandler): 8 def get(self): 9 self.write("Hello, world") 10 11def make_app(): 12 return tornado.web.Application([ 13 (r"/", MainHandler), 14 ]) 15 16if __name__ == "__main__": 17 tornado.options.parse_command_line() 18 # tornado.options.parse_config_file("/etc/chnservices.conf") 19 print 'port:',options.port 20 app = make_app() 21 app.listen(options.port) 22 tornado.ioloop.IOLoop.current().start()
nginx.conf(tornado推荐配置修改)
1user www-data; 2worker_processes 1; 3 4error_log /var/log/nginx/error.log; 5pid /var/run/nginx.pid; 6 7events { 8 worker_connections 1024; 9 use epoll; 10} 11 12http { 13 # Enumerate all the Tornado servers here 14 upstream frontends { 15 server 127.0.0.1:8001; 16 server 127.0.0.1:8002; 17 } 18 19 include /etc/nginx/mime.types; 20 default_type application/octet-stream; 21 22 access_log /var/log/nginx/access.log; 23 24 keepalive_timeout 65; 25 proxy_read_timeout 200; 26 sendfile on; 27 tcp_nopush on; 28 tcp_nodelay on; 29 gzip on; 30 gzip_min_length 1000; 31 gzip_proxied any; 32 gzip_types text/plain text/html text/css text/xml 33 application/x-javascript application/xml 34 application/atom+xml text/javascript; 35 36 # Only retry if there was a communication error, not a timeout 37 # on the Tornado server (to avoid propagating "queries of death" 38 # to all frontends) 39 proxy_next_upstream error; 40 proxy_next_upstream error; 41 42 server { 43 listen 8000; 44 45 # Allow file uploads 46 client_max_body_size 50M; 47 48 location ^~ /static/ { 49 root /var/www/tornado/; 50 if ($query_string) { 51 expires max; 52 } 53 } 54 location = /favicon.ico { 55 rewrite (.*) /static/favicon.ico; 56 } 57 location = /robots.txt { 58 rewrite (.*) /static/robots.txt; 59 } 60 61 location / { 62 proxy_pass_header Server; 63 proxy_set_header Host $http_host; 64 proxy_redirect off; 65 proxy_set_header X-Real-IP $remote_addr; 66 proxy_set_header X-Scheme $scheme; 67 proxy_pass http://frontends; 68 } 69 } 70}
安装:
使用supervisor做进程管理,使用Nginx做反向代理;supervisor可以使用源码安装和yum安装(CentOS),使用pip安装和源码安装是一样的道理:
sudo pip install supervisor
supervisor默认会从(/usr/local/etc/supervisord.conf, /usr/local/supervisord.conf, supervisord.conf, etc/supervisord.conf, /etc/supervisord.conf)寻找配置文件,为了方便,我们生成配置文件并修改后,链接到/etc/supervisord.conf。
生成配置文件:
echo_supervisord_conf > /a/path/etc/supervisord.conf
按示例修改,修改后链接到指定位置:
sudo ln -s /a/path/etc/chnservices.conf /etc/chnservices.conf
将Nginx配置文件修改后链接到默认位置:
sudo ln -s /a/path/etc/nginx/nginx.conf /etc/nginx/nginx.conf
使Nginx中设置的静态目录与项目的静态目录保持一致:
ln -s /a/path/chnservices /var/www/tornado
启动supervisor:
1sudo supervisord -c /etc/supervisord.conf 2sudo supervisorctl start all 3sudo supervisorctl reload all 4sudo supervisorctl restart all
此时,浏览器访问http://127.0.0.1:8001/和http://127.0.0.1:8002/应该就可以看到“Hello, world”了.
启动nginx:
sudo service nginx restart
此时,浏览器访问http://127.0.0.1:8000/就可以看到从tornado经nginx的“Hello, world”了.
添加开机启动:
tornado代码遇到异常退出时supervisor会自动重新启动我们的python代码,但是现在supervisor不是开机启动的,编辑/etc/rc.local, 在exit 0之前添加sudo supervisord即可。