Record how to deploy the flask interface on the Alibaba Cloud server and achieve public network access.
Article directory
1 Introduction
Due to the landing call callback service test, I need to write a test demo, which is used to receive the http post request sent by the callback service to the demo and return the corresponding response data according to certain logic. The data format is json. The interface code has been completed locally, and the self-test for access within the LAN has passed. The following describes how to deploy this set of interfaces on the Alibaba Cloud server and achieve public network access.
2. Deploy python3 environment
Python 3.7 is used locally, and a python3 environment needs to be deployed on the cloud server. Reference link: https://blog.csdn.net/u011090984/article/details/130984739
3. Generate requirement.txt
In the local pycharm test demo, execute the following statement in the virtual environment to generate a requirement.txt. This document will be used to install dependent libraries on the cloud server.
pip3 freeze > requirement.txt
4. Package and upload the project
rz -e #上传
unzip callbackdemo.zip #解压
The above callbackdemo.zip can be replaced with your own project package name.
5. Install dependent libraries
Enter the project directory and grant permissions to the executable py file
chmod +x aaa.py
Install dependent libraries.
pip3 install -r requirement.txt
After execution is completed, the dependent libraries are installed. At this time, we can only access the interface locally. In my interface, host='0.0.0.0' and post=5001. Therefore, you need to open port 5001 in the Alibaba Cloud security group. As shown below:
The configuration is saved in both the inbound and outbound directions. There is no need to restart the instance and it takes effect in real time.
6. Check the firewall
After configuring the security group, the server firewall will still block port 5001. So check the firewall status.
firewall-cmd --state
The default status here is not running, and the firewall is not enabled, so there is no need to worry about firewall issues. If the firewall is turned on, either choose to turn it off, or choose the command to configure the firewall to allow 5001 ingress and egress traffic:
firewall-cmd --add-port=5000/tcp --permanent #放行端口
firewall-cmd --reload #重启生效
7. Test whether it can be accessed from the public network
The cloud server starts the interface service through python3 -m main. For computers on the local LAN, use postman to access the interface. The interface address uses the cloud server public network IP + 5001 port + routing.