Setting Up a Flask Application on Alibaba Cloud ECS Ubuntu 16.04

Setting Up the System

For the purpose of this guide, we will setup an ECS instance with Ubuntu 16.04 LTS. The procedure itself is pretty straightforward — after we log into Alibaba Cloud web management console, we select Elastic Compute Service in the leftmost main menu, and choose Instances in the sub-menu. We will then be able to start the process of creating new instance by clicking the Create Instance button in the top-right corner of the screen.

Setting Up the Environment

Provided that we created our instance successfully, we will connect to it’s public IP, and update repositories and, optionally, upgrade the system: apt-get update && apt-get upgrade. If we have logged in as root, we don't need to use sudo.

curl -L | bash
export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
apt-get install build-essential python-dev python-setuptools python-pip python-smbus libncursesw5-dev libgdbm-dev libc6-dev zlib1g-dev libsqlite3-dev tk-dev libssl-dev openssl libffi-dev zlib1g-dev libbz2-dev
libreadline-dev -y
pyenv install -l | grep 3.7
pyenv global 3.7.0

Installing Flask and Server Software

We install flask by issuing command pip install Flask. Next thing we do is create a minimal Flask Hello World app. We will create a directory to contain our app under /var/www:

cd /var/wwwmkdir flaskapp
from flask import Flask
application = Flask(__name__)

def hello_world():
return 'Hello World!'
pip install uwsgi
sudo apt-get install nginx -y
# the upstream component nginx needs to connect to
upstream flask {
server unix:///tmp/flaskapp.sock;
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name; # substitute with your domain name
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass flask;
include /var/www/flaskapp/uwsgi_params; # the uwsgi_params file you installed

Setting Up UWSGI App as a Service

First thing we will do is to put our runtime settings for uwsgi into an ini file:

chdir = /var/www/flaskapp
module = flaskapp
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe)
socket = /tmp/flaskapp.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664
chown-socket = www-data:www-data
daemonize = /var/log/uwsgi.log
vacuum = true
ln -T /root/.pyenv/shims/uwsgi /usr/local/bin/uwsgi
#!/bin/sh -e
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.
/usr/local/bin/uwsgi --ini /var/www/flaskapp/uwsgi.iniexit 0


In this guide we have covered the setup and deployment of a Flask application to Alibaba Cloud Elastic Compute Service (ECS) instance, addressing potential issues along the way. Real, production setup will also, probably, use more subtleties and features of both Nginx (caching, for example), and Flask, and possibly SSL encryption, but this is, hopefully, a useful starting point.



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: