How to serve a Flask application with uWSGI and Nginx

Published on Dec. 19, 2018 by Gabriel Bordeaux


This is a simple introduction on how to serve a Python Flask application with uWSGI and Nginx on Ubuntu.


Install Nginx and Python:

sudo apt-get update
sudo apt-get install python3-pip python3-dev nginx
pip3 install uwsgi flask

Create a basic Flask application

mkdir /var/www/html/my_app
cd /var/www/html/my_app

Create a file with:


from flask import Flask
app = Flask(__name__)

def index():
    return """
        Welcome to my website!<br /><br />
        <a href="/hello">Go to hello world</a>

def hello():
    return """
        Hello World!<br /><br />
        <a href="/">Back to index</a>

if __name__ == '__main__':
    # Will make the server available externally as well'')

Test your application with flask built-in web server

Flask has a built in web server which you can use for test purposes. Just run:

# * Serving Flask app ""
# * Environment: production
#   WARNING: Do not use the development server in a production environment.
#   Use a production WSGI server instead.
# * Debug mode: off
# * Running on (Press CTRL+C to quit)

Alternatively, you can also use: flask run --host=

And visit

Once you are able to confirm that your Flask application is up and running with the development web server, it is time to configure uWSGI and Nginx.

Configure uWSGI

In the same directory, create a file with:


from app import app as application

if __name__ == "__main__":

You can then test uWSGI:

uwsgi --socket --protocol=http -w wsgi

And visit

Create a uWSGI Configuration File

Also in your project directory, create a file app.ini with:

module = wsgi

socket = my_app.sock
chmod-socket = 660
vacuum = true

master = true
processes = 10

die-on-term = true

You can test the app.ini file with:

/usr/local/bin/uwsgi --ini /var/www/html/my_app/app.ini

Change the project directory ownership to www-data

chown -R www-data.www-data /var/www/html/my_app

Create a uWSGI startup file

Then create a systemctl startup file /etc/systemd/system/my_app.service:

Description="uWSGI server instance for my_app"

ExecStart=/usr/local/bin/uwsgi --ini /var/www/html/my_app/app.ini


Start the process:

sudo systemctl start my_app

# Check the status
sudo systemctl status my_app.service

# You should be able to see the socket with
ls /var/www/html/my_app/my_app.sock
# /var/www/html/my_app/my_app.sock

# Enable it on startup
sudo systemctl enable my_app

Configure Nginx

Create a file /etc/nginx/sites-enabled/my_app:

server {
    listen 80;
    server_name; # Replace with the actual domain name

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/var/www/html/my_app/my_app.sock;

Test your Nginx configuration and restart Nginx:

nginx -t -c /etc/nginx/nginx.conf
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

/etc/init.d/nginx restart

You are all set, just visit the domain in your browser!