Detailed steps on how to deploy Flask project under cloud server

Referring to various solutions on the Internet, combined with the Django deployment solution I learned before, it was finally determined that the overall deployment of Flask is based on: centos7+nginx+uwsgi+python3+Flask.

I have developed and tested my OCR project locally on Windows. Now I want to deploy my OCR project to the cloud server for verification.

Step 1: Package and upload the Flask project code to the specified directory on the server

As shown in the picture:

Step 2: Install Flask, PaddleOCR and other related dependency packages

Start myapp.py manually, check which packages are missing, and download the missing packages.

Step 3: Install the Linux version of paddlepaddle Baidu Feipang

Enter Baidu Feijian official website installation address:

https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/windows-pip.html

 Execute the download command on the server:

python -m pip install paddlepaddle==2.5.1 -i https://pypi.tuna.tsinghua.edu.cn/simple

Step 4: Verify PaddleOCR installation

Execute the myapp.py startup program to be deployed, other verification methods are also acceptable.

Precautions:

Generally, a libstdc++.so.6 problem will appear when PaddleOCR is installed for the first time on the server.

 /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found problem solution:

1. Check and verify whether GLIBCXX_3.4.20 is missing

[root@localhost]# strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX

Found that GLIBCXX_3.4.20 is missing, the solution is to upgrade libstdc++.

Then I searched a lot on the Internet about how to upgrade libstdc++. I tried more than 10 methods but none of them worked. It may be related to the server version.
Finally, through direct search, linux downloads libstdc++.so.6.0.25
and finds libstdc++.so.6.0.25. Download link:
https://download.csdn.net/download/qq_39466755/87812280?utm_source=bbsseo
Spend some points to download to libstdc++ .so.6.0.25 file.
Then upload the file to the server, in the /usr/lib64 directory

Then perform the following steps:
0. Enter the current directory:
[root@localhost]# cd /usr/lib64

1. Delete the old libstdc++.so.6 soft connection
[root@localhost]# rm -rf libstdc++.so.6

2. Create a new soft link pointing to the 6.0.25 version of the library
[root@localhost]# ln -s libstdc++.so.6.0.25 libstdc++.so.6

3. View the libstdc++.so file
[root@localhost]# ls -lrt libstdc++.so*
-rwxr-xr-x 1 root root 995840 September 30 2020 libstdc++.so.6.0.19
-rwxrwxr-x 1 root root 12129824 8 August 19 17:01 libstdc++.so.6.0.25
lrwxrwxrwx 1 root root 19 August 19 17:18 libstdc++.so.6 -> libstdc++.so.6.0.25

3. Run again to check the GLIBCXX version
[root@localhost]# strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX

Step 5: Configure uwsgi.ini file

[uwsgi]
# 这里是你的项目根目录路径
chdir = /home/py_workspace/flask_pro/MyOCR
# 模块名,这里用myapp; myapp:app是指定一个Python的可执行文件,它包括Flask的代码from myapp.app import app
module = myapp:app
# 因为app是启动整个服务的入口,所以是app
callable = app
# 是否启动主进程来管理其他进程
master = true
# 设置进程数
processes = 5
# 每个进程的线程个数
threads = 10
# 这里的sock文件不是某个现成的文件,也不需要事先创建,运行时会自动创建,文件名也是自己定
# socket = /tmp/myapp.sock
# 套接字方式的 IP地址:端口号,搭配 nginx使用socket
socket = 127.0.0.1:8000
# chmod-socket = 660
# 当服务器退出时自动清理环境
vacuum = true
# 超时时间,单位秒
harakiri = 60
# 服务的pid记录文件
pidfile = uwsgi.pid
# 服务的日志文件位置
daemonize = uwsgi.log

After the configuration is complete, we can use the following command to start uWSGI:

#Start uwsgi command
$ cd ./flask_pro/MyOCR #Project folder, execute under uwsgi.ini configuration file
$ uwsgi --ini uwsgi.ini
#Stop uwsgi command
$ uwsgi --stop uwsgi.pid

Step 6: Configure the nginx.conf file

#user root;
#worker_processes  1;
 
events {
    worker_connections  1024;
}
 
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip on;
    # server被称为虚拟主机,可以有多个
    # 第2个主机
    server {
	    # 监听端口号
        listen  80;
	    # 你的服务器ip
        server_name xx.xx.xx.xx;
        charset     utf-8;
        client_max_body_size 75M;   # adjust to taste

        location / {
            # 将nginx所有请求转到uwsgi
            include uwsgi_params;
            # uwsgi的ip与端口
            uwsgi_pass 127.0.0.1:8000;
        }

        location /static {
            # 静态文件目录
            alias /home/py_workspace/flask_pro/MyOCR/static;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
             root   html;
        }
    }

}

After the configuration is complete, start nginx.

cd /usr/sbin #Enter the execution path

Copy code
#Start nginx

./nginx

#Restart nginx

./nginx -s reload

#stop nginx

./nginx -s stop

Step 7: Open the browser to verify the URL

After the deployment and configuration of the uwsgi server and nginx server, after the startup is completed, the deployment is basically completed. Open the browser and view the first Flask project deployed.

xx.xx.xx.xx represents your cloud server’s external access IP, because the default port configured by nginx is 80, and the cloud server generally opens port 80 access rights, and then uwsgi + Flask configures port 8000. You need to organize the network in the cloud server yourself. Add access rights to port 8000. This problem can be solved by searching online. If the network port is solved, directly enter the following URL to view the deployment results.

Problem 2 may occur after the deployment is completed: When opening http://xx.xx.xx.xx/upload, an error message indicates that it cannot be accessed:

Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

There is a problem of automatically adding a trailing slash when accessing through nginx. After configuring nginx, every access is 404. After troubleshooting the cause, I found that this is what happened:
In the back-end code, I wrote @app.route('/ info',methods=['GET','POST']) like this.
When not using uwsgi+nginx deployment, but using flask's own web server for testing, I access xxxx:xx/info and can access the interface.
However, when accessed through nginx, nginx will add a slash to all non-file requests without a slash at the end, give a 301 response, and then redirect to the URL with a slash.
This may be because in some other classic WEB development languages, the request is often a file such as .php, .aspx, .html, etc., while the python framework actually provides a "directory" node as an html file.
This requires adding a slash at the end to let nginx know that this is a request pointing to a directory.

The solution is also very simple. To directly initiate a GET request through the browser (that is, it must be accessed through nginx), in the python code and html code, just remember to add the trailing slash when setting the route.
Because GET requests that do not hit the browser address bar through the keyboard (such as the href value of a hyperlink on the page, or the URL pointed to by AJAX initiation) will not automatically complete the slash, so other pages will not automatically complete the slash. None will be affected.

-- Enter my OCR page

http://xx.xx.xx.xx/upload/

 --------------------------------end --------------------------------

Guess you like

Origin blog.csdn.net/xionghui2007/article/details/132754742