• centos7 使用nginx + tornado + supervisor搭建服务


    如何在Linux下部署一个简单的基于Nginx+Tornado+Supervisor的Python web服务。

    Tornado:官方介绍,是使用Python编写出来的一个极轻量级、高可伸缩性和非阻塞IO的Web服务器软件,著名的 Friendfeed 网站就是使用它搭建的。官方网站:http://www.tornadoweb.org/

    Supervisor:一个服务(进程)管理工具,主要用于监控我们的服务器上的服务,并且在出现问题时重启之。

    Nginx:作为Web服务器,在这里主要利用它做反向代理。

    整个的工作流程就是客户端访问Nginx主机,由Nginx反向代理到后端Tornado进程的服务器,而Tornado进程则由Supervisord管理。和其它常见的web服务架构相似,比如Nginx + PHP-FPM。

    1:准备工作:

      (1):编写代码: index.py

    import tornado.httpserver
    import tornado.ioloop
    import tornado.options
    import tornado.web
    import tornado.httpclient
    
    from tornado.options import define, options
    define("port", default=8001, help="run on the given port ", type=int)
    define("log_path", default='/tmp', help="log path ", type=str)
    
    class IndexHandler(tornado.web.RequestHandler):
        def get(self):
            headers = self.request.headers
            for k, v in headers.items():
                print(k, v)
            greeting = self.get_argument('greeting', 'Hello')
            self.write('%s , friendly user! %s ' % (greeting, headers))
    
        def write_error(self, status_code, **kwargs):
            self.write('Holly Shit Error %s' % status_code)
    
    
    if __name__=="__main__":
        tornado.options.parse_command_line()
        app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
        http_server = tornado.httpserver.HTTPServer(app)
        http_server.listen(options.port)
        tornado.ioloop.IOLoop.instance().start()

           (2):本地运行:通过浏览器访问:http://127.0.0.1:8001 ,看看访问能否成功。

           (3):如果能访问成功,将代码放到远程服务器上(centos7云服务),文件位置自定(本例  /var/data/index.py  ).

          (4): tornado.options.parse_command_line()  //添加这段,否则在使用supervisor配置多个端口是会报端口占用错误。

    安装supervisor : https://blog.csdn.net/donggege214/article/details/80264811 

    2:安装Tornado:(centos7中默认python是2.7)

          sudo pip install tornado

      (可能会因为python版本问题出现错误,那就添加版本号安装 : sudo pip install tornado==3.2.1 ) 

     

    3:安装nginx

         sudo yum install nginx  

    #启动   sudo systemctl start nginx
    #停止   sudo systemctl stop nginx
    #重启   sudo systemctl restart nginx
    #查看状态  sudo systemctl status nginx

         打开80端口,通过IP访问看看是否能访问成功。

    4: 配置nginx:

     (1):修改文件 /etc/nginx/nginx.conf:

    sudo vim /etc/nginx/nginx.conf 在 http{} 中添加 upstream tornadoes { server 127.0.0.1:8000; server 127.0.0.1:8001; server 127.0.0.1:8002; server 127.0.0.1:8003; }  (2):修改文件/etc/nginx/conf.d/default.conf:
    sudo vim /etc/nginx/conf.d/default.conf 

    location
    / {
      proxy_pass_header Server;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_set_header X
    -Real-IP $remote_addr;
      proxy_pass http:
    //
    tornadoes;
    }

    5:安装 supervisor :

     sudo su  #切换为root用户
    
     yum install epel-release
     yum install -y supervisor
    
     systemctl enable supervisord # 开机自启动

      管理 supervisord:

    # systemctl stop supervisord    #停止
    # systemctl start supervisord    #开始
    # systemctl status supervisord   #查看状态
    # systemctl reload supervisord   #重新启动
    # systemctl restart supervisord    #平滑启动
    # supervisorctl status      # 查看启动的线程

     6:配置 supervisor

    修改配置文件
    
    sudo vim /etc/supervisord.conf
    
    (1):将所有带 /tmp/...的路径修改下:
         file=/tmp/supervisor.sock 
           修改为 
         file=/var/run/supervisor.sock
    
         logfile=/tmp/supervisord.log
           修改为
         logfile=/var/log/supervisord.log
    
         pidfile=/tmp/supervisord.pid
           修改为
         pidfile=/var/run/supervisord.pid
    
         serverurl=unix:///tmp/supervisor.sock
            修改为
         serverurl=unix:///var/run/supervisor.sock

        

    (2):添加如下代码,对python进行管理
    
    
    # 为了方便管理,增加一个tornado组
    [group:tornados]
    programs=tornado-0,tornado-1,tornado-2
    
    # 分别定义三个tornado的进程配置
    [program:tornado-0]
    # 进程要执行的命令
    command=python /var/data/index.py --port=8020    #python文件路径,端口号跟前面nginx配置的端口号对应
    directory=/var/data/
    user=root# 自动重启
    autorestart=true
    redirect_stderr=true
    # 日志路径
    stdout_logfile=/var/log/nginx/tornado0.log
    loglevel=info
    
    [program:tornado-1]
    command=python /var/data/index.py --port=8021
    directory=/var/data/index.py
    user=root
    autorestart=true
    redirect_stderr=true
    stdout_logfile=/var/log/nginx/tornado1.log
    loglevel=info
    
    [program:tornado-2]
    command=python /var/data/index.py --port=8022
    directory=/var/data/
    user=root
    autorestart=true
    redirect_stderr=true
    stdout_logfile=/var/log/nginx/tornado2.log
    loglevel=info
    修改配置后需要重新加载
    
    # supervisorctl update     #每次修改配置后更新一下配置
     
        注意:#每次修改配置文件后运行  可能出现一些问题,如(error: <class 'socket.error'>, [Errno 2] No such file or directory: file: /usr/lib64/python2.7/socket.py line: 224)
           
         #则先运行   sudo /usr/bin/python2 /usr/bin/supervisord -c /etc/supervisord.conf //你的supervisord配置文件位置
          #再运行    supervisorctl update 
    
    
    # supervisorctl reload      #每次修改配置文件后重新启动
    7:启动nginx
        sudo systemctl start nginx

    8:查看运行状态:
       supervisorctl status

       浏览器通过ip访问:

    如果无法访问,查看日志  /var/log/nginx/  中 

    tornado0.log
    tornado1.log
    tornado2.log

      

    参考文档 : https://www.jianshu.com/p/9bebb99368ea

                     https://www.cnblogs.com/renfanzi/p/6233490.html#_label4

                    

     
     


  • 相关阅读:
    [No0000C9]神秘的掐指一算是什么?教教你也会
    [No0000C8]英特尔快速存储IRST要不要装
    [No0000C7]windows 10桌面切换快捷键,win10
    [No0000C6]Visual Studio 2017 函数头显示引用个数
    [No0000C4]TortoiseSVN配置外部对比工具
    [No0000C5]VS2010删除空行
    [No0000C3]StarUML2 全平台破解方法
    [No0000C2]WPF 数据绑定的调试
    [No0000C1]Excel 删除空白行和空白列VBA代码
    [No0000C0]百度网盘真实地址解析(不用下载百度网盘)20170301
  • 原文地址:https://www.cnblogs.com/cj8988/p/11288892.html
Copyright © 2020-2023  润新知