Alibaba Cloud ECS server deploys web applications and uses flask+uwsgi+nginx to deploy services

Here is a web service deployment method that provides interface services. Implementation of web application deployment based on flask+uwsgi+nginx, the implementation logic is shown in the following figure: The
Insert picture description here
project is deployed based on two Alibaba Cloud ECS servers, and the physical topology is as follows:
Insert picture description here
Next, the project implementation process is introduced in three aspects.

1. Flask-based python application

There are two scripts run.py and main.py

run.py:

#!usr/bin/env python
# _*_ coding:utf-8 _*_
"""python
Created on 2020/8/19
@author: 
@theme:接口开发
"""
from main import app
if __name__ == "__main__":
    app.run()
    # app.run(host="0.0.0.0", port=8080, debug=True, threaded=True)  

main.py

#!usr/bin/env python
# _*_ coding:utf-8 _*_
# pylint: disable=no-member
"""python
Created on 2019/2/10
@author: 
@theme:接口开发
"""
import datetime
import time
import pandas as pd
from flask import Flask, jsonify, request
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
#from flask_redis import FlaskRedis
from utilities import log
from utilities.mysql_conn import get_conn
import config
import get_data_funs
import AES_funs

app = Flask(__name__)
#redis_client = FlaskRedis(app)
limiter = Limiter(
    app,
    key_func=get_remote_address,   # 根据访问者的IP记录访问次数
    default_limits=["10000 per day", "1000 per hour"], # 默认限制,一天最多访问1000次,一小时最多访问100次
    #storage_uri="redis://localhost:6379/0"  #需开启redis服务器
)

# 查询接口apikey账号所拥有的权限列表
@app.route('/queryStationID', methods=['POST'])
def get_station_id():
    sub_log = log.logger.getChild("get_station_id")
    # 获取用户的ip、访问客户端类型、访问url
    user_ip = request.remote_addr
    # user_agent = request.headers.get("User-Agent")
    visit_url = request.url
    proc_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')  # 获取当前系统时间
    # 默认返回内容
    return_dict = {
    
    'return_code': 200, 'return_msg': 'success', 'proc_time': proc_time, 'station_id_list': ''}

    # 获取入参并判断入参是否填写
    params = request.get_json(force=True)  # 获取请求体body为raw的参数
    apikey = params['apikey']

    if (apikey): # 参数全部填写
        # 判断apikey的状态是否正常且是否过期失效
        sql_valid_apikey = "SELECT * from table WHERE status = 1 and DATEDIFF(invalid_time,NOW())>=0 " \
                           "and apikey='{}';".format(apikey)
        apikey_record = pd.read_sql(sql_valid_apikey, con=get_conn(config.username, config.password, config.hostname,
                                                                   config.port, config.database))

        if (apikey_record.empty is False):  # apikey 有效
            # 查询该apikey对应的station_id
            sql_valid_authority = "select * from table1 where status =1 and apikey='{}' " .format(apikey)
            authority_record = pd.read_sql(sql_valid_authority,
                                           con=get_conn(config.username, config.password, config.hostname,
                                                        config.port, config.database))
            if (authority_record.empty is False): #station_id  有效
                station_id = authority_record['station_id'].tolist()
                return_dict['station_id_list'] = station_id
                return jsonify(return_dict)       

Part of the content is deleted in the main.py script, which involves project privacy, just an example to illustrate the interface development of flask

2 ws uwsgi

Based on a configuration file such as uwsgi.ini, run the service on the ECS 1# server.
Create a new text uwsgi.ini in the directory where the above python script is located, the text content:

[uwsgi]

module = run:app    #要注意服务器默认python解释器能否执行run.py中的app  可指定python解释器

master = true

processes = 3

chdir = /home/restful

socket = %(chdir)/myproject.sock

socket = 192.168.23.22:5000     # 这里的ip必须是ECS1#服务器的内网地址

daemonize = %(chdir)/myapp_uwsgi.log

chmod-socket = 660

vacuum = true

stats = %(chdir)/uwsgi.status

pidfile = %(chdir)/uwsgi.pid

The relevant commands of uwsgi are summarized as follows:

# 启动uwsgi服务命令
uwsgi --ini uwsgin.ini

# 查看一下uwsgi的进程
ps aux | grep uwsgi

# 重启uwsgi服务命令
uwsgi --reload uwsgi.pid

# 显示进程和worker的状态
uwsgi --connect-and-read uwsgi/uwsgi.status

# 停止uwsgi服务命令
uwsgi --stop uwsgi.pid

3、nginx

Start the nginx service through the nginx.conf configuration file. The content of the configuration file is as follows:

worker_processes 6;
events{
    
    worker_connections 6144;}
http{
    
    
    include /etc/nginx/mime.types;
    default_type application/octet_stream;
    server{
    
    
        listen 1543;
        access_log /home/project_nginx/logs/access.log combined;
        error_log /home/project_nginx/logs/error.log error;
        location / {
    
    
            include /etc/nginx/uwsgi_params;
            uwsgi_pass 102.24.12.12:5000;   //ECS1#服务器的外网ip
        }
    }
}

The related commands used by nginx are as follows:

#nginx启动
nginx -c /home/project_nginx/nginx.conf

# nginx重载
nginx -s reload -c /home/project_nginx/nginx.conf

# nginx 停止
nginx -s stop

# 日志切割
0 0 * * * sh /home/project_nginx/nginx_log.sh

The content of the log cutting script nginx_log.sh is as follows:

#!/bin/bash
#设置日志文件存放目录

LOG_HOME="/home/project_nginx/logs/"
#备分文件名称
LOG_PATH_BAK="$(date -d yesterday +%Y%m%d%H%M)".access.log
#重命名日志文件
mv ${
    
    LOG_HOME}/access.log ${
    
    LOG_HOME}/${
    
    LOG_PATH_BAK}.log
#向nginx主进程发信号重新打开日志
kill -USR1 `cat /run/nginx.pid`

summary

After this project starts the uwsgi and nginx services, you can normally call the interface through the external network ip of the ECS 2# server, the listening port number 1543, and the request route address.
The parameters in the configuration file mentioned here can be adjusted. Alibaba Cloud ECS servers are distinguished by internal and external network addresses, and special attention should be paid to the ip address part of the configuration file.

Guess you like

Origin blog.csdn.net/qq_43314560/article/details/113041364
Recommended