Building a Multi-Tier Flask App with Gunicorn, Nginx, PostgreSQL, and Systemd Using Ansible

In this blog, we’ll walk through how to automate the deployment of a production-grade, multi-tier Flask application using Ansible. This deployment stack uses Gunicorn to serve the Flask app, Nginx as a reverse proxy, PostgreSQL as the backend database, and Systemd to manage services — all orchestrated through Ansible playbooks for repeatability and automation. Project Overview The core idea behind this project is to take a typical Flask app and elevate it into a real-world, scalable deployment using industry-standard components. By automating every step using Ansible, we eliminate manual setup, reduce configuration drift, and create a consistent deployment pipeline — all from scratch on a local or remote Ubuntu machine. Stack Components Ansible: Handles orchestration and automation Flask: Lightweight Python web framework Gunicorn: WSGI HTTP server for running Flask Nginx: Acts as a reverse proxy and static file server PostgreSQL: Relational database for application storage Systemd: Ensures services like Gunicorn and PostgreSQL are managed and restarted if needed Objectives Automate the full-stack deployment of a Flask application Use Ansible to manage packages, copy files, create users, and configure services Set up a proper separation between the app layer (Flask/Gunicorn), proxy layer (Nginx), and database (PostgreSQL) Configure Systemd services to ensure auto-start and reliability Learn practical Ansible skills for infrastructure as code (IaC) Workflow Clone the repository and enter the ansible directory. Set up the inventory file to target your localhost or remote machine. Customize application and database settings in the Ansible playbook if needed. Run the playbook using ansible-playbook, which installs dependencies, configures Nginx, sets up the database, and deploys the app. Access the app in your browser and validate that all services are running using systemctl. Project Structure multi-tier-flask-app/ ├── app/ # Flask app code ├── ansible/ │ ├── inventory.ini # Target host(s) │ ├── playbook.yml # Main Ansible automation │ └── templates/ # Jinja2 templates for configs └── README.md Requirements in my case Ubuntu system with Python 3.8+ and Ansible installed Sudo access to install and manage system services Internet access for package installation This hands-on project is perfect for DevOps beginners and intermediate users who want to solidify their understanding of Ansible, Linux services, and full-stack deployment patterns. It bridges the gap between development and operations by combining automation with real-world application deployment.

May 4, 2025 - 05:05
 0
Building a Multi-Tier Flask App with Gunicorn, Nginx, PostgreSQL, and Systemd Using Ansible

In this blog, we’ll walk through how to automate the deployment of a production-grade, multi-tier Flask application using Ansible. This deployment stack uses Gunicorn to serve the Flask app, Nginx as a reverse proxy, PostgreSQL as the backend database, and Systemd to manage services — all orchestrated through Ansible playbooks for repeatability and automation.

Project Overview

The core idea behind this project is to take a typical Flask app and elevate it into a real-world, scalable deployment using industry-standard components. By automating every step using Ansible, we eliminate manual setup, reduce configuration drift, and create a consistent deployment pipeline — all from scratch on a local or remote Ubuntu machine.

Stack Components

  • Ansible: Handles orchestration and automation
  • Flask: Lightweight Python web framework
  • Gunicorn: WSGI HTTP server for running Flask
  • Nginx: Acts as a reverse proxy and static file server
  • PostgreSQL: Relational database for application storage
  • Systemd: Ensures services like Gunicorn and PostgreSQL are managed and restarted if needed

Objectives

  • Automate the full-stack deployment of a Flask application
  • Use Ansible to manage packages, copy files, create users, and configure services
  • Set up a proper separation between the app layer (Flask/Gunicorn), proxy layer (Nginx), and database (PostgreSQL)
  • Configure Systemd services to ensure auto-start and reliability
  • Learn practical Ansible skills for infrastructure as code (IaC)

Workflow

  1. Clone the repository and enter the ansible directory.
  2. Set up the inventory file to target your localhost or remote machine.
  3. Customize application and database settings in the Ansible playbook if needed.
  4. Run the playbook using ansible-playbook, which installs dependencies, configures Nginx, sets up the database, and deploys the app.
  5. Access the app in your browser and validate that all services are running using systemctl.

Project Structure

multi-tier-flask-app/
├── app/                   # Flask app code
├── ansible/
│   ├── inventory.ini      # Target host(s)
│   ├── playbook.yml       # Main Ansible automation
│   └── templates/         # Jinja2 templates for configs
└── README.md

Requirements in my case

  • Ubuntu system with Python 3.8+ and Ansible installed
  • Sudo access to install and manage system services
  • Internet access for package installation

This hands-on project is perfect for DevOps beginners and intermediate users who want to solidify their understanding of Ansible, Linux services, and full-stack deployment patterns. It bridges the gap between development and operations by combining automation with real-world application deployment.