Getting a Flask website up and running in Ubuntu

In the dynamic digtal space, there exists more than one procedure to setup a domain infrastructure. This article will highlight the steps to get a simple website running on a flask environment. The important  tools that we need to ensure are installed before setting up the flask environment are:

  • python –  a standard installation of python-2.7
  • Nginx

 Nginx [engine x] is an HTTP and reverse proxy server program that can as well be implemented as a mail proxy server. 

To install nginx you first need to add the repository. Add the following to /etc/apt/sources.list.d/nginx-lucid.list :

deb lucid nginx
deb-src lucid nginx

Add the gpg key to the apt keyring:

sudo apt-key add nginx_signing.key
rm nginx_signing.key

Install nginx by running the commands below in a terminal

apt-get update
apt-get install nginx

installing uWSGI

use pip to install the latest version of uWSGI :

sudo apt-get install python-dev build-essential python-pip
sudo pip install uwsgi

On the first configuration, configure uWSGI to run as a daemon, thereafter creating a separate uwsgi user:

sudo useradd -c ‘uwsgi user,,,’ -g nginx -d /nonexistent -s /bin/false uwsgi

It is also neccessary to create an upstart configuration file, to auto-run uWSGI at the background. This can be done by creating  a new file called /etc/init/uwsgi.conf

and edit it with the content below:

description “uWSGI”
start on runlevel [2345]
stop on runlevel [06]


exec uwsgi –master –processes 4 –die-on-term –uid uwsgi –gid nginx –socket /tmp/uwsgi.sock –chmod-socket 660 –no-site –vhost –logto /var/log/uwsgi.log

create a logging environment by adding a logrotate configuration file at /etc/logrotate.d/uwsgi

/var/log/uwsgi.log {
rotate 10
create 640 uwsgi adm
initctl restart uwsgi >/dev/null 2>&1

 automate and keep them persistant by typing:

sudo touch /var/log/uwsgi.log
sudo logrotate -f /etc/logrotate.d/uwsgi

At this point you may run into a postrotate script  error while attempting to execute the logrotate command. That will be ok meanwhile as uWSGI is not currently running.

installing virtualenv

You can use the pip command to install virtualenv:

sudo pip install virtualenv

Configuring the flask environment

The first website for experiment will be the helloworld! therefore let us go straight to creating it;

create the  initial directory:

sudo mkdir -p /srv/www/helloworld

and a subdirectory for static files, to be hosted by nginx:

cd /srv/www/helloworld
mkdir static

Then also create a virtual environment for the application to run in:

virtualenv ./env

and to install Flask in that environment:

source env/bin/activate
pip install Flask

When all is successful and done, within the created folder create a sample ‘Hello World’ application, in

from flask import Flask
app = Flask(__name__)

def hello_world():
return ‘Hello World!

if __name__ == ‘__main__’:

The permissions of the directory now have to be configured. uWSGI needs read permission to read the contents of the scripts, and write permission to save compiled python files:

sudo usermod -a -G nginx $USER
sudo chown -R $USER:nginx /srv/www/helloworld
sudo chmod -R g+w /srv/www/helloworld


Before configuring nginx, first remove the default configuration file:

sudo rm /etc/nginx/conf.d/default.conf

Edit,  the helloworld configuration file at /etc/nginx/conf.d/helloworld.conf

server {
listen       80;
server_name  localhost;

location /static {
alias /srv/www/helloworld/static;

location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
uwsgi_param UWSGI_PYHOME /srv/www/helloworld/env;
uwsgi_param UWSGI_CHDIR /srv/www/helloworld;
uwsgi_param UWSGI_MODULE application;
uwsgi_param UWSGI_CALLABLE app;

error_page   404              /404.html;

error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   /usr/share/nginx/html;

Now it is possible for you to run your successfully configured  website, simply by typing the commands below:

sudo service uwsgi restart
sudo service nginx restart

Activating  flask environment for development

The above configuration in fact is a virtual environment which can host multiple websites;

within our virtual env folder we can test our newly created uWSGI as an http server

uwsgi –http –pyhome ./env –module application –callable app

and uWSGI will create an HTTP server hosting any development on port 9090.