1. 程式人生 > >使用python3+supervisor+nginx部署tornado

使用python3+supervisor+nginx部署tornado

1 安裝supervisor

本人使用的作業系統是Ubuntu-16.04,最好使用如下命令安裝:

sudo apt-get install supervisor

使用pip3安裝時會出現說supervisor只適合python2的情況而不能成功安裝,但其實用python3寫的tornado也能用supervisor部署

2 配置supervisor

對於16.04版本的Ubuntu系統,安裝成功supervisor後會自動在/etc/下生成一個資料夾supervisor/,所以不需要再手動建立這個資料夾,下面是這個/etc/supervisor/
這裡寫圖片描述
注:conf.d資料夾和配置檔案supervisor.conf都是安裝成功後自動生成的。然後修改surpervisord.conf檔案。部署tornado基本不用怎麼修改,通常只需要將最後一行改為下面的形式就可以了:
[include]
files = /etc/supervisor/conf.d/.conf
預設好像是以“/

.ini”結尾的配置檔案

3 配置tornado.conf檔案

建立一個tornado.conf檔案,並寫下如下內容:

[group:tornadoes]
programs=tornado-8080,tornado-8081

[program:tornado-8080]
command=/home/python/venv/bin/python3 /home/python/wechat/server.py --port=8080
directory=/home/python/wechat/
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/home/python/wechat/tornado.log
loglevel=info

[program:tornado-8081]
command=/home/python/venv/bin/python3 /home/python/wechat/server.py --port=8081
directory=/home/python/wechat
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/home/python/tornado.log
loglevel=info

主要解釋以下幾點:

  1. [group:tornadoes]分配組tornadoes,注意這個tornadoes字串在使用supervisorctl工具時會用到。
  2. command=/home/python/venv/bin/python3 /home/python/wechat/server.py --port=8080
    前面加黑的是你的python直譯器的絕對路徑,後面斜體是你的應用程式檔案的絕對路徑,要使用引數–port指明埠,所以你的程式中要使用tornado.options來新增引數執行程式
  3. directory——是應用程式檔案所在的資料夾的絕對路徑
    user——是你的使用者名稱
    stdout_logfile——配置你的log輸出檔案的位置
    loglevel——log等級

配置完成後,將該檔案放在/etc/supervisor/conf.d/資料夾下,這就是剛才配置supervisord.conf的路徑。

4 啟動supervisor

supervisord -c /etc/supervisor/supervisord.conf

如果沒有報錯,在輸入命令

supervisorctl

成功執行的結果:
這裡寫圖片描述
當你修改了你的程式時,你可以通過這個工具來停止,啟動或重啟你的應用程式

supervisor> status    # 檢視程式狀態
supervisor> stop tornadoes:*   # 關閉 tornadoes組 程式
supervisor> start tornadoes:*  # 啟動 tornadoes組 程式
supervisor> restart tornadoes:*    # 重啟 tornadoes組 程式
supervisor> update    # 重啟配置檔案修改過的程式

這裡的tornadoes就是你配置的組名。
到這裡,一切順利的話,你可以通過瀏覽器訪問你的tornado伺服器了,但是要在url地址後面加上你相應的埠,因為這裡還沒有使用Nginx來監聽80埠。
在這裡插入圖片描述

5 配置Nginx

建立一個以 .conf 結尾的配置檔案:

upstream tornadoes {
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
}

server {
    listen 80;
    server_name IP;  # IP填上你的伺服器IP地址或域名,如果是本地,就是127.0.0.1
	
    # 將Nginx作為代理伺服器
    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;  # 協議 http https
        proxy_pass http://tornadoes;
    }
}

配置完之後將該檔案放在/etc/nginx/conf.d/的目錄下(可能有些舊版本的Nginx的配置檔案不是放在這個目錄下,你可以參考我的另外一篇文章來安裝最新的Nginx伺服器點選檢視),然後將該目錄下的預設配置檔案(default.conf)移走,最好不要直接刪掉。啟動Nginx服務sudo /etc/init.d/nginx start
下面是我的tornado程式碼:

from tornado.web import Application, RequestHandler
from tornado.httpserver import HTTPServer
import tornado.ioloop
import tornado.options

# 下面這行不能少
tornado.options.define("port", type=int, default=8080, help="伺服器埠")

class IndexHandler(RequestHandler):
    def get(self, *args, **kwargs):
        # write表示將資料寫入緩衝區,後面可繼續新增,最終一起返回
        self.write("hello world!")

if __name__ == '__main__':
    # 下面這行不能少
    tornado.options.parse_command_line()
    route = [
        ('/', IndexHandler),
    ]
    server = HTTPServer(Application(route))
    server.listen(tornado.options.options.port)
    tornado.ioloop.IOLoop.current().start()

然後檢視瀏覽器,如下,可以用80埠訪問了:
在這裡插入圖片描述