Setting Up a Flask Application on Alibaba Cloud ECS Ubuntu 16.04

Setting Up the System

Setting Up the Environment

curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | 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

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

@application.route('/')
def hello_world():
return 'Hello World!'
pip install uwsgi
sudo apt-get install nginx -y
nginx
# 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 myxydomain.com; # 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

bash
[uwsgi]
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
bash
#!/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

Conclusion

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

PyInstaller with Pandas — Problems, solutions and workflow with code examples

Checked versus Unchecked Exceptions: Missing the Point Entirely

15+ Best Docker Tutorials For Beginners — Learn Docker Online

Deploying ML Apps on Heroku

Using Sidecar Mode for Kubernetes Log Collection

Testing Alexa Skills — The grail quest

Lesson 46: The Binary Search Algorithm

The day the IoT died

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

More from Medium

Elasticsearch Workshop #2: ingest pipelines

Using Nginx Ingress Controller to Manage HTTP/1.1 and HTTP/2 Protocols

First steps, searching for data with elastic

Kafka Instances, Made Simple: Introducing KafMira