Tornado
:Tornado 是 Python 编写出来的一个极轻量级、高可伸缩性和非阻塞IO的Web服务器软件,例如前 Friendfeed 网站。
Supervisor
:一个服务(进程)管理工具,主要用于监控服务器上的服务,并且在出现问题时自动重启。
Nginx
:在这里作为反向代理。
CentOS 系统基础环境
因为目前 Supervisor 仅支持 python2,但又需要使用 Python3 来开发,所以它们需要共存。为了不同环境互不干扰,将使用virtualenv
来达到这一目的。
安装 Python 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ ll /usr/bin/python* lrwxrwxrwx 1 root root 7 May 21 10:30 /usr/bin/python -> python2 lrwxrwxrwx 1 root root 9 May 21 10:30 /usr/bin/python2 -> python2.7 -rwxr-xr-x 1 root root 7216 Apr 11 15:36 /usr/bin/python2.7 $ yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make $ wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz $ tar xzvf Python-3.6.5.tgz $ cd Python-3.6.5 $ ./configure perfix=/usr/local /python3 $ make && make install $ python -V $ mv /usr/bin/python /usr/bin/python.bak $ ln -s /usr/local /python3/bin/python3.6 /usr/bin/python $ python -V $ vi /usr/bin/yum $ vi /usr/libexec/urlgrabber-ext-down
安装 Tornado
1 2 3 4 5 $ pip3 install tornado $ mkdir /var/www $ vi /var/www/index.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import tornado.ioloopimport tornado.web class MainHandler (tornado.web.RequestHandler) : def get (self) : self.write("Hello, Tornado!" ) application = tornado.web.Application([ (r"/" , MainHandler), (r"/index.py" , MainHandler), ]) if __name__ == "__main__" : application.listen(8006 ) tornado.ioloop.IOLoop.instance().start()
Test
1 2 3 $ python3 index.py http://10.0.77.119:8006
安装 Nginx
1 2 3 4 5 6 7 8 $ cat /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch / gpgcheck=0 enabled=1 $ yum install nginx
nginx signing key
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 $ cat /etc/nginx/nginx.conf user nginx; worker_processes 1; error_log /var/log /nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; use epoll; } http { upstream tornado { server 10.0.77.119:8006; } server { listen 80; root /var/www; index index.py index.html; server_name server; location / { if (!-e $request_filename ) { rewrite ^/(.*)$ /index.py/$1 last; } } location ~ /index\.py { proxy_pass_header Server; proxy_set_header Host $http_host ; proxy_set_header X-Real-IP $remote_addr ; proxy_set_header X-Scheme $scheme ; proxy_pass http://tornado; } } }
安装 Supervisor
pip2
1 2 3 $ yum install epel-release $ yum install python-pip
1 2 $ curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" $ python get-pip.py
virtualenv
1 $ pip2 install virtualenv
配置 supervisor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 $ mkdir /etc/supervisor $ virtualenv --distribute -p /usr/bin/python2 supervisor Already using interpreter /usr/bin/python2 New python executable in /etc/supervisor/bin/python2 Also creating executable in /etc/supervisor/bin/python Installing setuptools, pip, wheel...done. $ cd supervisor/ $ source bin/activate (supervisor) [root@tornado-web supervisor] ... Successfully installed meld3-1.0.2 supervisor-3.3.4 (supervisor) [root@tornado-web supervisor] [unix_http_server] file=/var/run/supervisor.sock chmod=0700 [inet_http_server] port=10.0.77.119:9001iface username=user password=123 [supervisord] logfile=/var/log /supervisor/supervisord.log pidfile=/var/run/supervisord.pid [supervisorctl] serverurl=unix:///var/run/supervisor.sock [program:hello] command =/usr/local /bin/python3.6 /var/www/index.py --port=8006directory=/var/www autorestart=true redirect_stderr=true (supervisor) [root@tornado-web supervisor] (supervisor) [root@tornado-web supervisor] hello RUNNING pid 5107, uptime 0:05:04 supervisor> help default commands (type help <topic>): ===================================== add exit open reload restart start tail avail fg pid remove shutdown status update clear maintail quit reread signal stop version (supervisor) [root@tornado-web supervisor]
Reference