ubuntu uwsgi flask

Last updated on February 1st, 2022 at 12:53 pm

This is a simple tutorial on how to install uWSGI server in Ubuntu 20.04 LTS (Focal Fossa) and deploy a simple Flask application. I am also explaining below how to enable debug mode and specify FLASK_APP(flask application file name instead of default) with an option to specify port.

You might also be interested in taking a look at this tutorial, Configure uWSGi Nginx to run Python applications 

Step 1

Understand your server configuration. In my case I am running on

NAME="Ubuntu"
VERSION="20.04 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

Step 2

Install pip3

apt install python3-pip

Step 3

Install uWSGI and check the version installed. Version of uWSGI I am running installing is 2.0.20

pip3 install uwsgi
server:# uwsgi --version
2.0.20

We can also cross check the Python version on which uWSGI is installed

Note: uWSGI we just installed can be used as a production server to forward request to Flask running on Werkzeug’s development WSGI server. It is not recommended to run development server in production hence we need uWSGI to push requests.

uwsgi --python-version

Step 4

Let’s create a simple application using flask . For that I am creating a directory under /root (just for demo) and create a file named app.py

mkdir /root/myapp
cd /root/myapp

Create a file named “app.py” and add the content below. Save the file.

~/myapp# cat app.py
from flask import Flask
app= Flask(__name__)
@app.route("/get")
def start():
    return "<h2>Hello From Server</h2>"
if __name__ == "__main__":
    app.run(host="0.0.0.0")

Step 5

Next step is to execute flask run command like below

~/myapp# flask run
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

As you can see the server is now listening to port 5000 on 127.0.0.1

Run curl command against http://127.0.0.1:5000/get and you should get the output below.

# curl  http://127.0.0.1:5000/get
<h2>Hello From Server</h2>

Step 6 (Optional)

If you want to specify name of the application file instead of app.py and also the port to listen use this command. We can even enable debug mode.

Assuming that my application file name is mynewapp.py and port to run is 4001 we can use export command for providing application name and to enable debug mode. To specify the port simple –port flag should suffice.

~/myapp# export FLASK_APP=mynewapp.py
~/myapp# export FLASK_DEBUG=1
~/myapp# flask run --port 4001
 * Serving Flask app 'mynewapp.py' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:4001/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 606-572-470
127.0.0.1 - - [13/Jan/2022 16:27:23] "GET /get HTTP/1.1" 200 -

Test the app by using the curl command similar to Step 4 (make sure to replace the port with 4001) . You can see from the output that the debugger is active.

Leave a Reply

Your email address will not be published. Required fields are marked *