Deploy Flask app in Nginx using uWSGI — with architectural explanation.

Pavan Kumar Yekabote
4 min readApr 6, 2020


A story of flask server’s survival.

It’s all about serving the clients being reliable, efficient, scalable & speed.

Hello Everyone..! In this article, we are going to understand how to deploy python flask web service into NGINX server.

Python flask web server is just a development server, which is incapable to serve as a production grade server. This is where, we start using most powerful and production grade NGINX web server to serve our flask web service.

Let’s start with simple questions…

Does NGINX support Python? If not, How can it serve python based application?

Well basically, NGINX doesn’t support python, which makes us clear that it is incapable of understanding the flask application routes. So, to solve this problem, WSGI comes into play.

WSGI — Web Server Gateway Interface

It is just like a middle man, who forwards requests from web servers, to web applications or frameworks written in Python programming language.

Using the above specifications, we can easily deploy a flask web service in a production grade web server. The deployment process include 3 major steps.

Steps to deploy:

  1. Install NGINX server, configure to serve from WSGI
  2. Install uWSGI server, create wsgi configuration for python app
  3. Start both NGINX and uWSGI servers.

As shown in the above picture, the flask web service is loaded in uWSGI server. The uWSGI server communicates with NGINX server through a unix socket connection.

  • Any http / https request triggered from an http client, is sent to the NGINX server, which can easily manage a large number of requests.
  • Then, NGINX sends/redirects these requests to uWSGI server through unix socket.
  • In turn, the uWSGI responds back with data, returned from the flask web service.

Well, this is all an overview on how things work within. Let’s go through the implementation part now.

Let’s create a simple flask app.

# /home/apps/myapp.pyfrom flask import Flaskapp = Flask(__name__)@app.route('/')
def welcome():
return 'Welcome to flask application'
def test():
return 'This is test route of myapp flask application'
if __name__ == '__main__':'', port=5000)

As discussed in Steps to deploy section, first let’s

Install nginx and configure it to serve from WSGI

The following command installs nginx server in your linux system.

$ sudo apt-get install nginx

Create an nginx configuration to communicate NGINX with WSGI server.

  • Create a file with your app name in /etc/nginx/sites-available/
$ sudo nano /etc/nginx/sites-available/myapp
  • Now paste the following configuration in myapp file
# /etc/nginx/sites-available/myappserver{ # port you want your server to expose for this app
listen 5000;
# Give a name to the server.
server_name myapp;

# This tells nginx to redirect all routes to unix socket.
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/myappserver.sock;

To enable this site, we need to link this configuration file in sites-enabled folder of nginx. The following command does links the file as expected.

$ sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/myapp

That’s it about nginx configuration. now let’s setup uWSGI server

Install uWSGI server and configure to serve flask app:

The following command installs uwsgi server.

$ pip install uwsgi 

Note: Usually while installing uwsgi, sometimes there occurs a linking error with description ‘error linking uWSGI ...’ This occurs due to the incompatibility of gcc in uwsgi. To solve this issue, follow the following steps.

Install uwsgi compatible gcc-4.8.

$ sudo apt-get install gcc-4.8

Replace the gcc file with newly installed gcc-4.8.

$ sudo rm /usr/bin/gcc
$ sudo ln -s /usr/bin/gcc-4.8 /usr/bin/gcc

Now continue installing the uwsgi again.

Resuming back, after installation of uwsgi,

  • Create a uwsgi configuration file in your flask app folder.
$ nano /home/apps/mywsgi.ini
  • Paste the following configuration in the mywsgi.ini file
# /home/apps/mywsgi.ini[uwsgi]# python file where flask object app is defined.
wsgi-file =
# The flask instance defined in
callable = app
enable-threads = true
master = true
processes = 8
# Unix socket on which uwsgi server should listen
# This is the same, which we defined in nginx configuration file
socket = /tmp/myappserver.sock
# Gives permission to access the server.
chmod-socket = 666
vaccum = true
die-on-term = true

Finally save this file. With this all the configurations part is completed.

Now let start the final part.

Start both NGINX and uWSGI servers:

First let’s start the nginx server with the following command.

$ sudo service nginx start

Finally, start the uwsgi server.

$ cd /home/apps
$ uwsgi --ini mywsgi.ini

That’s it. you have successfully deployed your flask application in a production grade nginx server.

Now go to your web browser and browse url http://localhost:5000

You should see, “Welcome to flask application” message in the browser.

Hope, you all are happy, deploying your flask app in a production server.

Share with your friends, if you find this useful. Clap 👏 as max as 50 times.

Pavan Kumar Yekabote.

Pavan Yekabote.