How to run the Flask project on the Alibaba Cloud server behind the Pagoda panel and be accessible from the public network?

Install the pagoda panel on your server

Pagoda panel is a server operation and maintenance management system
Before using Pagoda: Manually input commands to install various software, which is time-consuming, laborious and error-prone, and requires memorizing many Linux commands, which is very complicated.

After using the pagoda: Install the panel in 2 minutes, manage the server with one click, and replace the complicated and numerous commands before with a few clicks of the mouse. The operation is simple, and you can use it at a glance.
https://www.bt.cn/new/download.html

Use the FTP tool FileZilla to transfer the Flask project

insert image description here

Set startup file

insert image description here

Public network ip access will report an error

* Serving Flask app 'run'
* Debug mode: on
Cannot assign requested address

Nginx reverse proxy Flask to achieve public network access

Install Nginx with Pagoda Panel

If the server's public IP address is inconsistent with the IP address you set in the Flask application, it may be because the server is in a local area network, and the public IP address is assigned by a router or firewall. In this case, you need to set up port forwarding or use a reverse proxy to achieve public network access.

cd www/server/panel/vhost/nginx

A common way is to use Nginx to reverse proxy the Flask application. The reverse proxy can forward the client's request to the server where the Flask application is located, and then return the application's response to the client, thereby realizing public network access.

Specifically, you need to install and configure Nginx on the server, and then add reverse proxy rules in the Nginx configuration file to forward the client's request to the port where the Flask application is located.

insert image description here

Then you need to change the address that the Flask application listens to to the public IP address instead of using Nginx to reverse proxy to the local IP address.

Specifically, you need to change the listening address of the Flask application to 0.0.0.0, so that the Flask application will listen to all available network interfaces, including public IP addresses. For example, you can add the following code to your Flask application:

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Among them, host='0.0.0.0' indicates that the Flask application will listen to all available network interfaces, including public IP addresses. port=5000 means that the Flask application will listen on port 5000.

Then, in the Nginx configuration file, you can use the following rule to reverse proxy public traffic to the Flask application:

location / {
    
    
    proxy_pass http://localhost:5000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}

Among them, proxy_pass http://localhost:5000 means to forward all requests to the local port 5000, which is the port where the Flask application is located. proxy_set_header Host $host and proxy_set_header X-Real-IP $remote_addr are to set the request header information to ensure that the Flask application can correctly obtain the requested host name and IP address.

It should be noted that if you want to deploy the website to the public network, you need to ensure that the network environment and security environment of the server are fully protected to avoid security problems. Adequate security assessment and testing is recommended prior to deployment.

For example, assuming the Flask application listens on 127.0.0.1:5000, you can add the following rule to the Nginx configuration file:

server {
    
    
    listen 80;
    server_name example.com;

    location / {
    
    
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

The above configuration forwards the client's request to 127.0.0.1:5000, and sets the Host and X-Real-IP header information so that the Flask application can process the request correctly.

It should be noted that the configuration file of Nginx may vary depending on the version and system. For specific configuration methods, please refer to the official documentation of Nginx or other related materials.

Guess you like

Origin blog.csdn.net/weixin_45934622/article/details/130820119