Web App Development

Deploying a Flask app to Ubuntu with Python Paste

Here's a step by step guide to serving a Flask application.

Installing Paste, WSGI Utilities and Flask

Pretty straightforward:

apt-get update
apt-get install python-pastescript -y
apt-get install python-pip -y

pip install wsgiutils
pip install flask

Creating a wsgi.py file to expose the application

The wsgi.py just contains a factory function that returns an instance of Flask's wsgi_app. The code assumes that your Flask application is called "app" and the file that contains it your_app_package.py:

from your_app_package import app

def app_factory(global_config, **local_config):
    return app.wsgi_app

Creating a config file for Paster

Create a config.ini file like this:
[app:main]
paste.app_factory = wsgi:app_factory
[server:main]
use = egg:PasteScript#wsgiutils
host = 123.45.67.8
port = 80
Make sure to replace the host property with the IP address of your server.

Running Paster

Add the current directory to the python path and pass the config file to `paster serve`:

export PYTHONPATH=.
paster serve ./config.ini

Now your website should be served and at least accessible locally. Try `curl localhost` to confirm.

Update firewall settings if necessary

If the site is only available locally but con't be accessed by the IP address you might need to update firewall settings to allow traffic from port 80, both for your hosting service (e.g. rules in the EC2 security group) and on your machine (disable iptables to confirm this). Here's more information on this: http://cloud.lib.wfu.edu/blog/tech/2010/09/28/managing-firewall-rules-on-amazon-ec2-servers/


Comments


Follow me on Twitter
I'm building monitoring tool for site speed and Core Web Vitals.
➔ Start monitoring your website or run a free site speed test