Python: Preparing Python and Flask for production deployment

This documentation is part of the Creating a REST API guide. You can view the complete guide here: How to create a REST API using Flask.

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use Python cloud solution that provides a host of benefits, including:

  • Deploy your application in seconds with a simple git push.
  • Use your own domain name and benefit from the automatic configuration of HTTPS certificates for enhanced security.
  • Enjoy peace of mind with automatic backups, one-click updates, and straightforward, transparent, and predictable pricing.
  • Get optimal performance and robust security thanks to a private and dedicated VM.

Save time and simplify your life: it only takes 5 minutes to try Stackhero's Python cloud hosting solution!

While this guide uses Flask's built-in development server, for production it is essential to use a production-ready WSGI server such as Gunicorn. Follow these steps:

  1. Install Gunicorn:

    pip install gunicorn
    pip freeze > requirements.txt
    
  2. Start your app using Gunicorn with the app:app argument (where the first app is the filename and the second app is the Flask instance):

    ENV=production gunicorn app:app \
      --error-logfile - \
      -b 0.0.0.0:8080
    
  3. Create a Makefile to simplify switching between development and production modes:

    .DEFAULT_GOAL := dev
    
    # By default, Stackhero for Python executes the "run" rule. We override it to run the 'prod' rule.
    run: prod
    
    prod:
     	ENV=production gunicorn app:app \
     	  --error-logfile - \
     	  -b 0.0.0.0:8080
    
    dev:
     	python app.py
    

You can run your server in development mode using make dev (or simply make), and in production mode using make prod.