美文网首页大数据 爬虫Python AI SqlPython小哥哥
使用python3+supervisor+nginx部署torn

使用python3+supervisor+nginx部署torn

作者: 14e61d025165 | 来源:发表于2019-04-18 14:48 被阅读0次

    1 安装supervisor

    本人使用的操作系统是Ubuntu-16.04,最好使用如下命令安装:

    sudo apt-get install supervisor

    1欢迎加入新手技术交流基地:1004391443 群里有大牛解答,有资源,有源码,学不学的会就看你了!

    使用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

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    主要解释以下几点:

    [group:tornadoes]分配组tornadoes,注意这个tornadoes字符串在使用supervisorctl工具时会用到。

    command=/home/python/venv/bin/python3 /home/python/wechat/server.py --port=8080

    前面加黑的是你的python解释器的绝对路径,后面斜体是你的应用程序文件的绝对路径,要使用参数–port指明端口,所以你的程序中要使用tornado.options来添加参数运行程序

    directory——是应用程序文件所在的文件夹的绝对路径

    user——是你的用户名

    stdout_logfile——配置你的log输出文件的位置

    loglevel——log等级

    配置完成后,将该文件放在/etc/supervisor/conf.d/文件夹下,这就是刚才配置supervisord.conf的路径。

    4 启动supervisor

    supervisord -c /etc/supervisor/supervisord.conf

    1

    如果没有报错,在输入命令

    supervisorctl

    1

    成功运行的结果:

    这里写图片描述

    当你修改了你的程序时,你可以通过这个工具来停止,启动或重启你的应用程序

    supervisor> status # 查看程序状态

    supervisor> stop tornadoes:* # 关闭 tornadoes组 程序

    supervisor> start tornadoes:* # 启动 tornadoes组 程序

    supervisor> restart tornadoes:* # 重启 tornadoes组 程序

    supervisor> update # 重启配置文件修改过的程序

    1

    2

    3

    4

    5

    这里的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;
    

    }

    }

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    配置完之后将该文件放在/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()

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    然后查看浏览器,如下,可以用80端口访问了:

    在这里插入图片描述


    相关文章

      网友评论

        本文标题:使用python3+supervisor+nginx部署torn

        本文链接:https://www.haomeiwen.com/subject/fxqtgqtx.html