CentOS 7.6
Python3.7
Therefore, this article mainly introduces the use of
Supervisor
+Nginx
to deploy a simple Tornado application
Used to start/close Tornado application management
Reverse proxy + Load Balancing
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
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
[ 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
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()
supervisord -c /etc/supervisor/supervisord.conf
ps -ef | grep supervisord
kill -s SIGTERM pid
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
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
}}