The warning from Flask is there to prevent you from using the development server in production. Indeed, it is not very efficient or secure because it includes a set of functionality for developing and debugging (such as the debugger and reloader). You can find a list WSGI server you can use at Deploying to Production — Flask Documentation (3.0.x). We will dwell into details on using Waitress which is a very easy to setup solution with good performances:
First, you need to install Waitress on your virtual environment with:
pip install waitress
Then you will need to launch your app. This will depend on where your app is created. This is generally done in a Python file close to the root with a line that looks like:
app = flask.Flask(__name__)
(you need to find where Flask
is called)
Let’s say this line is in a file named flask_app.py
at the location: /projects/flask_deployement/src/flask_app.py
Move to the project root with cd /projects/flask_deployement
, noting that the name of the variable running flask was app
, we will send the location of the app to Waitress with the path: src.flask_app:app
If we then run Waitress on our server, on port 5000 (this is an example, you can take any available port), we will run the command:
waitress-serve --host 127.0.0.1 --port 5000 src.flask_app:app
Your app is now running on port 5000. If you have a page on the root endpoint, you can test that the app runs successfully with: curl http://127.0.0.1:5000/
If you want help with the whole process of deploying a Flask app in production on a server, I have written a whole tutorial here