Serving Flask Applications with uSWGI on Ubuntu


In this tutorial, we’ll build a sample Python application using Flask and deploy the application using uWSGI and Nginx. Flask is a popular micro framework widely used in web development using Python. It is called a microframework as it doesn’t required any other tools or library to create a web application, and all other resources needed in the process are imported as third party libraries including database abstraction layer.


To complete this tutorial, you will need:

  • Alibaba Cloud Elastic Compute Service (ECS) Ubuntu server with a non-root user with sudo privileges.
  • Basic working knowledge of Nginx and uWSGI.

Step 1 — Setting Up Required Components and Tools

In the first step we’ll install all the dependencies needed to setup the application from Ubuntu repositories and we’ll start with installing python-pip which is a package manager for Python, python-dev which contains the header files needed to compile python extensions and Nginx which will work as a front end reverse proxy server for us.

sudo apt-get update
sudo apt-get install python-pip python-dev nginx
sudo pip install virtualenv
mkdir ~/samepleapp
cd ~/sampleapp
user@instance:~/samepleapp $ virtualenv venv
user@instance:~/samepleapp $ source venv/bin/activate

Step 2 — Creating a Sample Flask Application

Now that we have our virtual environment activated, lets install Flask and uWSGI and get started on creating our Hello World Application.

(venv) user@instance:~/samepleapp $ pip install flask uwsgi
(venv) user@instance:~/samepleapp $ nano
from flask import Flask
app = Flask(__name__)
def hello():
return "It Works!"
if __name__ == "__main__":'',port=3000)
Note: Make sure your port 3000 is open over the Internet.
(venv) user@instance:~/samepleapp $ python

Step 3 — Configuring uWSGI

So now that we have our application ready to be deployed, we’ll move on to uWSGI and configure uWSGI to serve our application. To start with we’ll check if uWSGI is able to start our application using the command line.

from app import app
if __name__ == "__main__":
(venv) user@instance:~/samepleapp $ uwsgi --socket --protocol=http -w wsgi:app
#python module to import
module = wsgi:app
#enable the master process
master = true
#spawn number of workers
processes = 5
#socket file's location
socket = sampleflask.sock
#permissions for the socket file
chmod-socket = 660

Step 4 — Creating systemd unit file

Once we have the configuration file, next we need to create a systemd unit file which will make ubuntu’s init system to start our application everytime the server boots.

#Metadata and dependencies section
Description=Sampleflask service
#Define users and app working directory
ExecStart=/home/<your_user>/sampleflask/venv/bin/uwsgi --ini sampleflask.ini
#Link the service to start on multi-user system up
(venv) user@instance:~/samepleapp $ sudo systemctl start sampleflask
(venv) user@instance:~/samepleapp $ sudo systemctl enable sampleflask

Step 5 — Configuring Nginx to Proxy requests

Our uWSGI service should now be up and running on port 3000, next we need to configure Nginx to proxy the requests on port 80. To do so we’ll need to configure Nginx to pass the web requests to the socket using uwsgi protocol. Nginx comes with native support for passing uwsgi protocol traffic and works out of the box with uwsgi.

server {
# the port your site will be served on
listen 80;
# the IP Address your site will be served on
server_name <your_ip>;
# Proxy connections to application server
location / {
include uwsgi_params;
uwsgi_pass unix:/home/<your_user>/sampleflask/sampleflask.sock;
(venv) user@instance:~/samepleapp $ sudo ln -s /etc/nginx/sites-available/sampleflask /etc/nginx/sites-enabled
(venv) user@instance:~/samepleapp $ sudo ngnix -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
(venv) user@instance:~/samepleapp $ sudo systemctl restart nginx


In this tutorial, we created a simple flask application which runs on uWSGI and is reverse proxied through Nginx, Flask is a simple yet powerful micro framework which can enable you to create complex web application and services. uWSGI and Nginx are again powerful tools which enables you to deploy and manage your Flask application for your users as they grow.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website: