Table of contents
1.
Introduction
1.1.
Deployment
2.
Flask app Deployment on the localhost
3.
Flask app Deployment on apache
3.1.
Installing mod_wsgi
3.2.
Creating .wsgi file
3.3.
Configuring Apache
4.
Flask app Deployment on Heroku
5.
Frequently Asked Questions
6.
Key Takeaways
Last Updated: Mar 27, 2024

Flask Deployment

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A micro-framework written in python language is called a flask. As flask doesn't require any particular library or tools, that's why it is denoted as microframework. It doesn't contain form validation, database abstraction, or other components where pre-existing third-party libraries provide standard functions.

Deployment

An application must be deployed on a real webserver to switch to a full-fledged production environment from a development environment. There are many ways available for deploying a flask application.

For a small flask application, you can choose any one of the following -

We can use these cloud platforms for flask app deployment. Other options are available, like deploying the flask app on the google cloud platform and using localtunnel service without messing with DNS and firewall settings allows us to share our flask application on localhost.

Flask app Deployment on the localhost

Here the flask application gets deployed in the local environment.

Externally Visible Server

Our flask application has the default nature of being accessible only on the computer on which the development environment is set up on the development server. Furthermore, this default nature is because the user can execute arbitrary code during debugging.

If the debug mode is off, we can set the hostname and make the development server available for the users in the network. 

app.run(host = ’0.0.0.0’)

and then your os will listen to all the public IPs.

Flask app Deployment on apache

For hosting Python-based web applications, a flask application on Apache server, we need an Apache module, mod_wsgi, that provides a WSGI compliant interface.

Installing mod_wsgi

From PyPi install an official release by running the following command on command prompt -

pip install mod_wsgi

Output-

For checking if installation is done successfully, run the following command -

mod_wsgi-express start-server

After this, Apache/mod_wsgi on port 8000 will get started.

It will point your browser to http://localhost:8000/.

Creating .wsgi file

yourapplication.wsgi file should be present, which contains the code mod_wsgi. This code is to get the application object get executed on startup.

Following the given command should be sufficient for most of the applications-

from yourapplication import app as application

Ensure that the libraries and yourapplication we are using should be on the Python load path.

Configuring Apache

The location of the flask application should be provided to mod_wsgi.

<VirtualHost *>
   CodingNinjaexample.com
   WSGIScriptAlias / C:\mydir\myapp.wsgi


   <Directory C:\yourdir>
      Order deny,allow
      Allow from all
   </Directory>


</VirtualHost>

 

Must Read Apache Server

Flask app Deployment on Heroku

Before deploying the flask app to Heroku, ensure that the git and heroku CLI(Command Line Interface) are installed; we will now learn how to use Heroku CLI and git for flask app deployment. Heroku CLI allows you to manage and create applications directly from the terminal.

First of all, create an app on Heroku to prepare it to get your app's source code.

heroku create

When creating your app, the git remote called Heroku is designed and associated with your local git repository.

By default, Heroku gives a random name to your app, but you can also pass a parameter for specifying your app name.

Now for deploying the app on Heroku, run the following code-

git push heroku main

Congratulations! your app is currently deployed.

heroku ps: scale web=1

While running the above command, if you get an error that is "Couldn't find that process type (web)," it means the deployment of your app is taking more time than usual. After waiting for a few minutes, run the same command.

Now you can go to your app from the URL generated by your app name, or you can use the following command to open it in your browser directly-

heroku open

Output-

 

Check out most important Git Interview Questions here.

Frequently Asked Questions

1.Name some platforms on which we can deploy flask applications other than discussed above.

Here are some of the platforms where we can deploy our flask application-

  • Deploying Flask on Google App Engine
  • Deploying Flask on Google Cloud Run
  • Deploying on Azure (IIS)
  • Deploying on PythonAnywhere
  • Deploying Flask on AWS Elastic Beanstalk

 

2.What dependencies are required for flask app deployment on the Google app engine?

App engine uses pip ( python package manager ) to install dependencies during deployment, defined in a file located in your project's root directory, the ‘requirements.txt’ metadata file. As App Engine performs a fresh install, we do not need to upload dependencies. Also, Pipfile/Pipfile.lock files should not be present in your project, and dependency specifications using these files are not currently supported.

3.Name some Standalone WSGI containers.

WSGI applications and HTTP are contained by many popular servers written in python.

  • Gunicorn
  • Gevent
  • Twisted Web
  • Tornado

Key Takeaways

In this blog, we learned about Flask Deployment. Don't come to a halt here. Check out our Flask vs Django in 2021:Which Framework to Choose? Coding Ninjas Blog . Can you also check out the Top 30 Intermediate Django Interview Questions: Part 2 | CN Blog  blogs? Check out more blogs here. More blogs.

 

Happy Learning!

Live masterclass