The original article is reprinted from liu Yue’s Technology blog v3u.cn/a_id_102

This time, the combination of Vue. js+ Tornado was used to deploy the Web project with the separation of front and rear ends. Needless to say, vue.js is popular in the front end and is widely used. No matter it is single page application, mixed development APP, or wechat small program development, everything is very handy. Tornado recently lost its spotlight to the new framework SANIC, but as an old asynchronous non-blocking framework, it has built-in efficient network libraries supporting epoll/ Kqueue and has the ability to deal with high concurrency and development efficiency is not low. Tornado acts as a carrier of back-end pressure this time.

It is worth mentioning that node.js service is not needed in the production environment, because the front-end page can be packaged by using the features of vue.js to make it a pure static page package, and nginx can be used to proxy it after it goes online, which is convenient and safe. Run the NPM run build command in the project directory. Before executing, configure bulid in the index.js directory in the conifg directory.. / dist to. / dist

After the command is executed, a static page is generated in the dist directory under the config directory of the project, and the dist directory is uploaded to the /root directory of the server

Run chmod 755 /root/dist to authorize project files

Then, install nginx online

# set the source
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

# installation
yum install -y nginx

# start service
systemctl start nginx.service
Copy the code

The server needs to expose port 80. At this point, test to see if the welcome page can be displayed

No problem, then revise the nginx vim configuration file/etc/nginx/conf. D/default. Conf increase the configuration, the front-end service here listening port 80 by default

server {
    listen       80;
    server_name  localhost;

    access_log      /root/md_vue_access.log;
    error_log       /root/md_vue_error.log;


    client_max_body_size 75M;


    location / {

        root /root/dist;
        index index.html;
        try_files $uri $uri/ /index.html;

    }
    
    error_log    /root/dist/error.log    error;

}
Copy the code
Continue to modify the configurationVim /etc/nginx/nginx.conf change the first line to user root;Copy the code

After that, restart the nginx service

systemctl reload nginx.service
Copy the code

You must restart the service every time you modify the nginx configuration. Otherwise, the modification will not take effect

Visit it and see what it looks like

At this point, the front end is deployed, isn’t it simple? Instead of the ugly and original template of the old Meadow Store, we have a new template with a responsive design that works with any size screen and can be packaged as APK, which is awesome

Python2.7 is installed on centos. Do not affect python2.7 when installing software, because the system depends on Python2 for many things, so we only need python2 and Python3 coexist

yum install epel-release yum install python36 wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py python3 Get-pip3 install Pymysql pip3 Install Pillow PIp3 Install Pycryptodome Pip3 Install Tornado ==5.1.1 PIp3 Install sqlalchemyCopy the code

Upload Tornado project to /root/MyTornado, modify the project permission: chmod-r 755 /root/myTornado, then close the debug mode of the project, modify the port number to 8001, and expose the 8001 port of Aliyun for project root directory. Start the service :python3 server.py and check

There is no problem. At this time, modify the Nginx configuration file, reverse proxy Tornado with Nginx, and create a new configuration file

vim /etc/nginx/conf.d/tornado.conf
Copy the code
Tornado {server 127.0.0.1:8001; } server { listen 8000; root /root/mytornado; index server.py index.html; server_name server;Static files are handled directly by Nginx
    location /static/{
        alias /root/mytornado/static/;
        expires 24h;
    }
    location /{
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        # Transfer requests to tornado server, load balancingproxy_pass http://tornado; }}Copy the code

This configuration means that Nginx listens on port 8000 and reverse proxies requests to Tornado service. Here, we only have one 8001 service and more can be started, which is load balancing in the traditional sense

Restart nginx

systemctl restart nginx.service
Copy the code

Access server 8000 port, Ali cloud also do not forget to expose 8000

Finally, it is troublesome to manually start the application on the command line every time. We also need a convenient tool to manage the service process, including automatic restart of the process, etc. The role of Supervisor can be reflected here. We used it to manage the Tornado Web Server-related process

The installation supervisor

yum install epel-release
yum install -y supervisor
Copy the code

Generating a Configuration File

supervisord -c /etc/supervisord.conf 
Copy the code

Modifying a Configuration File

It is then modified to the configuration file vim /etc/supervisord.conf to unpack the following lines of comments

[inet_http_server]         ; inet (TCP) server disabled by default
port=*:9001        ; (ip_address:port specifier, *:port for all iface)
username=user              ; (default is no username (open server))
password=123               ; (default is no password (open server))
Copy the code

Note that the IP address should be written *, otherwise the external network can not access, and username and password are the username and password to log in to the service page, can be changed a bit more complex, in addition, ali cloud also need to expose the 9001 port to the external network

Then add tornado’s configuration at the end of the profile

[program:mytornado]
command=python3 /root/mytornado/server.py --port=8001
directory=/root/mytornado
autorestart=true
redirect_stderr=true
Copy the code

After saving the file and exiting, start the Supervisor service

supervisord -c /etc/supervisord.conf
Copy the code

At this point, close Tornado Service, which has just been manually started, then access port 9001 of the server and log in with the account and password in the configuration file

In this way, Tornado service can be controlled in the management page. At the same time, the Supervisor also endows the daemon mode to facilitate the service to be pulled up

Note that when the Supervisor configuration is modified, the Supervisor service must be restarted to take effect. The command to terminate the Supervisor service is:

killall -s INT /usr/bin/python
Copy the code

Because the Supervisor is python2 based, you don’t have to worry about python3 processes

It is also possible to control Tornado only from the command line

# Stop Tornado Service
supervisorctl stop mytornado

# Launch tornado Service
supervisorctl start mytornado
Copy the code

At this point, we deployed tornado load balancing through nGINx reverse proxy and supervisor management, which was pretty simple

The original article is reprinted from liu Yue’s Technology blog v3u.cn/a_id_102