General situation of

During development, we typically run Flask programs directly from Python commands. Such a run is only suitable for our development, convenient for us to debug. Flask applications run this way tend to have low performance once they are deployed online. UWSGI +Nginx can be used for deployment.

uWSGI

Before we deploy, we need to understand a few concepts

  • Interfaces between WSGI Web applications. It acts as a bridge between the Web server and the Web application framework.

  • Uwsgi is a transport protocol that defines the type of information to be transmitted.

  • UWSGI is a Web server that implements THE uWSGI protocol, WSGI.

The deployment of

Start by preparing a flask program named run.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index(a):
    return 'deployment'

if __name__ == '__main__':
    app.run()
Copy the code

Install uWSGI

 pip install uwsgi
Copy the code

Create a UWSGi configuration file in the project directory

[uwsgi]
Use with nginx
socket = 127.0. 01.:8000
# project path/Users/xiaoyuan/Desktop/flask_testChdir = own project pathThe wsgi file run is the flask startup file with the suffix removed. App is the flask object in run.py
module          = run:app
# specify the worker process
processes       = 4
# main process
master          = true
Each worker process has 2 threads
threads = 2
# refers to the place where the background starts the log output
daemonize       = uwsgi.log
Save the process number of the main process
pidfile = uwsgi.pid
# Virtual environment environment path
virtualenv = /Users/xiaoyuan/.virtualenvs/flask_test
Copy the code

Then configure Nginx, go to the Nginx configuration file nginx.conf, and add the following code to the HTTP block

server {
	# monitor port
    listen 80;
    Change the listening IP address to the public IP address of the server
    server_name 127.0. 01.;
 
	# dynamic request
	location / {
	  include uwsgi_params;
	  uwsgi_pass 127.0. 01.:8000;
	}
	Static requestlocation /static { alias /Users/xiaoyuan/Desktop/flask_test/static; }}Copy the code

Start the

Start the uWSGI

uwsgi --ini uwsgi.ini
Copy the code

Start the Nginx

/etc/init.d/nginx start
Copy the code

Then access the IP that Nginx listens on, and since I’m deployed locally, go directly to http://127.0.0.1:80/

Other commands

  • Uwsgi restart
uwsgi --reload uwsgi.pid
Copy the code
  • Uwsgi stop
uwsgi --stop uwsgi.pid
Copy the code

Welcome to follow my official account: