Serve Python Flask web app with NGINX

I’m running this on Ubuntu environment. If you understand how below things work then you may applied it on other distros

Let say you have Flask app something like this


from flask import Flask

app = Flask(__name__)

def index():
    return '''
                <center><strong>Hello, I'm from Flask application</strong></center>

if __name__ == '__main__':

And then I’m gonna include gunicorn in my python environment. gunicorn is used to manage python process for your web app. I’m using pip to manage python library, so I’m gonna include in requirements.txt and pip install -r requirements.txt

# requirements.txt

After that, I’m gonna write some service file to manage our python process. I put the service file in /etc/systemd/system/python-test.service

Description=Run my Flask web app

ExecStart=/path/to/your/python/venv/bin/gunicorn --workers 2 --bind unix:/var/www/python/python-test.sock -m 007 main:app


Finally, my NGINX config

server {
        listen 80;
        server_name python.test;

        location / {
                include proxy_params;
                proxy_pass http://unix:/var/www/python/python-test.sock;

        location ~ /\.ht {
                deny all;

You are done! You may run systemctl daemon-reload, service python-test restart and service nginx restart for your system to reload the configuration you save just now.

comments powered by Disqus