Deploying Flask

Deployment is the process of moving your app from your laptop onto a live server so anyone on the internet can use it — replacing the development server with a robust production setup.

Learn Deploying Flask in our free Flask course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Flask course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll pin your dependencies in a requirements file, serve your app with the Gunicorn WSGI server, and configure it safely using environment variables.

The server you start with flask run is a development server . It is convenient for coding, but it handles one request at a time, is slow under load, and is not hardened against attacks.

For real traffic you need a production WSGI server . WSGI is the standard interface between Python web apps and web servers. Flask is a WSGI app, so a server like Gunicorn can run it efficiently and serve many users at once.

A server needs to install the exact packages your app uses. List them in requirements.txt so they can be installed with one command. You also need a module that exposes the app object Gunicorn will run.

Generate it from your environment and install on the server:

Now expose the app object Gunicorn will serve. In app.py you simply have a module-level app :

Start the production server with the gunicorn command. Add --workers to run several processes, and --bind to choose the address and port. Read your secrets from environment variables so they never live in your code.

Your Flask code reads those variables with os.environ , so the same code is safe everywhere:

Hosting platforms like Render, Railway, or Fly.io let you set these environment variables in a dashboard and run your gunicorn command for you. The dev server's flask run stays on your laptop.

Complete the production config below. Replace each ___ so secrets come from the environment and debug is off.

Gunicorn could not find your app object. Check the module:variable name — for a factory use gunicorn "app:create_app()" .

❌ The app works locally but crashes on the server

A missing package or unset environment variable is the usual cause. Confirm requirements.txt is complete and every os.environ.get value is set on the host.

Make a tiny app deploy-ready by reading its port and secret from the environment.

Lesson 21 complete — you can put Flask online!

You now know why the dev server stays local, how WSGI and Gunicorn serve real traffic, and how to pin dependencies and load secrets from the environment. Your app is ready for the world.

🚀 Up next: Capstone — combine everything into one complete CRUD application.

Practice quiz

Why should you not use 'flask run' in production?

  • It cannot read files
  • The dev server is single-threaded and not hardened
  • It does not support routes
  • It requires a database

Answer: The dev server is single-threaded and not hardened. The built-in dev server is for development; it is slow and insecure under real load.

What does WSGI stand for?

  • Web Server Gateway Interface
  • Web Socket Gateway Integration
  • Wide Scale Gunicorn Interface
  • Worker Service Gateway Instance

Answer: Web Server Gateway Interface. WSGI is the Web Server Gateway Interface, the standard between Python apps and servers.

Which is a production-grade WSGI server for Flask?

  • npm
  • webpack
  • Gunicorn
  • nodemon

Answer: Gunicorn. Gunicorn is a common production WSGI server you point at your app object.

What does the command 'gunicorn app:app' mean?

  • run two apps
  • app twice for redundancy
  • app database name
  • module app.py, variable app inside it

Answer: module app.py, variable app inside it. The first 'app' is the module/filename, the second is the Flask object.

What is requirements.txt used for?

  • pinning the exact dependencies to install
  • storing secrets
  • routing URLs
  • configuring the database schema

Answer: pinning the exact dependencies to install. It lists dependencies installed with pip install -r requirements.txt.

How should production secrets like SECRET_KEY be supplied?

  • hard-coded in app.py
  • from environment variables via os.environ
  • committed to git
  • in requirements.txt

Answer: from environment variables via os.environ. Read secrets from environment variables so they stay out of source code.

Which command freezes your installed packages into a file?

  • pip list
  • pip show
  • pip freeze > requirements.txt
  • pip dump

Answer: pip freeze > requirements.txt. pip freeze writes the current packages and versions to requirements.txt.

What should DEBUG be set to in production?

  • True
  • 1
  • 'on'
  • False

Answer: False. Debug mode must be off in production; it exposes a code-executing debugger.

Which Gunicorn flag chooses the address and port to listen on?

  • --bind
  • --port-only
  • --listen-addr
  • --socket-host

Answer: --bind. --bind 0.0.0.0:8000 sets the interface and port Gunicorn serves on.

Which Gunicorn flag runs several worker processes?

  • --threads
  • --workers
  • --procs
  • --forks

Answer: --workers. --workers N runs N worker processes to handle concurrent traffic.