Supervisor + Nginx + Python3 deploy Tornado

Supervisor deployment tornado

#1 surroundings##

CentOS 7.6
Python3.7

#2 demand analysis##

Therefore, this article mainly introduces the use of Supervisor + Nginx to deploy a simple Tornado application

#3 Overall division of labor##

Used to start/close Tornado application management

Reverse proxy + Load Balancing

#4 Supervisor

#4.1 Install Supervisor on CentOS

There are two installation methods:

yum install -y supervisor
pip3 install supervisor

This article uses pip3 to install

This method of installation will not automatically generate configuration files, you need to manually create configuration folders and configuration files

Manually generate node files:

mkdir -p /etc/supervisor/conf.d 

Manually generate the configuration file:

echo_supervisord_conf >/etc/supervisor/supervisord.conf

#4.2 Explain the role of the two files###

Supervisor configuration file, we only need to care about the last part of the file

......[ include]
files =/etc/supervisor/conf.d/*.conf

This means that by adding the *.conf file under /etc/supervisor/conf.d/, we only need to add the node file in /etc/supervisor/conf.d/ (similar to Nginx configuration files)

Add folder

#4.3 Node configuration file###

[ group:tornadoes]
programs=tornado-9898,tornado-9899[program:tornado-9898]
command=python3 /root/a.py --port=9898
directory=/root
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/root/tornado1.log
loglevel=info                                                                                
[ program:tornado-9899]
command=python3 /root/a.py --port=9899
directory=/root
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/root/tornado2.log
loglevel=info   

Explanation:

python3 /root/a.py --port=9898

The command that needs to be executed, python3 can be replaced with the absolute path of the python interpreter, /root/a.py is my Tornado file path

#4.4 tornado application###

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.httpclient
from tornado.options import define, options

define("port",default=8765, help="run on the given port ", type=int)classExampleHandler(tornado.web.RequestHandler):                                            
 def get(self):
  self.write({"status":1,"msg":"success"})if __name__=="__main__":

 tornado.options.parse_command_line()
 app = tornado.web.Application(handlers=[(r"/", ExampleHandler)])
 http_server = tornado.httpserver.HTTPServer(app)
 http_server.listen(options.port)
 tornado.ioloop.IOLoop.instance().start()

#4.5 operating###

supervisord -c /etc/supervisor/supervisord.conf
ps -ef | grep supervisord
kill -s SIGTERM pid

#4.6 Management Supervisor

supervisorctl
supervisor> stop tornadoes:tornado-9898
supervisor> status
supervisor> stop tornadoes:
supervisorctl start program_name
supervisorctl restart program_name
supervisorctl stop groupworker:
supervisorctl stop all
supervisorctl reload
supervisorctl update

#5 Nginx

upstream tornados{
 server 127.0.0.1:9898;
 server 127.0.0.1:9899;}
proxy_next_upstream error;
server {
 listen 9999;
 server_name www.minhung.me;
 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://tornados; #Pass the request direction proxy to tornado server, load balance
    }}

Recommended Posts

Supervisor + Nginx + Python3 deploy Tornado
Deploy python3 and nginx projects on ubuntu18.04
Python tornado upload file function