Using Gunicorn and Nginx for Flask Deployment

Tutorial 2 of 5

Flask Deployment with Gunicorn and Nginx: A Detailed Tutorial

1. Introduction

  • Goal of the tutorial: Deploy a Flask application using Gunicorn as a WSGI HTTP Server and Nginx as a reverse proxy server.
  • What you will learn: This tutorial will walk you through the process of deploying a Flask application using Gunicorn and Nginx. After completing this tutorial, you will be able to deploy your own Flask application to a production environment.
  • Prerequisites: Basic knowledge of Python, Flask, and Linux command line. A Flask application ready for deployment. Access to a Linux server.

2. Step-by-Step Guide

  1. Install Gunicorn and Nginx: Use the following commands to install Gunicorn and Nginx on your server:
    sudo apt update sudo apt install python3-pip python3-dev nginx
  2. Set up Flask Application: Navigate to the directory containing your Flask application and install your application's dependencies using pip. If your application is stored in a Git repository, you can clone it onto your server.
  3. Test Gunicorn's ability to serve the project: Use the following command:
    gunicorn --bind 0.0.0.0:5000 wsgi:app
  4. Configure Gunicorn to run as a system service: Create a systemd service file that starts Gunicorn on boot. This file will also configure Gunicorn to start 3 worker processes.
  5. Configure Nginx to proxy requests: Nginx will be set up to pass web traffic to the Gunicorn server using proxy_pass directive.

3. Code Examples

  1. Example of a Gunicorn systemd service file (/etc/systemd/system/myproject.service):
    ```
    [Unit]
    Description=Gunicorn instance to serve myproject
    After=network.target

    [Service]
    User=sammy
    Group=www-data
    WorkingDirectory=/home/sammy/myproject
    Environment="PATH=/home/sammy/myproject/myprojectenv/bin"
    ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app

    [Install]
    WantedBy=multi-user.target
    ```
    This file starts Gunicorn and directs it to serve your Flask application.

  2. Example of an Nginx server block (/etc/nginx/sites-available/myproject):
    ```
    server {
    listen 80;
    server_name your_domain www.your_domain;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/sammy/myproject/myproject.sock;
    }
    

    }
    ```
    This file tells Nginx to listen on port 80 and to proxy requests to the Gunicorn server.

4. Summary

  • Key points covered: We learnt how to deploy a Flask application using Gunicorn and Nginx. We installed the necessary software, set up the Flask application, tested the Gunicorn server, configured Gunicorn and Nginx, and finally started these services.
  • Next Steps: You can now explore more advanced topics like SSL configuration, load balancing with Nginx, or even setting up a database for your Flask application.
  • Additional resources:
  • DigitalOcean Tutorial on deploying a Flask application
  • Flask Mega-Tutorial

5. Practice Exercises

  1. Exercise 1: Deploy a simple Flask "Hello, World!" application using Gunicorn and Nginx.
  2. Exercise 2: Configure Gunicorn to start more worker processes. Monitor the system performance.
  3. Exercise 3: Set up SSL for your Flask application served by Nginx
  4. Solutions and explanations: Solutions can vary depending on the server environment and the Flask application. The main idea is to get hands-on experience with deployment.
  5. Tips for further practice: Try deploying different types of Flask applications. Experiment with different numbers of Gunicorn worker processes. Look into optimizing your Nginx configuration.