Serving Flask Applications with uSWGI on Ubuntu

Introduction

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.

Prerequisites

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.

bash
sudo apt-get update
sudo apt-get install python-pip python-dev nginx
bash
sudo pip install virtualenv
bash
mkdir ~/samepleapp
cd ~/sampleapp
bash
user@instance:~/samepleapp $ virtualenv venv
bash
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.

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

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.

python
from app import app
if __name__ == "__main__":
app.run()
bash
(venv) user@instance:~/samepleapp $ uwsgi --socket 0.0.0.0:3000 --protocol=http -w wsgi:app
ini
[uwsgi]
#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.

ini
#Metadata and dependencies section
[Unit]
Description=Sampleflask service
After=network.target
#Define users and app working directory
[Service]
User=<your_user>
Group=www-data
WorkingDirectory=/home/<your_user>/sampleflask
Environment="PATH=/home/<your_user>/sampleflask/venv/bin"
ExecStart=/home/<your_user>/sampleflask/venv/bin/uwsgi --ini sampleflask.ini
#Link the service to start on multi-user system up
[Install]
WantedBy=multi-user.target
bash
(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.

nginx
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;
}
}
bash
(venv) user@instance:~/samepleapp $ sudo ln -s /etc/nginx/sites-available/sampleflask /etc/nginx/sites-enabled
bash
(venv) user@instance:~/samepleapp $ sudo ngnix -t
bash
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
bash
(venv) user@instance:~/samepleapp $ sudo systemctl restart nginx

Conclusion

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:https://www.alibabacloud.com